diff --git a/modules/viz/include/opencv2/viz/widgets.hpp b/modules/viz/include/opencv2/viz/widgets.hpp index 101d8568d..7b7d09725 100644 --- a/modules/viz/include/opencv2/viz/widgets.hpp +++ b/modules/viz/include/opencv2/viz/widgets.hpp @@ -147,7 +147,7 @@ namespace cv class CV_EXPORTS ImageOverlayWidget : public Widget2D { public: - ImageOverlayWidget(const Mat &image, const Point2i &pos); + ImageOverlayWidget(const Mat &image, const Rect &rect); void setImage(const Mat &image); diff --git a/modules/viz/src/shape_widgets.cpp b/modules/viz/src/shape_widgets.cpp index 3bc94534e..4b999a20b 100644 --- a/modules/viz/src/shape_widgets.cpp +++ b/modules/viz/src/shape_widgets.cpp @@ -622,7 +622,7 @@ struct cv::viz::ImageOverlayWidget::CopyImpl } }; -cv::viz::ImageOverlayWidget::ImageOverlayWidget(const Mat &image, const Point2i &pos) +cv::viz::ImageOverlayWidget::ImageOverlayWidget(const Mat &image, const Rect &rect) { CV_Assert(!image.empty() && image.depth() == CV_8U); @@ -641,14 +641,25 @@ cv::viz::ImageOverlayWidget::ImageOverlayWidget(const Mat &image, const Point2i flipFilter->SetInputConnection(vtk_image->GetProducerPort()); flipFilter->Update(); + // Scale the image based on the Rect + vtkSmartPointer transform = vtkSmartPointer::New(); + transform->Scale(double(image.cols)/rect.width,double(image.rows)/rect.height,1.0); + + vtkSmartPointer image_reslice = vtkSmartPointer::New(); + image_reslice->SetResliceTransform(transform); + image_reslice->SetInputConnection(flipFilter->GetOutputPort()); + image_reslice->SetOutputDimensionality(2); + image_reslice->InterpolateOn(); + image_reslice->AutoCropOutputOn(); + vtkSmartPointer imageMapper = vtkSmartPointer::New(); - imageMapper->SetInputConnection(flipFilter->GetOutputPort()); + imageMapper->SetInputConnection(image_reslice->GetOutputPort()); imageMapper->SetColorWindow(255); // OpenCV color imageMapper->SetColorLevel(127.5); vtkSmartPointer actor = vtkSmartPointer::New(); actor->SetMapper(imageMapper); - actor->SetPosition(pos.x, pos.y); + actor->SetPosition(rect.x, rect.y); WidgetAccessor::setProp(*this, actor); }