opencv实现轮廓高斯滤波平滑
作者:BHY_ 时间:2023-07-23 02:05:56
本文实例为大家分享了opencv实现轮廓高斯滤波平滑的具体代码,供大家参考,具体内容如下
一个小测试的题目:
在图像上点选,找到与点选处相邻的颜色相近的点集合,对该点集合提取轮廓,对该点集合轮廓平滑处理,显示处理结果。
#include <opencv2/opencv.hpp>
#include <iostream>
using namespace std;
using namespace cv;
//************************************
// Method: findRegion 漫水填充
// FullName: findRegion
// Access: public
// Returns: vector<Point>
// Qualifier:
// Parameter: Mat img
// Parameter: Point pos
// Parameter: int LowDifference
// Parameter: int UpDifference
//************************************
vector<Point> findRegion(Mat img, Point pos, int LowDifference, int UpDifference)
{
Mat image = img.clone();
Mat imgBack = img.clone();
Rect ccomp;
Scalar pixel = image.at<Vec3b>(pos);
Scalar pixel2 = Scalar(255 - pixel[0], 255 - pixel[1], 255 - pixel[2], pixel[3]);
floodFill(image, pos, pixel2, &ccomp, Scalar(LowDifference, LowDifference, LowDifference),
Scalar(UpDifference, UpDifference, UpDifference));
Mat diff;
absdiff(image, imgBack, diff);
//统计所有非零像素
vector<Point> pt;
for (int i = 0; i < diff.rows; i++)
{
for (int j = 0; j < diff.cols; j++)
{
Point newPos(j, i);
Scalar pixel3 = diff.at<Vec3b>(newPos);
if (pixel3[0] != 0 || pixel3[1] != 0 || pixel3[2] != 0)
{
pt.push_back(newPos);
}
}
}
return pt;
}
//************************************
// Method: findPerimeter 从点集合中寻找轮廓点
// FullName: findPerimeter
// Access: public
// Returns: vector<Point>
// Qualifier:
// Parameter: vector<Point> pt
// Parameter: Size size
//************************************
vector<Point> findPerimeter(vector<Point> pt, Size size)
{
Mat imgGray(size, CV_8UC1, Scalar(0));
for (int i = 0; i < pt.size(); i++)
{
imgGray.at<uchar>(pt[i]) = 255;
}
std::vector<std::vector<cv::Point>> contours;
//获取轮廓不包括轮廓内的轮廓
cv::findContours(imgGray.clone(), contours, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_NONE);
return contours[0];
}
//************************************
// Method: displayImage 显示图像
// FullName: displayImage
// Access: public
// Returns: void
// Qualifier:
// Parameter: Mat img
// Parameter: vector<Point> contours 轮廓点
// Parameter: Point pos
//************************************
void displayImage(Mat img, vector<Point> contours, Point pos)
{
Mat imgShow = img.clone();
for (int i = 0; i < contours.size(); i++)
{
imgShow.at<Vec3b>(contours[i])[0] = 0;
imgShow.at<Vec3b>(contours[i])[1] = 0;
imgShow.at<Vec3b>(contours[i])[2] = 0;
}
circle(imgShow, pos, 3, Scalar(0, 0, 0), 1, 8, 0);//画用户选择的点
imshow("img", imgShow);
waitKey(0);
}
//************************************
// Method: findSmoothPeimeter 高斯滤波轮廓点平滑
// FullName: findSmoothPeimeter
// Access: public
// Returns: void
// Qualifier:
// Parameter: Mat img 原图
// Parameter: vector<Point> pt 轮廓点集合
//************************************
void findSmoothPeimeter(Mat img, vector<Point> pt)
{
vector<Point> contours = findPerimeter(pt, img.size());
Mat im;
cvtColor(img, im, CV_BGR2GRAY);
Mat cont = ~im;
Mat original = Mat::zeros(im.rows, im.cols, CV_8UC3);
Mat smoothed = img.clone();
// contour smoothing parameters for gaussian filter
int filterRadius = 10;
int filterSize = 2 * filterRadius + 1;
double sigma = 10;
size_t len = contours.size() + 2 * filterRadius;
size_t idx = (contours.size() - filterRadius);
vector<float> x, y;
for (size_t i = 0; i < len; i++)
{
x.push_back(contours[(idx + i) % contours.size()].x);
y.push_back(contours[(idx + i) % contours.size()].y);
}
// filter 1-D signals
vector<float> xFilt, yFilt;
GaussianBlur(x, xFilt, Size(filterSize, filterSize), sigma, sigma);
GaussianBlur(y, yFilt, Size(filterSize, filterSize), sigma, sigma);
// build smoothed contour
vector<Point> smoothContours;
for (size_t i = filterRadius; i < contours.size() + filterRadius; i++)
{
smoothContours.push_back(Point(xFilt[i], yFilt[i]));
}
Scalar color;
for (int i = 0; i < smoothContours.size(); i++)
{
smoothed.at<Vec3b>(smoothContours[i])[0] = 0;
smoothed.at<Vec3b>(smoothContours[i])[1] = 0;
smoothed.at<Vec3b>(smoothContours[i])[2] = 0;
}
imshow("smoothed", smoothed);
waitKey(10);
}
void main()
{
Mat img = imread("4.jpg", 1);
vector<Point> pt, contours;
Point pos(1438, 590);//先列后行
int para1 = 2;
int para2 = 2;
pt = findRegion(img, pos, para1, para2);
findSmoothPeimeter(img, pt);
contours = findPerimeter(pt, img.size());//轮廓点集合
displayImage(img, contours, pos);//显示图像
}
漫水填充找到的轮廓
轮廓滤波平滑
来源:https://blog.csdn.net/qq_15947787/article/details/78518834
标签:opencv,轮廓,高斯滤波
0
投稿
猜你喜欢
Java实现聊天室界面
2023-12-15 10:43:32
Android 实现滑动的六种方式
2021-12-05 08:27:48
c# RSA非对称加解密及XML&PEM格式互换方案
2022-07-10 12:19:31
MyBatis传入集合 list 数组 map参数的写法
2022-12-02 20:21:45
c# WPF中CheckBox样式的使用总结
2023-07-17 15:44:46
java中如何获取时间戳的方法实例
2021-06-14 15:29:16
聊聊Spring——AOP详解(AOP概览)
2023-11-01 04:44:18
mybatis insert foreach循环插入方式
2023-10-15 22:32:53
Java实战入门之双色球彩票小游戏
2023-05-12 04:07:13
JDK8中的HashMap初始化和扩容机制详解
2023-11-25 10:06:44
在Java的Struts中判断是否调用AJAX及用拦截 器对其优化
2023-11-25 00:40:40
Android RecycleView和线型布局制作聊天布局
2022-08-01 15:36:56
java 2d画图示例分享(用java画图)
2023-07-25 22:03:52
详解Spring-boot中读取config配置文件的两种方式
2021-07-04 15:52:55
C#中通过API实现的打印类 实例代码
2021-10-19 15:55:52
解决response.setHeader设置下载文件名无效的问题
2021-08-15 20:43:54
SpringMVC中@RequestMapping注解用法实例
2022-02-26 11:22:58
基于android中的各种颜色在drawable.xml中的值详解
2022-03-30 01:56:18
C#中AS和IS关键字的用法
2021-06-12 09:44:53
快速定位Java 内存OOM的问题
2022-05-26 00:19:38