opencv实现轮廓高斯滤波平滑

 更新时间:2020年07月21日 14:35:25   作者:BHY_  
这篇文章主要为大家详细介绍了opencv实现轮廓高斯滤波平滑,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了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);//显示图像
}

漫水填充找到的轮廓

轮廓滤波平滑

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

相关文章

  • C语言for循环嵌套for循环在实践题目中应用详解

    C语言for循环嵌套for循环在实践题目中应用详解

    初学C语言,常常遇到for循环中嵌套个for循环,初学者对于这种形式总是一知半解,这次我就整理了常见的for循环嵌套for循环的题目,我们一起争取一举拿下这类题。学废他们,以后再见到就不怕啦!每天都要学一点呀。加油,奋斗的我们
    2022-05-05
  • C++中CopyFile和MoveFile函数使用区别的示例分析

    C++中CopyFile和MoveFile函数使用区别的示例分析

    这篇文章主要介绍了C++中CopyFile和MoveFile函数使用区别的示例分析,CopyFile表示将文件A拷贝到B,如果B已经存在则覆盖,MoveFile表示将文件A移动到。对此感兴趣的可以来了解一下
    2020-07-07
  • 详解C++中StringBuilder类的实现及其性能优化

    详解C++中StringBuilder类的实现及其性能优化

    在Java和C#中,StringBuilder可以创造可变字符序列来动态地扩充字符串,那么在C++中我们同样也可以实现一个StringBuilder并且用来提升性能,下面就来详解C++中StringBuilder类的实现及其性能优化
    2016-05-05
  • C语言判断数是否为素数与素数输出

    C语言判断数是否为素数与素数输出

    大家好,本篇文章主要讲的是C语言判断数是否为素数与素数输出,感兴趣的同学赶快来看一看吧,对你有帮助的话记得收藏一下,方便下次浏览
    2021-12-12
  • 求32位机器上unsigned int的最大值及int的最大值的解决方法

    求32位机器上unsigned int的最大值及int的最大值的解决方法

    本篇文章是对求32位机器上unsigned int的最大值及int的最大值的解决方法进行了详细的分析介绍,需要的朋友参考下
    2013-05-05
  • C语言计算Robots机器人行走路线

    C语言计算Robots机器人行走路线

    这篇文章介绍了C语言计算Robots机器人行走路线,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2021-12-12
  • C语言使用广度优先搜索算法解决迷宫问题(队列)

    C语言使用广度优先搜索算法解决迷宫问题(队列)

    这篇文章主要介绍了C语言使用广度优先搜索算法解决迷宫问题,结合迷宫问题分析了C语言队列广度优先搜索算法的相关使用技巧,需要的朋友可以参考下
    2017-09-09
  • libxml教程(图文详解)

    libxml教程(图文详解)

    本篇文章是对libxm进行了详细的分析介绍,需要的朋友参考下
    2013-05-05
  • C++ Boost Array与Unordered使用介绍

    C++ Boost Array与Unordered使用介绍

    Boost是为C++语言标准库提供扩展的一些C++程序库的总称。Boost库是一个可移植、提供源代码的C++库,作为标准库的后备,是C++标准化进程的开发引擎之一,是为C++语言标准库提供扩展的一些C++程序库的总称
    2022-11-11
  • 从头学习C语言之if语句的使用

    从头学习C语言之if语句的使用

    这篇文章主要为大家详细介绍了C语言之if语句的使用,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下,希望能够给你带来帮助
    2022-01-01

最新评论