opencv学习之路(30)、分水岭算法及图像修补

时间:2022-07-04 04:44:28

一、简介

opencv学习之路(30)、分水岭算法及图像修补

opencv学习之路(30)、分水岭算法及图像修补

二、分水岭算法

opencv学习之路(30)、分水岭算法及图像修补

 #include "opencv2/opencv.hpp"
using namespace cv; void main()
{
Mat srcImg = imread("E://bird.jpg");
imshow("src", srcImg);
Mat dstImg = srcImg.clone();
//medianBlur(srcImg, srcImg, 5);
//GaussianBlur(srcImg, srcImg, Size(5, 5), 0, 0);
Mat imgMask(srcImg.size(), CV_8U, Scalar());//标记图像
//标示背景图像
rectangle(imgMask, Point(, ), Point(srcImg.cols - , srcImg.rows - ), Scalar(), );
//标示鸟
rectangle(imgMask, Point(srcImg.cols / - , srcImg.rows / - ), Point(srcImg.cols / + , srcImg.rows / + ), Scalar(), );
//标示岩石
rectangle(imgMask, Point(, ), Point(, ), Scalar(), );
imshow("mask", imgMask); imgMask.convertTo(imgMask, CV_32S);
watershed(srcImg, imgMask); //调用分水岭算法
Mat mark1, mark2;
imgMask.convertTo(mark1, CV_8U);
imshow("mark1", mark1); Mat mark3 = mark1.clone();
bitwise_not(mark1, mark2);//图像取反
imshow("mark2", mark2);
threshold(mark1, mark1, , , CV_THRESH_TOZERO_INV);//CV_THRESH_TOZERO_INV:大于阈值都为0,其余正常 找出岩石
threshold(mark2, mark2, , , CV_THRESH_TOZERO_INV);//因为取反后背景为0,所以筛选掉岩石后剩下的即为鸟
threshold(mark3, mark3, , , CV_THRESH_BINARY);//鸟为128,大于128才能为255,所以找出了背景 vector<vector<Point>> contours;
vector<Vec4i> hierarcy;
findContours(mark1, contours, hierarcy, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_NONE);
drawContours(dstImg, contours, -, Scalar(, , ), -, ); vector<vector<Point>> contours2;
vector<Vec4i> hierarcy2;
findContours(mark2, contours2, hierarcy2, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_NONE);
drawContours(dstImg, contours2, -, Scalar(, , ), -, ); vector<vector<Point>> contours3;
vector<Vec4i> hierarcy3;
findContours(mark3, contours3, hierarcy3, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_NONE);
drawContours(dstImg, contours3, -, Scalar(, , ), -, ); Mat result = srcImg*0.5 + dstImg*0.5;
imshow("result", result);
waitKey();
}

opencv学习之路(30)、分水岭算法及图像修补

三、图像修补

opencv学习之路(30)、分水岭算法及图像修补

#include "opencv2/opencv.hpp"
using namespace cv; void main()
{
Mat srcImg = imread("E://snow.jpg"); Mat mask(srcImg.size(), CV_8U, Scalar());
rectangle(mask, Point(, ), Point(, srcImg.rows), Scalar(), -, ); Point org = Point(, );
putText(srcImg, "OpenCV", org, CV_FONT_HERSHEY_SIMPLEX, 2.2f, CV_RGB(, , ), );
imshow("src", srcImg); //putText( mask, "OpenCV", org, CV_FONT_HERSHEY_SIMPLEX, 2.2f, CV_RGB(255,255,255),2);
rectangle(mask, Point(, ), Point(srcImg.cols, srcImg.rows), Scalar(), -, );
imshow("mask", mask); Mat result;
inpaint(srcImg, mask, result, , CV_INPAINT_NS); //图像修复 imshow("result", result);
waitKey();
}

opencv学习之路(30)、分水岭算法及图像修补

四、祛痘

 #include "opencv2/opencv.hpp"
using namespace cv; #define WINDOW_NAME0 "【原始图参考】"
#define WINDOW_NAME1 "【原始图】"
#define WINDOW_NAME2 "【修补后的效果图】" Mat srcImage0, srcImage1, inpaintMask;
Point previousPoint(-, -);//原来的点坐标 static void ShowHelpText()
{
printf("\n\n\t\t\t 当前使用的OpenCV版本为:" CV_VERSION);
printf("\n\n ----------------------------------------------------------------------------\n"); printf("\n\t请在进行图像修复操作之前,在【原始图】窗口中进行适量的绘制"
"\n\n\t按键操作说明: \n\n"
"\t\t【鼠标左键】-在图像上绘制白色线条\n\n"
"\t\t键盘按键【ESC】- 退出程序\n\n"
"\t\t键盘按键【2】- 恢复原始图像\n\n"
"\t\t键盘按键【1】或【SPACE】-进行图像修复操作 \n\n");
} //-----------------------------------【On_Mouse( )函数】--------------------------------
// 描述:响应鼠标消息的回调函数
//----------------------------------------------------------------------------------------------
static void On_Mouse(int event, int x, int y, int flags, void*)
{
//鼠标左键弹起消息
if (event == CV_EVENT_LBUTTONUP || !(flags & CV_EVENT_FLAG_LBUTTON))//提取flags的CV_EVENT_FLAG_LBUTTON的标志位,!()的意思是标志位无效,即鼠标左键不是拖拽动作
previousPoint = Point(-, -);
//鼠标左键按下消息
else if (event == CV_EVENT_LBUTTONDOWN)
previousPoint = Point(x, y);
//鼠标按下并移动,进行绘制
else if (event == CV_EVENT_MOUSEMOVE && (flags & CV_EVENT_FLAG_LBUTTON))
{
Point pt(x, y);
if (previousPoint.x < )
previousPoint = pt;
//绘制白色线条
line(inpaintMask, previousPoint, pt, Scalar::all(), , , );
line(srcImage1, previousPoint, pt, Scalar::all(), , , );
previousPoint = pt;
imshow(WINDOW_NAME1, srcImage1);
}
} void main()
{
system("color 2F");
ShowHelpText(); //载入原始图并进行掩膜的初始化
Mat srcImage = imread("face.jpg", -);
if (!srcImage.data) { printf("读取图片错误,请确定目录下是否有imread函数指定图片存在~! \n"); return; }
srcImage0 = srcImage.clone();
srcImage1 = srcImage.clone();
inpaintMask = Mat::zeros(srcImage1.size(), CV_8U); imshow(WINDOW_NAME0, srcImage0);
imshow(WINDOW_NAME1, srcImage1);
//设置鼠标回调消息
setMouseCallback(WINDOW_NAME1, On_Mouse, ); //轮询按键,根据不同的按键进行处理
while ()
{
//获取按键键值
char c = (char)waitKey();
//键值为ESC,程序退出
if (c == )
break;
//键值为2,恢复成原始图像
if (c == '')
{
inpaintMask = Scalar::all();
srcImage.copyTo(srcImage1);
imshow(WINDOW_NAME1, srcImage1);
}
//键值为1或者空格,进行图像修补操作
if (c == '' || c == ' ')
{
Mat inpaintedImage;
inpaint(srcImage1, inpaintMask, inpaintedImage, , CV_INPAINT_TELEA);
imshow(WINDOW_NAME2, inpaintedImage);
}
}
}

opencv学习之路(30)、分水岭算法及图像修补

opencv学习之路(30)、分水岭算法及图像修补