void QuickDemo::Image_segmentation_Demo(Mat& image) {
for (int row = 0; row < image.rows; row++) {
for (int col = 0; col < image.cols; col++) {
if (image.at<Vec3b>(row, col) == Vec3b(255, 255, 255)) {
image.at<Vec3b>(row, col)[0] = 0;
image.at<Vec3b>(row, col)[1] = 0;
image.at<Vec3b>(row, col)[2] = 0;
}
}
}
imshow("image", image);
// 使用掩码的方式替换颜色:python:a = np.where(a=255,0)
//Mat mask_;
//inRange(image, Scalar(255, 255, 255), Scalar(255, 255, 255), mask);
//image.setTo(Scalar(0, 0, 0), mask_);
Mat kernel_ = (Mat_<float>(3, 3) << 1, 1, 1, 1, -8, 1, 1, 1, 1);
Mat ImgLaplance;
Mat sharp = image;
filter2D(sharp, ImgLaplance, CV_32F, kernel_, Point(-1, -1), 0);
cout << "&sharp" << &sharp << endl;
cout << "&image" << &image << endl;
image.convertTo(sharp, CV_32F);
Mat ImgResult = sharp - ImgLaplance;
ImgResult.convertTo(ImgResult, CV_8UC3);
ImgLaplance.convertTo(ImgLaplance, CV_8UC3);
imshow("ImgResult", ImgResult);
imshow("ImgLaplance", ImgLaplance);
Mat binImg;
image = ImgResult; // 指向锐化后的图像
cvtColor(image, binImg, COLOR_BGR2GRAY);
threshold(binImg, binImg, 40, 255, THRESH_BINARY | THRESH_OTSU);
imshow("binImg", binImg);
Mat distImg;
distanceTransform(binImg, distImg, DIST_L2, 5);
normalize(distImg, distImg, 0, 1, NORM_MINMAX);
imshow("normalize", distImg);
threshold(distImg, distImg, 0.4, 1, THRESH_BINARY);
imshow("distance_threshold", distImg);
// 查看二值化后的效果,觉得没必要做这一步了
Mat kernel2_ = Mat::ones(5, 5, CV_8UC1);
erode(distImg, distImg, kernel2_);
imshow("peaks", distImg);
// 轮廓查找,并绘制轮廓
Mat dist_u8;
distImg.convertTo(dist_u8, CV_8U);
vector<vector<Point>> contours;
findContours(dist_u8, contours, RETR_EXTERNAL, CHAIN_APPROX_SIMPLE);
if (contours.size() == 0) {
cout << "未找到轮廓" << endl;
return;
}
Mat marks = Mat::zeros(distImg.size(), CV_32SC1);
for (size_t i = 0; i < contours.size(); i++) {
drawContours(marks, contours, static_cast<int>(i), Scalar::all(static_cast<uchar>(i + 1)), -1);
}
circle(marks, Point(5, 5), 3, Scalar(255), -1);
//imshow("mark_dist", marks * 1000); //报错
watershed(image, marks);
Mat mark_dist = Mat::zeros(marks.size(), CV_8UC1);
marks.convertTo(mark_dist, CV_8UC1);
bitwise_not(mark_dist, mark_dist);
imshow("watershed", mark_dist);
// generator random color
RNG rng(12335);
vector<Vec3b> colors;
for (size_t i = 0; i < contours.size(); i++){
uchar b = rng.uniform(0, 255);
uchar g = rng.uniform(0, 255);
uchar r = rng.uniform(0, 255);
colors.push_back(Vec3b(b, g, r));
}
//着色
Mat drawImg = Mat::zeros(marks.size(), CV_8UC3);
for (int row = 0; row < marks.rows; row++) {
for (int col = 0; col < marks.cols; col++) {
uchar index = marks.at<int>(row, col);
if (index > 0 && index <= static_cast<int>(contours.size())) {
drawImg.at<Vec3b>(row, col) = colors[index - 1];
}
else {
drawImg.at<Vec3b>(row, col) = Vec3b(0, 0, 0);
}
}
}
imshow("dst", drawImg);
}