OpenCV32---基于距离变换与分水岭的图像分割

三十二、基于距离变换与分水岭的图像分割

1、什么是图像分割(Image Segmentation)

  • 图像分割是图像处理最重要的处理手段之一
  • 图像分割的目标是将图像中像素根据一定的规则分为若干(N)个cluster集合,每个集合包含一类像素
  • 根据算法分为监督学习和无监督学习方法,图像分割的算法多数都是无监督学习方法—Kmeans

OpenCV32---基于距离变换与分水岭的图像分割_第1张图片
2、距离变换与分水岭介绍

  • 距离变换常见算法有两种
    • 不断膨胀/腐蚀得到
    • 基于倒角距离
  • 分水岭常见的算法
    • 基于浸泡理论实现
      OpenCV32---基于距离变换与分水岭的图像分割_第2张图片

3、API

  • 距离变换APIcv::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
)
  • 分水岭APIcv::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;
   	}
    }
 }

OpenCV32---基于距离变换与分水岭的图像分割_第3张图片

  • 使用filter2D与拉普拉斯算子实现图像对比度提高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);

OpenCV32---基于距离变换与分水岭的图像分割_第4张图片

  • 转为二值图像通过threshold
Mat binaryimg;
cvtColor(src, result, COLOR_BGR2GRAY);//转灰度
threshold(result, binaryimg, 40, 255, THRESH_BINARY | THRESH_OTSU);//二值化

OpenCV32---基于距离变换与分水岭的图像分割_第5张图片

  • 距离变换
distanceTransform(binaryimg, dst, DIST_L1, 3, 5);

OpenCV32---基于距离变换与分水岭的图像分割_第6张图片

  • 对距离变换结果进行归一化到0-1之间
normalize(dst, dst, 0, 1, NORM_MINMAX);
  • 使用阈值,再次二值化,得到标记,它的目的是用来区分单独的扑克牌
threshold(dst, dst, 0.4, 1, THRESH_BINARY);

OpenCV32---基于距离变换与分水岭的图像分割_第7张图片

  • 腐蚀得到每个扑克牌的腐蚀图像,它的目的就是使经过二值化仍然连在一起的地方分开
Mat k1 = Mat::ones(13, 13, CV_8UC1);
erode(dst, dst, k1, Point(-1, -1));

OpenCV32---基于距离变换与分水岭的图像分割_第8张图片

  • 发现轮廓—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);

OpenCV32---基于距离变换与分水岭的图像分割_第9张图片

  • 分水岭变换—watershed
watershed(src, markers);
Mat mark = Mat::zeros(markers.size(), CV_8UC1);
markers.convertTo(mark, CV_8UC1);
bitwise_not(mark, mark, Mat());

OpenCV32---基于距离变换与分水岭的图像分割_第10张图片

  • 对每个分割区域着色输出结果
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);
   	}
    }
}

OpenCV32---基于距离变换与分水岭的图像分割_第11张图片
示例代码:(分水岭图像分割)输出结果在各个步骤中展示

#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;
}

你可能感兴趣的:(OpenCV,opencv)