三十二、基于距离变换与分水岭的图像分割
1、什么是图像分割(Image Segmentation)
3、API
cv::distanceTransform
distanceTransform(
InputArray src,//输入图像
OutputArray dst,//输出8位或者32位的浮点数,单一通道,大小与输入图像一致
OutputArray labels,//离散维诺图输出
int distanceType,//distanceType=DIST_L1/DIST_L2
int maskSize, //maskSize=3*3,也支持5*5,推荐3*3
int labelType=DIST_LABEL_CCOMP
)
cv::watershed
watershed(
InputArray image,//输入图像
InputOutputArray markers//既做为输入也做为输出,其为具有一个个小山头的图像
)
4、处理流程
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;
}
}
}
sharp
Mat kernel = (Mat_<float>(3, 3) << 1, 1, 1, 1, -8, 1, 1, 1, 1);
Mat imglaplance;
Mat sharpimg = src;
src.convertTo(sharpimg, CV_32F);
filter2D(src, imglaplance, CV_32F, kernel, Point(-1, -1), 0, BORDER_DEFAULT);//掩膜操作,提升对比度
Mat result = sharpimg - imglaplance;
result.convertTo(result, CV_8UC3);
threshold
Mat binaryimg;
cvtColor(src, result, COLOR_BGR2GRAY);//转灰度
threshold(result, binaryimg, 40, 255, THRESH_BINARY | THRESH_OTSU);//二值化
distanceTransform(binaryimg, dst, DIST_L1, 3, 5);
normalize(dst, dst, 0, 1, NORM_MINMAX);
threshold(dst, dst, 0.4, 1, THRESH_BINARY);
Mat k1 = Mat::ones(13, 13, CV_8UC1);
erode(dst, dst, k1, Point(-1, -1));
findContours
通过发现轮廓可以找到一个个独立的小山头Mat dist_8U;
dst.convertTo(dist_8U, CV_8U);
vector<vector<Point>> contours;
findContours(dist_8U, contours, RETR_EXTERNAL, CHAIN_APPROX_SIMPLE, Point(0, 0));
drawContours
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);//将每一个轮廓画出来,最后一个参数为-1,代表填充轮廓
}
circle(markers, Point(5, 5), 3, Scalar(255, 255, 255), -1);
watershed
watershed(src, markers);
Mat mark = Mat::zeros(markers.size(), CV_8UC1);
markers.convertTo(mark, CV_8UC1);
bitwise_not(mark, mark, Mat());
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 fin_result = 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())){
fin_result.at<Vec3b>(row, col) = colors[index - 1];
}
else {
fin_result.at<Vec3b>(row, col) = Vec3b(0, 0, 0);
}
}
}
#include
#include
#include
using namespace cv;
using namespace std;
int main(int argc, char** argv) {
Mat src, dst;
src = imread("添加图片路径");
if (!src.data) {
cout << "could not load image..." << endl;
return -1;
}
imshow("input image", src);
//change background
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;
}
}
}
Mat k2 = getStructuringElement(MORPH_RECT, Size(1, 3), Point(-1, -1));
medianBlur(src, src, 5);
morphologyEx(src, src, MORPH_OPEN, k2);
namedWindow("black background", WINDOW_AUTOSIZE);
imshow("black background", src);
//sharp
Mat kernel = (Mat_<float>(3, 3) << 1, 1, 1, 1, -8, 1, 1, 1, 1);
Mat imglaplance;
Mat sharpimg = src;
src.convertTo(sharpimg, CV_32F);
filter2D(src, imglaplance, CV_32F, kernel, Point(-1, -1), 0, BORDER_DEFAULT);//掩膜操作,提升对比度
Mat result = sharpimg - imglaplance;
result.convertTo(result, CV_8UC3);
imglaplance.convertTo(imglaplance, CV_8UC3);
imshow("sharpen image", result);
src = result;
//convert to binary
Mat binaryimg;
cvtColor(src, result, COLOR_BGR2GRAY);
threshold(result, binaryimg, 40, 255, THRESH_BINARY | THRESH_OTSU);//二值化
imshow("binary image", binaryimg);
distanceTransform(binaryimg, dst, DIST_L1, 3, 5);//距离变换
normalize(dst, dst, 0, 1, NORM_MINMAX);//归一化
imshow("distance image", dst);
//binary again
threshold(dst, dst, 0.4, 1, THRESH_BINARY);//二值化
imshow("dst", dst);
//erode the distance image
Mat k1 = Mat::ones(13, 13, CV_8UC1);
erode(dst, dst, k1, Point(-1, -1));
imshow("distance binary image", dst);
//markers
Mat dist_8U;
dst.convertTo(dist_8U, CV_8U);
vector<vector<Point>> contours;
findContours(dist_8U, contours, RETR_EXTERNAL, CHAIN_APPROX_SIMPLE, Point(0, 0));
//create markers
Mat markers = Mat::zeros(src.size(), CV_32SC1);
//draw markers
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("my markers", markers * 1000);//markers的灰度级别很低,需要*1000
//perform watershed
watershed(src, markers);
Mat mark = Mat::zeros(markers.size(), CV_8UC1);
markers.convertTo(mark, CV_8UC1);
bitwise_not(mark, mark, Mat());
imshow("water image", mark);
//generate random color
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));
}
//fill with color and display final result
Mat fin_result = 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())){
fin_result.at<Vec3b>(row, col) = colors[index - 1];
}
else {
fin_result.at<Vec3b>(row, col) = Vec3b(0, 0, 0);
}
}
}
imshow("fin_result", fin_result);
waitKey(0);
return 0;
}