VTK 图像基本操作_彩色图像生成灰度图像
转载
1.灰度图像映射
vtkImageLuminance负责将一个RGB彩色图像转换为一个单组分的灰度图像。映射公式如下:luminance = 0.3*R + 0.59*G + 0.11*B
该公式中,R为输入图像的第一分量(红色),G为第二分量(绿色),B为第三分量(蓝色)。这个计算结果计算一个RGB颜色的亮度。该类的使用也比较简单,用户无须设置参数,代码如下:
1 #include <vtkAutoInit.h>
2 VTK_MODULE_INIT(vtkRenderingOpenGL);
3
4 #include <vtkSmartPointer.h>
5 #include <vtkBMPReader.h>
6 #include <vtkImageLuminance.h>
7 #include <vtkImageActor.h>
8 #include <vtkRenderer.h>
9 #include <vtkRenderWindow.h>
10 #include <vtkRenderWindowInteractor.h>
11 #include <vtkInteractorStyleImage.h>
12
13 int main()
14 {
15 vtkSmartPointer<vtkBMPReader> reader =
16 vtkSmartPointer<vtkBMPReader>::New();
17 reader->SetFileName("lena.bmp");
18
19 vtkSmartPointer<vtkImageLuminance> luminanceFilter =
20 vtkSmartPointer<vtkImageLuminance>::New();
21 luminanceFilter->SetInputConnection(reader->GetOutputPort());
22 luminanceFilter->Update();
23 //原始演员
24 vtkSmartPointer<vtkImageActor> origActor =
25 vtkSmartPointer<vtkImageActor>::New();
26 origActor->SetInputData(reader->GetOutput());
27 //灰度演员
28 vtkSmartPointer<vtkImageActor> shiftscaleActor =
29 vtkSmartPointer<vtkImageActor>::New();
30 shiftscaleActor->SetInputData(luminanceFilter->GetOutput());
31 double origView[4] = { 0.0, 0.0, 0.5, 1.0 };
32 double shiftscaleView[4] = { 0.5, 0.0, 1.0, 1.0 };
33 //原始演员化妆
34 vtkSmartPointer<vtkRenderer> origRender =
35 vtkSmartPointer<vtkRenderer>::New();
36 origRender->SetViewport(origView);
37 origRender->AddActor(origActor);
38 origRender->ResetCamera();
39 origRender->SetBackground(1.0, 1.0, 1.0);
40 //灰度演员化妆
41 vtkSmartPointer<vtkRenderer> shiftscaleRender =
42 vtkSmartPointer<vtkRenderer>::New();
43 shiftscaleRender->SetViewport(shiftscaleView);
44 shiftscaleRender->AddActor(shiftscaleActor);
45 shiftscaleRender->ResetCamera();
46 shiftscaleRender->SetBackground(1.0, 1.0, 1.0);
47
48 vtkSmartPointer<vtkRenderWindow> renderwindow =
49 vtkSmartPointer<vtkRenderWindow>::New();
50 renderwindow->AddRenderer(origRender);
51 renderwindow->AddRenderer(shiftscaleRender);
52 renderwindow->SetSize(720, 480);
53 renderwindow->Render();
54 renderwindow->SetWindowName("RGB to Gray");
55
56 vtkSmartPointer<vtkRenderWindowInteractor> rwi =
57 vtkSmartPointer<vtkRenderWindowInteractor>::New();
58 vtkSmartPointer<vtkInteractorStyleImage> style =
59 vtkSmartPointer<vtkInteractorStyleImage>::New();
60 rwi->SetInteractorStyle(style);
61 rwi->SetRenderWindow(renderwindow);
62 rwi->Initialize();
63
64 rwi->Start();
65 return 0;
66 }
本文章为转载内容,我们尊重原作者对文章享有的著作权。如有内容错误或侵权问题,欢迎原作者联系我们进行内容更正或删除文章。