2020-12-28 learning opencv3: 十:Smoothing
12 2020 learning 28 OPENCV3
2023-09-27 14:25:50 时间
2020-11-30 03_空域图像处理 笔记
Smoothing, also called blurring as depicted in Figure 10-6, is a simple and frequently
used image-processing operation. There are many reasons for smoothing, but it is
often done to reduce noise or camera artifacts.
Simple Blur and the Box Filter
The simple blur operation is provided by cv::blur(). Each pixel in the output is the simple mean of all of the pixels in a window (i.e., the kernel), around the correspond‐ing pixel in the input.
#include <opencv2/opencv.hpp>
using namespace cv;
int blurType = 0;
Mat src;
void copyPixel(const Mat& src, Mat& dst)
{
for (size_t i = 0; i < src.rows; i++)
{
for (size_t j = 0; j < src.cols; j++)
{
dst.at<Vec3b>(i, j)[0] = src.at<Vec3b>(i, j)[0]; //蓝色通道
dst.at<Vec3b>(i, j)[1] = src.at<Vec3b>(i, j)[1]; //绿色通道
dst.at<Vec3b>(i, j)[2] = src.at<Vec3b>(i, j)[2]; //红是通道
}
}
}
void blurTest(Mat& src)
{
Mat dstBlur = src.clone();
blur(src, dstBlur, Size(5, 5));
Mat imageWarp = Mat(Size(src.cols, src.rows), src.type());
Mat warp = (Mat_<float>(2, 3) << 1, 0, src.cols , 0, 1, 0);
//cv::Mat t_mat = cv::Mat::zeros(2, 3, CV_32FC1);
//t_mat.at<float>(0, 0) = 1;
//t_mat.at<float>(0, 2) = 20; //水平平移量
//t_mat.at<float>(1, 1) = 1;
//t_mat.at<float>(1, 2) = 10; //竖直平移量
warpAffine(dstBlur, imageWarp, warp, Size(2 * src.cols, src.rows));
imshow("image", imageWarp);
copyPixel(src, imageWarp);
imshow("image", imageWarp);
waitKey();
}
void boxFilterTest(Mat& src)
{
Mat dstBlur = src.clone();
boxFilter(src, dstBlur, -1, Size(5, 5), Point(-1, 1), false);
Mat imageWarp = Mat(Size(src.cols, src.rows), src.type());
Mat warp = (Mat_<float>(2, 3) << 1, 0, src.cols, 0, 1, 0);
//cv::Mat t_mat = cv::Mat::zeros(2, 3, CV_32FC1);
//t_mat.at<float>(0, 0) = 1;
//t_mat.at<float>(0, 2) = 20; //水平平移量
//t_mat.at<float>(1, 1) = 1;
//t_mat.at<float>(1, 2) = 10; //竖直平移量
warpAffine(dstBlur, imageWarp, warp, Size(2 * src.cols, src.rows));
imshow("image", imageWarp);
copyPixel(src, imageWarp);
imshow("image", imageWarp);
waitKey();
}
void medianFilterTest(Mat& src)
{
Mat dstBlur = src.clone();
medianBlur(src, dstBlur, 5);
Mat imageWarp = Mat(Size(src.cols, src.rows), src.type());
Mat warp = (Mat_<float>(2, 3) << 1, 0, src.cols, 0, 1, 0);
warpAffine(dstBlur, imageWarp, warp, Size(2 * src.cols, src.rows));
imshow("image", imageWarp);
copyPixel(src, imageWarp);
imshow("image", imageWarp);
waitKey();
}
void myBlur(int, void*)
{
Mat dstBlur;
//printf("type is %d\n ", blurType);
switch (blurType)
{
case 0:
blur(src, dstBlur, Size(5, 5));
break;
case 1:
boxFilter(src, dstBlur, -1, Size(5, 5), Point(-1, 1), false);
break;
case 2:
medianBlur(src, dstBlur, 5);
break;
For the Gaussian blur(an example kernel is shown in Figure 10 - 10), the parameter
ksize gives the widthand height of the filter window.The next parameter indicates
the sigma value(half width at half max) of the Gaussian kernel in the x - dimension.
The fourth parameter similarly indicates the sigma value in the y - dimension.If you
specify only the x value, and set the y value to 0 (its default value), then the yand x
values will be taken to be equal.
case 3:
GaussianBlur(src, dstBlur, Size(5, 5), 1, 1);
break;
Bilateral filtering is
one operation from a somewhat larger class of image analysis operators known as
edge - preserving smoothing.Bilateral filtering is most easily understood when contras‐
ted to Gaussian smoothing.
case 4:
bilateralFilter(src, dstBlur, -1, 10, 1);
break;
default:
break;
}
//if (blurType == 0)
//{
// blur(src, dstBlur, Size(5, 5));
//}
//else if (blurType == 1)
//{
// boxFilter(src, dstBlur, -1, Size(5, 5), Point(-1, 1), false);
//}
Mat imageWarp = Mat(Size(src.cols, src.rows), src.type());
Mat warp = (Mat_<float>(2, 3) << 1, 0, src.cols, 0, 1, 0);
warpAffine(dstBlur, imageWarp, warp, Size(2 * src.cols, src.rows));
imshow("image", imageWarp);
copyPixel(src, imageWarp);
imshow("image", imageWarp);
waitKey();
}
int main()
{
//Mat src = imread("NWPULB.jpg", IMREAD_COLOR);
src = imread("NWPULB.jpg", IMREAD_COLOR);
int width = src.cols / 6;
int height = src.rows / 6;
resize(src, src, Size(width, height));
Mat image = Mat(Size(2*width, height), src.type());
namedWindow("image", 1);
imshow("image", image);
if (src.empty())
{
std::cout << "Could not open or find the image!\n" << std::endl;
return EXIT_FAILURE;
}
//1 blur
//blurTest(src);
//The main difference between cv::box
//Filter() and cv::blur() is that the former can be run in an unnormalized mode
//(normalize = false), and that the depth of the output image dst can be controlled.
//(In the case of cv::blur(), the depth of dst will always equal the depth of src.) If the
//value of ddepth is set to - 1, then the destination image will have the same depth as
//the source; otherwise, you can use any of the usual aliases(e.g., CV_32F)
2 boxFilter
//boxFilterTest(src);
The median filter[Bardyn84] replaces each pixel by the median or “middle - valued”
pixel(as opposed to the mean pixel) in a rectangular neighborhood around the center
pixel.7 Results of median filtering are shown in Figure 10 - 9. Simple blurring by aver‐
aging can be sensitive to noisy images, especially images with large isolated outlier
values(e.g., shot noise in digital photography).Large differences in even a small
number of points can cause a noticeable movement in the average value.Median fil‐
tering is able to ignore the outliers by selecting the middle points.
//3 Median Filter
//medianFilterTest(src);
The next smoothing filter, the Gaussian filter, is probably the most useful.Gaussian
filtering involves convolving each point in the input array with a(normalized) Gaus‐
sian kerneland then summing to produce the output array
//4 All Filter
createTrackbar("show", "image", &blurType, 4, myBlur);
myBlur(0, 0);
return 0;
}
相关文章
- IntelliJ IDEA 12 与 Tomcat7 配置
- js去除空格12种方法
- 12进程同步的概念
- 创业12
- 2016-12-31:最后一天:回顾
- 最新Linux内核本地DoS漏洞CVE-2017-8064 4.10.12之前版本均受影响 还有一个CVE-2017-7979
- 2022-12-14 mysql-innerJoin访问多表-优化器改变表访问顺序-分析
- 2022-12-10青少年软件编程(C语言)等级考试试卷(六级)解析
- 1247:河中跳房子 2020-12-28
- 1246:膨胀的木棍 2020-12-28
- 1240:查找最接近的元素 2020-12-27
- 1238:一元三次方程求解 2020-12-27
- 1233:接水问题 2020-12-11
- 1231:最小新整数 2020-12-11
- 1224:最大子矩阵 2020-12-02
- 2020-12-23
- 2020-12-12
- 2020-11-12
- 剑指offer:12-17记录
- 2022-09-12 Lex and YACC primer/HOWTO
- 可以作为你的候选的12个很赞的移动开发框架
- 2020-10-12 大二2020cf训练
- 2018-12-18-WPF-一个空的-WPF-程序有多少个窗口
- 2019-11-12-WPF-添加窗口消息钩子方法
- 2018-12-25-win10-uwp-release-因为-Entry-Point-Not-Found-无法启动
- 2018-4-12-win10-uwp-使用油墨输入