【发布时间】:2015-10-14 15:16:08
【问题描述】:
我正在尝试使用 C++ 在 OpenCV 中实现抓取算法 我偶然发现了this site,并找到了一种非常简单的方法。不幸的是,代码似乎不适合我
#include "opencv2/opencv.hpp"
#include <iostream>
using namespace cv;
using namespace std;
int main( )
{
// Open another image
Mat image;
image= cv::imread("images/mango11a.jpg");
// define bounding rectangle
cv::Rect rectangle(50,70,image.cols-150,image.rows-180);
cv::Mat result; // segmentation result (4 possible values)
cv::Mat bgModel,fgModel; // the models (internally used)
// GrabCut segmentation
cv::grabCut(image, // input image
result, // segmentation result
rectangle,// rectangle containing foreground
bgModel,fgModel, // models
1, // number of iterations
cv::GC_INIT_WITH_RECT); // use rectangle
cout << "oks pa dito" <<endl;
// Get the pixels marked as likely foreground
cv::compare(result,cv::GC_PR_FGD,result,cv::CMP_EQ);
// Generate output image
cv::Mat foreground(image.size(),CV_8UC3,cv::Scalar(255,255,255));
image.copyTo(foreground,result); // bg pixels not copied
// draw rectangle on original image
cv::rectangle(image, rectangle, cv::Scalar(255,255,255),1);
cv::namedWindow("Image");
cv::imshow("Image",image);
// display result
cv::namedWindow("Segmented Image");
cv::imshow("Segmented Image",foreground);
waitKey();
return 0;
}
谁能帮我解决这个问题?应该是什么问题 PS:编译时没有打印错误。
【问题讨论】:
-
您的代码对我来说看起来不错。您确定您的图像位于正确的文件夹中吗?您的图像是否大于 150x180 像素?否则,您将在图像区域之外定义一个矩形。
-
是的,我的图像是 930 X 700 并且图像位于正确的文件夹中,因为我已经使用同一个图像很长一段时间了。我尝试在命令提示符下打印,并且在grabcut函数调用之后,cout似乎没有被实现。
标签: opencv