使用分水岭在 C++ 中进行 OpenCV 图像分割
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16069184/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
OpenCV image segmentation in C++ using Watershed
提问by Hugh Pearse
Hi i'm currently writing a basic C++ application using the OpenCV library to segment the subject of the image from its background. The application reads in an image file and uses the watershed algorithm to generate a mask based on data it finds around the edges and data it finds in the centre of the image.
嗨,我目前正在使用 OpenCV 库编写一个基本的 C++ 应用程序,以从背景中分割图像的主题。应用程序读入一个图像文件,并使用分水岭算法根据它在边缘周围找到的数据和它在图像中心找到的数据生成一个掩码。
(To start I created an image object that has an overall value of -1. Then I created a border around an empty image which has a value of 1. Then I created a rectangle roughly in the centre of the image which has a value of 2. The border and the rectangle are not touching.)
(首先,我创建了一个整体值为 -1 的图像对象。然后我在一个值为 1 的空图像周围创建了一个边框。然后我在图像的大致中心创建了一个矩形,其值为2. 边框和矩形不接触。)
I attempt to remove data from the image using the generated mask using a bitwise AND between the original image and the auto-generated mask.
我尝试使用原始图像和自动生成的掩码之间的按位 AND 生成的掩码从图像中删除数据。
I have written this in C++ and would be very grateful if someone could take a quick look at my code. The only similar example I could find was using the native OpenCV bindings for Python.
我已经用 C++ 编写了这个,如果有人能快速浏览一下我的代码,我将不胜感激。我能找到的唯一类似的例子是使用 Python 的原生 OpenCV 绑定。
Sample Mask: http://i.imgur.com/a0SUwy3.png
示例蒙版:http: //i.imgur.com/a0SUwy3.png
Sample Image: http://i.imgur.com/FQywu6P.png
示例图片:http: //i.imgur.com/FQywu6P.png
// Usage: ./app input.jpg
#include "opencv2/opencv.hpp"
#include <string>
using namespace cv;
using namespace std;
class WatershedSegmenter{
private:
cv::Mat markers;
public:
void setMarkers(cv::Mat& markerImage)
{
markerImage.convertTo(markers, CV_32S);
}
cv::Mat process(cv::Mat &image)
{
cv::watershed(image, markers);
markers.convertTo(markers,CV_8U);
return markers;
}
};
int main(int argc, char* argv[])
{
cv::Mat image = cv::imread(argv[1]);
cv::Mat blank(image.size(),CV_8U,cv::Scalar(0xFF));
cv::Mat dest(image.size(),CV_8U,cv::Scalar(0xFF));
imshow("originalimage", image);
// Create markers image
cv::Mat markers(image.size(),CV_8U,cv::Scalar(-1));
//Rect(topleftcornerX, topleftcornerY, width, height);
//top rectangle
markers(Rect(0,0,image.cols, 5)) = Scalar::all(1);
//bottom rectangle
markers(Rect(0,image.cols-5,image.cols, 5)) = Scalar::all(1);
//left rectangle
markers(Rect(0,0,5,image.rows)) = Scalar::all(1);
//right rectangle
markers(Rect(image.cols-5,0,5,image.rows)) = Scalar::all(1);
//centre rectangle
markers(Rect(image.cols/2,image.rows/2,50, 50)) = Scalar::all(2);
//Create watershed segmentation object
WatershedSegmenter segmenter;
segmenter.setMarkers(markers);
cv::Mat result = segmenter.process(image);
result.convertTo(result,CV_8U);
bitwise_and(image, blank, dest, result);
imshow("final_result", dest);
cv::waitKey(0);
return 0;
}
回答by Hugh Pearse
Got it working!
搞定了!
// Usage: ./app input.jpg
#include "opencv2/opencv.hpp"
#include <string>
using namespace cv;
using namespace std;
class WatershedSegmenter{
private:
cv::Mat markers;
public:
void setMarkers(cv::Mat& markerImage)
{
markerImage.convertTo(markers, CV_32S);
}
cv::Mat process(cv::Mat &image)
{
cv::watershed(image, markers);
markers.convertTo(markers,CV_8U);
return markers;
}
};
int main(int argc, char* argv[])
{
cv::Mat image = cv::imread(argv[1]);
cv::Mat blank(image.size(),CV_8U,cv::Scalar(0xFF));
cv::Mat dest;
imshow("originalimage", image);
// Create markers image
cv::Mat markers(image.size(),CV_8U,cv::Scalar(-1));
//Rect(topleftcornerX, topleftcornerY, width, height);
//top rectangle
markers(Rect(0,0,image.cols, 5)) = Scalar::all(1);
//bottom rectangle
markers(Rect(0,image.rows-5,image.cols, 5)) = Scalar::all(1);
//left rectangle
markers(Rect(0,0,5,image.rows)) = Scalar::all(1);
//right rectangle
markers(Rect(image.cols-5,0,5,image.rows)) = Scalar::all(1);
//centre rectangle
int centreW = image.cols/4;
int centreH = image.rows/4;
markers(Rect((image.cols/2)-(centreW/2),(image.rows/2)-(centreH/2), centreW, centreH)) = Scalar::all(2);
markers.convertTo(markers,CV_BGR2GRAY);
imshow("markers", markers);
//Create watershed segmentation object
WatershedSegmenter segmenter;
segmenter.setMarkers(markers);
cv::Mat wshedMask = segmenter.process(image);
cv::Mat mask;
convertScaleAbs(wshedMask, mask, 1, 0);
double thresh = threshold(mask, mask, 1, 255, THRESH_BINARY);
bitwise_and(image, image, dest, mask);
dest.convertTo(dest,CV_8U);
imshow("final_result", dest);
cv::waitKey(0);
return 0;
}