在C ++中按四点矢量裁剪后在PictureBox中显示Mat

时间:2018-09-18 08:04:39

标签: c++ opencv windows-forms-designer

我目前正在做一个项目,将所选区域拖放到图像中。

我使用OpenCV获取Mat区域并将其显示在Windows窗体C ++的PictureBox中

这是我的代码正确无误,并在imshow中显示确定。但是,当我转换为位图并插入PictureBox时,它显示错误。有时候还可以,我不知道为什么。

std::vector<cv::Point> imagePoints; // This have 4 selected point
Rect r = boundingRect(imagePoints);
Mat imgRoi = cam_view(r);

cv::Point2f src_vertices[4];
src_vertices[0] = imagePoints[0];
src_vertices[1] = imagePoints[1];
src_vertices[2] = imagePoints[2];
src_vertices[3] = imagePoints[3];

cv::Point2f dst_vertices[4];
dst_vertices[0] = cv::Point(0, 0);
dst_vertices[1] = cv::Point(imgRoi.cols - 1, 0);
dst_vertices[2] = cv::Point(imgRoi.cols - 1, imgRoi.rows - 1);
dst_vertices[3] = cv::Point(0, imgRoi.rows - 1);


Mat warpMatrix = getPerspectiveTransform(src_vertices, dst_vertices);
cv::Size size(imgRoi.cols, imgRoi.rows);
warpPerspective(cam_view, mat_cropped, warpMatrix, size, INTER_CUBIC, BORDER_CONSTANT);

**//This imshow show correctly image that cropped**
imshow("mat_cropped", mat_cropped);

Mat camview_crop = mat_cropped.clone();
System::IntPtr ptr(camview_crop.ptr());

//This step cause error
Bitmap^ bmpSawMarkRGB = gcnew Bitmap(camview_crop.cols, camview_crop.rows, camview_crop.step, System::Drawing::Imaging::PixelFormat::Format24bppRgb, ptr);
ptbAreaView->Image = bmpSawMarkRGB;
ptbAreaView->SizeMode = PictureBoxSizeMode::Normal;
ptbAreaView->Refresh();
delete bmpSawMarkRGB;

运行后显示错误

************** 例外テキスト **************
System.ArgumentException:Parameter is not valid。
   場所 System.Drawing.Bitmap..ctor(Int32 width, Int32 height, Int32 stride, PixelFormat format, IntPtr scan0) 

感谢您的帮助。

0 个答案:

没有答案