#include<string>
#include <opencv2/opencv.hpp>
using namespace cv;
using namespace std;
int main()
{
Mat g_midimage, himg, simg, vimg, tmpimage, binary_image;
Mat g_srcimage = imread("D:\\testImg\\t12.png");
cvtColor(g_srcimage, g_midimage, COLOR_BGR2HSV);
vector<Mat> hsv;
split(g_midimage, hsv);
himg = hsv[0];
simg = hsv[1];
vimg = hsv[2];
for (int i = 0;i < himg.rows;i++)
{
for (int j = 0;j < himg.cols;j++)
{
uchar hvalue = himg.at<uchar>(i, j);
if ((hvalue > 0 && hvalue < 30) || (hvalue > 170 && hvalue < 180))
himg.at<uchar>(i, j) = 255;
else
himg.at<uchar>(i, j) = 0;
}
}
for (int i = 0;i < simg.rows;i++)
{
for (int j = 0;j < simg.cols;j++)
{
uchar svalue = simg.at<uchar>(i, j);
if ((svalue > 0 && svalue < 20) || (svalue > 50 && svalue < 100))
simg.at<uchar>(i, j) = 255;
else
simg.at<uchar>(i, j) = 0;
}
}
for (int i = 0;i < vimg.rows;i++)
{
for (int j = 0;j < vimg.cols;j++)
{
uchar vvalue = vimg.at<uchar>(i, j);
if (vvalue > 170 && vvalue < 220)
vimg.at<uchar>(i, j) = 255;
else
vimg.at<uchar>(i, j) = 0;
}
}
bitwise_and(himg, simg, tmpimage);
bitwise_and(tmpimage, vimg, binary_image);
//imshow("binary", binary_image);
Mat fg;
erode(binary_image, fg, Mat(), Point(-1, -1), 6);
//imshow("foreground", fg);
Mat bg;
dilate(binary_image, bg, Mat(), Point(-1, -1), 6);
threshold(bg, bg, 1, 128, THRESH_BINARY_INV);
//imshow("background", bg);
Mat markers(binary_image.size(), CV_8U, Scalar(0));
markers = fg + bg;
markers.convertTo(markers, CV_32S);
watershed(g_srcimage, markers);
Mat tmp;
markers.convertTo(tmp, CV_8U);
double pixekcount = 0., blackcount = 0.;
for (int i = 0;i < tmp.rows;i++)
{
for (int j = 0;j < tmp.cols;j++)
{
pixekcount++;
uchar gvalue = tmp.at<uchar>(i, j);
if (gvalue == 255)
blackcount++;
}
}
double scale_img;
scale_img = blackcount / pixekcount;
if (scale_img > 0.04)
{
putText(g_srcimage, "have dust!", Point(10, 20), 3, 0.5, Scalar(0, 0, 255));
}
else
putText(g_srcimage, "no dust!", Point(10, 20), 3, 0.5, Scalar(255, 0, 0));
imshow("ori", g_srcimage);
waitKey(0);
return 0;
}
分水岭算法
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- 一. 什么是图像分割? 图像分割的目标是将图像中像素根据一定的规则分为若干个cluster集合,每个集合包含一类像...
- 》》点赞,收藏+关注,理财&技术不迷路《《 分水岭算法对图像分割非常有用。这种方法是一种很完美的分割算法。分水岭是...