增加了标记,分水岭变换与着色,着色中的轮廓填充判定条件可以仔细看一下,下面是分水岭分割完整代码
#include"pch.h" #include<iostream> #include<opencv2/opencv.hpp> #include<math.h> using namespace std; using namespace cv; int main(int argc, char** argv) { Mat src,gray; src = imread("water.jpg"); imshow("input img", src); //cvtColor(src, gray, COLOR_BGR2GRAY); //反转背景 for(int row=0;row<src.rows;++row) for (int col = 0; col < src.cols; ++col) { if (src.at<Vec3b>(row, col) == Vec3b(255, 255, 255)) { src.at<Vec3b>(row, col)[0] = 0; src.at<Vec3b>(row, col)[1] = 0; src.at<Vec3b>(row, col)[2] = 0; } } imshow("black background", src); //锐化 Mat imgLaplance; Mat sharp = src; src.convertTo(sharp, CV_32F); Mat kernel = (Mat_<float>(3, 3) << 1, 1, 1, 1, -8, 1, 1, 1, 1); filter2D(src, imgLaplance, CV_32F, kernel, Point(-1, -1), 0, BORDER_DEFAULT); Mat resultImg = sharp - imgLaplance; resultImg.convertTo(resultImg, CV_8UC3); imgLaplance.convertTo(imgLaplance, CV_8UC3); imshow("sharp", resultImg); //二值距离变换 Mat binaryImg; cvtColor(resultImg, resultImg, COLOR_BGR2GRAY); threshold(resultImg, binaryImg, 40, 255, THRESH_BINARY | THRESH_OTSU); imshow("binary", binaryImg); //距离变换 Mat dstImg; distanceTransform(binaryImg, dstImg, DIST_L1, 3, 5); normalize(dstImg, dstImg, 0, 1, NORM_MINMAX); imshow("dist img", dstImg); threshold(dstImg, dstImg, 0.4, 1, THRESH_BINARY); imshow("threshold", dstImg); //二值腐蚀 Mat Kernel1 = Mat::ones(13,13, CV_8UC1); erode(dstImg, dstImg, Kernel1, Point(-1, -1)); imshow("erode", dstImg); //标记 Mat dist_8u; dstImg.convertTo(dist_8u, CV_8U); vector<vector<Point>> contours; findContours(dist_8u, contours, RETR_EXTERNAL, CHAIN_APPROX_SIMPLE, Point(0, 0)); Mat markers = Mat::zeros(src.size(), CV_32SC1); for(size_t i=0;i<contours.size();++i) { drawContours(markers, contours, static_cast<int>(i), Scalar::all(static_cast<int>(i) + 1),-1); } circle(markers, Point(5, 5), 3, Scalar(255, 255, 255), -1); imshow("markers", markers * 1000); //分水岭变换 watershed(src, markers); Mat mark = Mat::zeros(markers.size(), CV_8UC1); markers.convertTo(mark, CV_8UC1); bitwise_not(mark, mark, Mat()); imshow("watershold", mark); //着色 vector<Vec3b> colors; for (size_t i = 0; i < contours.size(); ++i) { int r = theRNG().uniform(0, 255); int g = theRNG().uniform(0, 255); int b = theRNG().uniform(0, 255); colors.push_back(Vec3b((uchar)b, (uchar)g, (uchar)r)); } Mat dst = Mat::zeros(markers.size(), CV_8UC3); for(int row=0;row<markers.rows;++row) for (int col = 0; col < markers.cols; ++col) { int index = markers.at<int>(row, col); if (index > 0 && index <= static_cast<int>(contours.size())) dst.at<Vec3b>(row, col) = colors[index - 1]; else dst.at<Vec3b>(row, col) = Vec3b(0, 0, 0); } imshow("final rslt", dst); waitKey(); return 0; }