我从另一个stackoverflow question中获取了代码.代码对我来说很好,我已经成功地提取了前景(根据我的要求).现在我想从原始图像中完全删除此前景.我希望它是这样的: –
背景=原始图像 – 前景
空的空间可以填充黑色或白色.我怎样才能做到这一点?
我尝试过使用这种技术: –
Mat background = image2 - foreground;
但它给出了一个完整的黑色图像.
码:-
#include <opencv2/opencv.hpp>#include <iostream>using namespace cv;using namespace std;int main( ){// Open another imageMat image;image= cv::imread("images/abc.jpg");Mat image2 = image.clone();// define bounding rectanglecv::Rect rectangle(40,90,image.cols-80,image.rows-170);cv::Mat result; // segmentation result (4 possible values)cv::Mat bgModel,fgModel; // the models (internally used)// GrabCut segmentationcv::grabCut(image,// input image result,// segmentation result rectangle,// rectangle containing foreground bgModel,fgModel,// models 1,// number of iterations cv::GC_INIT_WITH_RECT); // use rectanglecout << "oks pa dito" <<endl;// Get the pixels marked as likely foregroundcv::compare(result,cv::GC_PR_FGD,result,cv::CMP_EQ);// Generate output imagecv::Mat foreground(image.size(),CV_8UC3,cv::Scalar(255,255,255));//cv::Mat background(image.size(),255));image.copyTo(foreground,result); // bg pixels not copIEd// draw rectangle on original imagecv::rectangle(image,rectangle,255),1);imwrite("img_1.jpg",image);imwrite("Foreground.jpg",foreground);Mat background = image2 - foreground;imwrite("Background.jpg",background);return 0;}
注意:我是一名opencv初学者,现在对它并不了解.如果你能发布完整的代码(根据我的要求)或者只是发布代码行并告诉我这些代码行放在哪里,我将非常感谢你.谢谢.
附:这是我在StackOverflow.com上的第二个问题.道歉……如果不遵守任何惯例.
解决方法 它不是复制所有前景像素,而是复制所有不是前景的像素.您可以使用〜来取消掩码:image.copyTo(background,~result);总结
以上是内存溢出为你收集整理的c – 使用GrabCut提取背景图像全部内容,希望文章能够帮你解决c – 使用GrabCut提取背景图像所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)