VTK学习之二值化(vtkImageThreshold)、阈值

 参考大神博客:VTK修炼之道31:图像二值化_阈值法_二值化阈值_沈子恒的博客-CSDN博客

 vtkImageThreshold中设置临界值、如果大于这个临界值,输出的像素就是255,如果小于这个临界值,输出的像素就是0。

上代码:

#include 

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 

VTK_MODULE_INIT(vtkRenderingOpenGL2);
VTK_MODULE_INIT(vtkInteractionStyle);

void main()
{
	vtkSmartPointer reader = vtkSmartPointer::New();
	reader->SetFileName("234.jpg");
	reader->Update();

	//二值化阈值,设置如通过大于90的所有像素输出值为255,而小于90的像素值为0
	vtkSmartPointer vtkThreshould = vtkSmartPointer::New();
	vtkThreshould->SetInputConnection(reader->GetOutputPort());
	vtkThreshould->ThresholdByUpper(90);//设置二值化的界限值
	vtkThreshould->SetInValue(255);
	vtkThreshould->SetOutValue(0);
	vtkThreshould->Update(); //算法执行后必须添加更新消息!!!

	vtkSmartPointer origActor = vtkSmartPointer::New();
	origActor->SetInputData(reader->GetOutput());
	vtkSmartPointer threshouldActor = vtkSmartPointer::New();
	threshouldActor->SetInputData(vtkThreshould->GetOutput());

	double origView[4] = { 0, 0, 0.5, 1 };
	double threshouldView[4] = { 0.5, 0, 1, 1 };
	vtkSmartPointer origRender = vtkSmartPointer::New();
	origRender->SetViewport(origView);
	origRender->AddActor(origActor);
	origRender->ResetCamera();
	origRender->SetBackground(0, 0, 0);

	vtkSmartPointer threshouldRender = vtkSmartPointer::New();
	threshouldRender->SetViewport(threshouldView);
	threshouldRender->AddActor(threshouldActor);
	threshouldRender->ResetCamera();
	threshouldRender->SetBackground(0, 0, 0);

	vtkSmartPointer renderWindow = vtkSmartPointer::New();
	renderWindow->AddRenderer(origRender);
	renderWindow->AddRenderer(threshouldRender);
	renderWindow->SetSize(640, 240);
	renderWindow->Render();
	renderWindow->SetWindowName("Threshould Image");

	vtkSmartPointer interactor = vtkSmartPointer::New();
	vtkSmartPointer style = vtkSmartPointer::New();
	interactor->SetInteractorStyle(style);
	interactor->SetRenderWindow(renderWindow);
	interactor->Initialize();
	interactor->Start();
}

最终效果:

VTK学习之二值化(vtkImageThreshold)、阈值_第1张图片

 左侧是原图,右侧是二值化之后的图

你可能感兴趣的:(学习)