一:内容介绍
本节主要介绍OpenCV的imgproc模块的图像轮廓与分割部分:
1. 查找并绘制轮廓
2. 寻找物体的凸包
3. 使用多边形将轮廓包围
4. 图像的矩
5. 分水岭算法
6. 图像修补
二:学习笔记
1. findContours()函数查找图像轮廓和canny检测边缘、hough检测直线,这些都非常使用(参见:OpenCV成长之路(8):直线、轮廓的提取与描述)。但是关于opencv中findContours()的具体原理我也没看,想深入研究的话可以看What is the algorithm that opencv uses for finding contours?
2. 寻找凸包和使用多边形将轮廓包围
3. 图像矩作为图像的一种统计特征,满足平移、伸缩、旋转的不变性(参见:图像的矩特征)。同时,矩本身也有一定的物理含义,特殊地,轮廓的m00矩代表轮廓的面积。
4. 分水岭算法可以将图像的边缘转换为“山脉”,将均匀区域转化为“山谷”,有助于图像分割。例程里边用的还有点复杂,得稍微理解一下。
5. 图像修补,不怎么用感觉。
6. 关于contours官网给的很好的一份资料:Contour Features
6. 本节函数清单
三:相关源码及解析
本章示例较多,示例列表:
1.查找并绘制轮廓
2.寻找和绘制物体的凸包
3.使用多边形将包围轮廓
4.查找并绘制图像轮廓矩
5.分水岭算法
6.图像修补
1. 查找并绘制轮廓
源码:
#include
#include
#include
using namespace std;
using namespace cv;
#define WINDOW_NAME1 "【原始图窗口】"
#define WINDOW_NAME2 "【轮廓图】"
Mat g_srcImage, g_grayImage;
int g_nThresh = 80;
int g_nThresh_max = 255;
RNG g_rng;
Mat g_cannyMat_output;
vector<vector > g_vContours;
vector g_vHierarchy;
void on_ThreshChange(int, void*);
int main()
{
g_srcImage = imread("poster_cola.jpg"); //加载源图像
cvtColor(g_srcImage, g_grayImage, COLOR_BGR2GRAY); //转换灰度图
blur(g_grayImage, g_grayImage, Size(3, 3) ); //降噪
namedWindow(WINDOW_NAME1);
imshow(WINDOW_NAME1, g_srcImage);
createTrackbar("canny阈值", WINDOW_NAME1, &g_nThresh, g_nThresh_max, on_ThreshChange);
on_ThreshChange(0, 0);
while(waitKey(9)!=27);
return 0;
}
void on_ThreshChange(int, void*)
{
Canny(g_grayImage, g_cannyMat_output, g_nThresh, g_nThresh*2); //用canny算子检测边缘
Mat temp = g_cannyMat_output.clone();
findContours(g_cannyMat_output, g_vContours, g_vHierarchy, RETR_TREE, CHAIN_APPROX_SIMPLE);
//此程序中对二值图像寻找轮廓是有点问题的
Mat drawing = Mat::zeros(g_cannyMat_output.size(), CV_8UC3);
for (int i = 0; i < g_vContours.size(); i++)
{
Scalar color = Scalar(g_rng.uniform(0, 255), g_rng.uniform(0, 255), g_rng.uniform(0, 255)); //任意值
drawContours(drawing, g_vContours, i, color);
}
imshow(WINDOW_NAME2, drawing);
}
素材:
效果图:
提示:
这二有一个官方samples里带的寻找轮廓的例子,更容易理解一点:
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/highgui/highgui.hpp"
#include
#include
using namespace cv;
using namespace std;
const int w = 500; //图像的长和宽
int levels = 3;
vector<vector > contours;
vector hierarchy;
static void on_trackbar(int, void*)
{
Mat cnt_img = Mat::zeros(w, w, CV_8UC3);
int _levels = levels - 3;
drawContours(cnt_img, contours, _levels <= 0 ? 3 : -1, Scalar(128, 255, 255), //可尝试更换此处的3试一下
3, LINE_AA, hierarchy, std::abs(_levels));
imshow("contours", cnt_img);
}
int main(int argc, char** argv)
{
Mat img = Mat::zeros(w, w, CV_8UC1);
//Draw 6 faces
for (int i = 0; i < 6; i++)
{
int dx = (i % 2) * 250 - 30;
int dy = (i / 2) * 150;
const Scalar white = Scalar(255);
const Scalar black = Scalar(0);
if (i == 0)
{
for (int j = 0; j <= 10; j++)
{
double angle = (j + 5)*CV_PI / 21;
line(img, Point(cvRound(dx + 100 + j * 10 - 80 * cos(angle)),
cvRound(dy + 100 - 90 * sin(angle))),
Point(cvRound(dx + 100 + j * 10 - 30 * cos(angle)),
cvRound(dy + 100 - 30 * sin(angle))), white, 1, 8, 0);
}
}
ellipse(img, Point(dx + 150, dy + 100), Size(100, 70), 0, 0, 360, white, -1, 8, 0);
ellipse(img, Point(dx + 115, dy + 70), Size(30, 20), 0, 0, 360, black, -1, 8, 0);
ellipse(img, Point(dx + 185, dy + 70), Size(30, 20), 0, 0, 360, black, -1, 8, 0);
ellipse(img, Point(dx + 115, dy + 70), Size(15, 15), 0, 0, 360, white, -1, 8, 0);
ellipse(img, Point(dx + 185, dy + 70), Size(15, 15), 0, 0, 360, white, -1, 8, 0);
ellipse(img, Point(dx + 115, dy + 70), Size(5, 5), 0, 0, 360, black, -1, 8, 0);
ellipse(img, Point(dx + 185, dy + 70), Size(5, 5), 0, 0, 360, black, -1, 8, 0);
ellipse(img, Point(dx + 150, dy + 100), Size(10, 5), 0, 0, 360, black, -1, 8, 0);
ellipse(img, Point(dx + 150, dy + 150), Size(40, 10), 0, 0, 360, black, -1, 8, 0);
ellipse(img, Point(dx + 27, dy + 100), Size(20, 35), 0, 0, 360, white, -1, 8, 0);
ellipse(img, Point(dx + 273, dy + 100), Size(20, 35), 0, 0, 360, white, -1, 8, 0);
}
//show the faces
namedWindow("image", 1);
imshow("image", img);
//Extract the contours so that
vector<vector > contours0;
findContours(img, contours0, hierarchy, RETR_TREE, CHAIN_APPROX_SIMPLE);
contours.resize(contours0.size());
for (size_t k = 0; k < contours0.size(); k++)
approxPolyDP(Mat(contours0[k]), contours[k], 3, true);
namedWindow("contours", 1);
createTrackbar("levels+3", "contours", &levels, 7, on_trackbar);
on_trackbar(0, 0);
waitKey();
return 0;
}
2 . 寻找和绘制物体的凸包
源码:
#include
#include
using namespace cv;
using namespace std;
#define WINDOW_NAME1 "【原始图窗口】"
#define WINDOW_NAME2 "【效果图窗口】"
Mat g_srcImage, g_grayImage;
int g_nThresh = 50;
int g_maxThresh = 255;
RNG g_rng;
Mat srcImage_copy = g_srcImage.clone();
Mat g_thresholdImage_output;
vector<vector > g_vContours;
vector g_vHierarchy;
void on_ThreshChange(int, void*);
int main()
{
g_srcImage = imread("poster_cartoon_1.jpg");
cvtColor(g_srcImage, g_grayImage, COLOR_BGR2GRAY);
blur(g_grayImage, g_grayImage, Size(3, 3));
namedWindow(WINDOW_NAME1);
imshow(WINDOW_NAME1, g_srcImage);
createTrackbar("阈值:", WINDOW_NAME1, &g_nThresh, g_maxThresh, on_ThreshChange);
on_ThreshChange(0, 0); //调用一次进行初始化
while (waitKey(2) != 27);
return 0;
}
void on_ThreshChange(int, void*)
{
threshold(g_grayImage, g_thresholdImage_output, g_nThresh, 255, THRESH_BINARY); //二值化
findContours(g_thresholdImage_output, g_vContours, g_vHierarchy, RETR_TREE, CHAIN_APPROX_SIMPLE);
//遍历每个轮廓,寻找其凸包
vector<vector > hull(g_vContours.size());
for (unsigned int i = 0; i < g_vContours.size(); i++)
{
convexHull(Mat(g_vContours[i]), hull[i]);
}
//绘出轮廓及凸包
Mat drawing = Mat::zeros(g_thresholdImage_output.size(), CV_8UC3);
for (unsigned int i = 0; i < g_vContours.size(); i++)
{
Scalar color = Scalar(g_rng.uniform(0, 255), g_rng.uniform(0, 255), g_rng.uniform(0, 255) );
drawContours(drawing, g_vContours, i, color); //画轮廓
color = Scalar(g_rng.uniform(0, 255), g_rng.uniform(0, 255), g_rng.uniform(0, 255));
drawContours(drawing, hull, i, color); //画凸包图
}
imshow(WINDOW_NAME2, drawing);
}
素材:
效果图:
提示:
无
3 . 使用多边形将包围轮廓
源码:
#include
#include
using namespace cv;
using namespace std;
#define WINDOW_NAME1 "【原始图窗口】"
#define WINDOW_NAME2 "【效果图窗口】"
Mat g_srcImage;
Mat g_grayImage;
int g_nThresh = 50; //阈值
int g_nMaxThresh = 255; //最大阈值
RNG g_rng;
void on_ContoursChange(int, void*);
int main()
{
g_srcImage = imread("poster_landscape_4.jpg");
cvtColor(g_srcImage, g_grayImage, COLOR_BGR2GRAY); //转化为灰度图
blur(g_grayImage, g_grayImage, Size(3, 3)); //平滑处理
namedWindow(WINDOW_NAME1);
imshow(WINDOW_NAME1, g_srcImage);
createTrackbar("阈值:", WINDOW_NAME1, &g_nThresh, g_nMaxThresh, on_ContoursChange);
on_ContoursChange(0, 0);
while (waitKey(3) != 27);
return 0;
}
void on_ContoursChange(int, void*)
{
Mat threshold_output;
vector<vector > contours;
vector hierarchy;
threshold(g_grayImage, threshold_output, g_nThresh, 255, THRESH_BINARY); //Threshold检测边缘
findContours(threshold_output, contours, hierarchy, RETR_TREE, CHAIN_APPROX_SIMPLE);
//多边形逼近轮廓+获取矩形和圆形边界框
vector<vector > contours_poly(contours.size());
vector boundRect(contours.size());
vector center(contours.size());
vector<float> radius(contours.size());
// Mat tmp(contours[3]);
//一个循环,遍历所有部分,进行本程序最核心的操作
for (unsigned int i = 0; i < contours.size(); i++)
{
approxPolyDP(Mat(contours[i]), contours_poly[i], 3, true); //指定精度逼近多边形曲线
boundRect[i] = boundingRect(Mat(contours_poly[i])); //计算点集的最外面矩形边框
minEnclosingCircle(contours_poly[i], center[i], radius[i]); //对给定的2D点集,寻找最小面积的包围圆形
}
//绘制多边形轮廓+包围的矩形框+圆形框
Mat drawing = Mat::zeros(threshold_output.size(), CV_8UC3);
for (unsigned int i = 0; i < contours.size(); i++)
{
Scalar color = Scalar(g_rng.uniform(0, 255), g_rng.uniform(0, 255), g_rng.uniform(0, 255)); //随机设置颜色
drawContours(drawing, contours_poly, i, color);
rectangle(drawing, boundRect[i].tl(), boundRect[i].br(), color);
circle(drawing, center[i], (int)radius[i], color);
}
namedWindow(WINDOW_NAME2);
imshow(WINDOW_NAME2, drawing);
}
素材:
效果图:
提示:
无
4 . 查找并绘制图像轮廓矩
源码:
#include
#include
using namespace cv;
using namespace std;
#define WINDOW_NAME1 "【原始图】"
#define WINDOW_NAME2 "【图像轮廓】"
Mat g_srcImage;
Mat g_grayImage;
int g_nThresh = 100;
int g_nMaxThresh = 255;
RNG g_rng;
Mat g_cannyMat_output;
vector<vector > g_vContours;
vector g_vHierarchy;
void on_ThreshChange(int, void*);
int main()
{
g_srcImage = imread("poster_landscape_5.jpg");
cvtColor(g_srcImage, g_grayImage, COLOR_BGR2GRAY);
blur(g_grayImage, g_grayImage, Size(3,3));
namedWindow(WINDOW_NAME1);
imshow(WINDOW_NAME1, g_srcImage);
createTrackbar("阈值", WINDOW_NAME1, &g_nThresh, g_nMaxThresh, on_ThreshChange);
on_ThreshChange(0, 0);
while (waitKey(5)!=27);
return 0;
}
void on_ThreshChange(int, void*)
{
Canny(g_grayImage, g_cannyMat_output, g_nThresh, g_nThresh*2); //使用canny检测边缘
findContours(g_cannyMat_output, g_vContours, g_vHierarchy, RETR_TREE, CHAIN_APPROX_SIMPLE); //找到轮廓
vector mu(g_vContours.size()); //计算矩
for (unsigned int i = 0; i < g_vContours.size(); i++)
mu[i] = moments(g_vContours[i], false);
vector mc(g_vContours.size()); //计算中心矩
for (unsigned int i = 0; i < g_vContours.size(); i++)
mc[i] = Point2f(static_cast<float>(mu[i].m10/mu[i].m00), static_cast<float>(mu[i].m01 / mu[i].m00));
cout << "输出内容:面积和轮廓长度" << endl;
Mat drawing = Mat::zeros(g_cannyMat_output.size(), CV_8UC3); //绘制轮廓
for (unsigned int i = 0; i < g_vContours.size(); i++) //通过m00计算轮廓面积并且和OpenCV函数比较
{
cout << "通过m00计算出轮廓" << i << "的面积,(M_00)=" << mu[i].m00 << endl
<< " OpenCV函数计算出的面积=" << contourArea(g_vContours[i]) << ", 长度: " << arcLength(g_vContours[i], true) << endl << endl;
Scalar color = Scalar(g_rng.uniform(0, 255), g_rng.uniform(0, 255), g_rng.uniform(0, 255));
drawContours(drawing, g_vContours, i, color, 2, 8, g_vHierarchy, 0, Point()); //绘制外层和内层轮廓
circle(drawing, mc[i], 4, color, -1);
}
namedWindow(WINDOW_NAME2); //显示到窗口
imshow(WINDOW_NAME2, drawing);
}
#include
#include
using namespace std;
using namespace cv;
#define WINDOW_NAME "【程序窗口1】"
Mat g_maskImage, g_srcImage;
Point prevPt(-1, -1);
static void on_Mouse(int event, int x, int y, int flags, void*);
int main()
{
//载入原图,初始化掩膜和灰度图
g_srcImage = imread("poster_landscape_6.jpg");
imshow(WINDOW_NAME, g_srcImage);
Mat srcImage, grayImage;
g_srcImage.copyTo(srcImage);
cvtColor(g_srcImage, g_maskImage, COLOR_BGR2GRAY);
cvtColor(g_maskImage, grayImage, COLOR_GRAY2BGR);
g_maskImage = Scalar::all(0);
//设置鼠标回调函数
setMouseCallback(WINDOW_NAME, on_Mouse);
//轮询按键
while (1)
{
int c = waitKey(0);
if ((char)c == 27) break;
if ((char)c == '2') { //按键‘2’, 恢复源图
g_maskImage = Scalar::all(0);
srcImage.copyTo(g_srcImage);
imshow("image", g_srcImage);
}
if ((char)c=='1' || (char)c==' ' ) {
//定义一些参数
vector<vector > contours;
vector hierarchy;
//寻找轮廓
findContours(g_maskImage, contours, hierarchy, CV_RETR_CCOMP, CV_CHAIN_APPROX_SIMPLE);
//轮廓为空时的处理
if (contours.empty()) continue;
//复制掩膜
Mat maskImage(g_maskImage.size(), CV_32S);
maskImage = Scalar::all(0);
//循环绘制出轮廓
int compCount = 0;
for (int index = 0; index >= 0; index = hierarchy[index][0], compCount++)
drawContours(maskImage, contours, index, Scalar::all(compCount+1), -1, LINE_8, hierarchy);
//compCount为零时的处理
if (compCount == 0)
continue;
//生成随机颜色
vector colorTab;
for (unsigned int i = 0; i < compCount; i++) {
int b = theRNG().uniform(0, 255);
int g = theRNG().uniform(0, 255);
int r = theRNG().uniform(0, 255);
colorTab.push_back(Vec3b((uchar)b, (uchar)g, (uchar)r));
}
//计算处理时间并输出到窗口中
double dTime = (double)getTickCount();
watershed(srcImage, maskImage);
dTime = (double)getTickCount() - dTime;
printf("处理时间=%gms\n", dTime*1000./getTickFrequency());
//双层循环,将分水岭图像遍历存入watershedImage中
Mat watershedImage(maskImage.size(), CV_8UC3);
for (unsigned int i = 0; i < maskImage.rows; i++)
for (unsigned int j = 0; j < maskImage.cols; j++)
{
int index = maskImage.at<int>(i, j);
if (index == -1)
watershedImage.at(i, j) = Vec3b(255, 255, 255);
else if (index <= 0 || index > compCount)
watershedImage.at(i, j) = Vec3b(0, 0, 0);
else
watershedImage.at(i, j) = colorTab[index - 1];
}
//混合灰度图和分水岭效果图并显示最终的窗口
watershedImage = watershedImage*0.5 + grayImage*0.5;
imshow("watershed transform", watershedImage);
}
}
return 0;
}
static void on_Mouse(int event, int x, int y, int flags, void*)
{
//处理鼠标不在窗口中的情况
if (x < 0 || x >= g_srcImage.cols || y < 0 || y >= g_srcImage.rows) return;
//处理鼠标左键相关消息
if (event == EVENT_LBUTTONUP || !(flags & EVENT_FLAG_LBUTTON)) //左键抬起动作或处于没有按下状态
prevPt = Point(-1, -1);
else if (event == EVENT_LBUTTONDOWN) //左键按下动作
prevPt = Point(x, y);
//鼠标左键按下并移动,绘制出白色线条
else if (event == EVENT_MOUSEMOVE && (flags & EVENT_FLAG_LBUTTON))
{
Point pt(x, y);
if (prevPt.x < 0) prevPt = pt;
line(g_maskImage, prevPt, pt, Scalar::all(255), 5);
line(g_srcImage, prevPt, pt, Scalar::all(255), 5);
prevPt = pt;
imshow(WINDOW_NAME, g_srcImage);
}
}
#include
#include
using namespace cv;
using namespace std;
#define WINDOW_NAME1 "【原始图】"
#define WINDOW_NAME2 "【修补后的效果图】"
Mat srcImage1, inpaintMask;
Point previousPoint(-1, -1); //原来的点坐标
static void on_Mouse(int event, int x, int y, int flags, void*)
{
if (event == EVENT_LBUTTONUP || !(flags & EVENT_FLAG_LBUTTON)) //鼠标左键抬起或没有按键按下
previousPoint = Point(-1, -1);
else if (event == EVENT_LBUTTONDOWN) //鼠标左键按下消息
previousPoint = Point(x, y);
else if (event==EVENT_MOUSEMOVE && (flags & EVENT_FLAG_LBUTTON)) //鼠标左键按下并移动,进行绘制
{
Point pt(x, y);
if (previousPoint.x < 0) previousPoint = pt;
//绘制白色线条
line(inpaintMask, previousPoint, pt, Scalar::all(255), 5);
line(srcImage1, previousPoint, pt, Scalar::all(255), 5);
previousPoint = pt;
imshow(WINDOW_NAME1, srcImage1);
}
}
int main()
{
Mat srcImage = imread("poster_landscape_7.jpg");
srcImage1 = srcImage.clone();
inpaintMask = Mat::zeros(srcImage1.size(), CV_8U);
imshow(WINDOW_NAME1, srcImage1); //显示原始图
cvSetMouseCallback(WINDOW_NAME1, on_Mouse); //设置鼠标回调消息
while (1) //轮询按键,根据不同的按键进行处理
{
char c = (char)waitKey();
if (c == 27) break;
if (c == '2') { //键值为2, 恢复成原始图像
inpaintMask = Scalar::all(0);
srcImage.copyTo(srcImage1);
imshow(WINDOW_NAME1, srcImage1);
}
if (c=='1' || c==' ') //键值为1或者空格,进行图像修补操作
{
Mat inpaintedImage;
inpaint(srcImage1, inpaintMask, inpaintedImage, 3, INPAINT_TELEA);
imshow(WINDOW_NAME2, inpaintedImage);
}
}
return 0;
}