#include "vtkAutoInit.h"
VTK_MODULE_INIT(vtkRenderingOpenGL2);
VTK_MODULE_INIT(vtkInteractionStyle);
#include <vtkPNGReader.h>
#include <vtkImageViewer.h>
#include <vtkRenderWindowInteractor.h>
#include <opencv2/opencv.hpp>
#include <vtkImageData.h>
#include <vtkMapper.h>
#include <vtkSmartPointer.h>
#include <vtkImageImport.h>
#include <vtkCamera.h>
#include <vtkImageFlip.h>
#include <vtkRenderer.h>
#include <vtkImageActor.h>
using namespace cv;
void Ipl2VTK(cv::Mat& src, vtkImageData* dst)
{
cv::flip(src, src, 0);
vtkImageImport* importer = vtkImageImport::New();
importer->SetOutput(dst);
importer->SetDataSpacing(1, 1, 1);
importer->SetDataOrigin(0, 0, 0);
importer->SetWholeExtent(0, src.size().width - 1, 0, src.size().height - 1, 0, 0);
importer->SetDataExtentToWholeExtent();
importer->SetDataScalarTypeToUnsignedChar();
importer->SetNumberOfScalarComponents(src.channels());
importer->SetImportVoidPointer(src.data);
importer->Update();
return;
}
int main()
{
vtkPNGReader* pngReader = vtkPNGReader::New();
pngReader->SetFileName("E:/picture/lena.png");
Mat img = imread("E:/picture/lena.png");
Mat gray;
cvtColor(img, gray, COLOR_BGR2GRAY);
imshow("1", img);
cvtColor(img, img, COLOR_BGR2RGB);
vtkSmartPointer<vtkImageData>imageData = vtkSmartPointer<vtkImageData>::New();
Ipl2VTK(gray, imageData);
vtkImageViewer* Viewer = vtkImageViewer::New();
Viewer->SetInputData(imageData);
Viewer->SetColorLevel(128);
Viewer->SetColorWindow(256);
Viewer->Render();
waitKey();
return 0;
}
#include <vtkAutoInit.h>
VTK_MODULE_INIT(vtkRenderingOpenGL2);
VTK_MODULE_INIT(vtkInteractionStyle)
#include<vtkSmartPointer.h>
#include<vtkImageCanvasSource2D.h>
#include<vtkImageData.h>
#include<vtkImageActor.h>
#include<vtkRenderer.h>
#include<vtkRenderWindow.h>
#include<vtkRenderWindowInteractor.h>
#include<vtkInteractorStyleImage.h>
#include<vtkPNGReader.h>
#include <vtkImageImport.h>
#include<opencv2/opencv.hpp>
using namespace cv;
void Mat2VTK(cv::Mat& src, vtkImageData* dst)
{
cv::flip(src, src, 0);
vtkImageImport* importer = vtkImageImport::New();
importer->SetOutput(dst);
importer->SetDataSpacing(1, 1, 1);
importer->SetDataOrigin(0, 0, 0);
importer->SetWholeExtent(0, src.size().width-1, 0, src.size().height-1, 0, 0);
importer->SetDataExtentToWholeExtent();
importer->SetDataScalarTypeToUnsignedChar();
importer->SetNumberOfScalarComponents(src.channels());
importer->SetImportVoidPointer(src.data);
importer->Update();
return;
}
int main()
{
Mat img = imread("E:/picture/lena.png");
Mat gray;
cvtColor(img, gray, COLOR_BGR2GRAY);
imshow("1", img);
cvtColor(img, img, COLOR_BGR2RGB);
vtkSmartPointer<vtkImageData>imageData = vtkSmartPointer<vtkImageData>::New();
Mat2VTK(gray, imageData);
vtkSmartPointer<vtkImageActor> actor =
vtkSmartPointer<vtkImageActor>::New();
actor->SetInputData(imageData);
vtkSmartPointer<vtkRenderer> renderer = vtkSmartPointer<vtkRenderer>::New();
renderer->AddActor(actor);
renderer->ResetCamera();
renderer->SetBackground(0.0, 0.0, 0.0);
vtkSmartPointer<vtkRenderWindow> renderwindow =
vtkSmartPointer<vtkRenderWindow>::New();
renderwindow->AddRenderer(renderer);
renderwindow->SetSize(512,512);
renderwindow->SetBorders(1);
renderwindow->SetFullScreen(0);
renderwindow->SetLineSmoothing(0);
renderwindow->Render();
vtkSmartPointer<vtkRenderWindowInteractor> rwi =
vtkSmartPointer<vtkRenderWindowInteractor>::New();
vtkSmartPointer<vtkInteractorStyleImage> style =
vtkSmartPointer<vtkInteractorStyleImage>::New();
rwi->SetInteractorStyle(style);
rwi->SetRenderWindow(renderwindow);
rwi->SetLightFollowCamera(1);
rwi->Initialize();
rwi->Start();
return 0;
}
|