当前位置:网站首页>OpenCV3 6.3 用滤波器进行缩减像素采样
OpenCV3 6.3 用滤波器进行缩减像素采样
2022-07-02 06:28:00 【Villanelle#】
缩减像素采样
降低图像精度的过程称为缩减像素采样(downsampling),提升图像精度的过程称为提升像素采样(upsampling)。
空间假频
如果要缩小一幅图像,仅仅消除图像中部分行和列,再将剩余的像素拼接起来,得到的图像效果往往不尽人意。
cv::Mat image = cv::imread("boldt.jpg");
cv::Mat reduced1(image.rows / 4, image.cols / 4, CV_8UC3);
for (int i = 0; i < reduced1.rows; i++)
{
for (int j = 0; j < reduced1.cols; j++)
{
reduced1.at<cv::Vec3b>(i, j) = image.at<cv::Vec3b>(i * 4, j * 4);
}
}
//将每个像素按原大小的四倍显示(使用最近点插值)
cv::resize(reduced1, reduced1, cv::Size(), 4, 4, cv::INTER_NEAREST);
cv::imshow("badly reduced", reduced1);

在本例中,先遍历保留四个像素中的一个,再通过cv::resize()函数放大。可以看出,图像的质量明显降低了,可以看到明显的锯齿变形,是由于空间假频造成的。
当试图在图像中包含高频成分而由于图像太小无法包含时,就会出现空间假频,解决办法是在缩小图像之前去除其高频成分,本例中使用了上一节的高斯滤波。
cv::Mat reduced2(image.rows / 4, image.cols / 4, CV_8UC3);
//首先使用高斯滤波去除高频成分
cv::GaussianBlur(image, image, cv::Size(11, 11), 2.0);
for (int i = 0; i < reduced2.rows; i++)
{
for (int j = 0; j < reduced2.cols; j++)
{
reduced2.at<cv::Vec3b>(i, j) = image.at<cv::Vec3b>(i * 4, j * 4);
}
}
//将每个像素按原大小的四倍显示(使用最近点插值)
cv::resize(reduced2, reduced2, cv::Size(), 4, 4, cv::INTER_NEAREST);
cv::imshow("reduced image", reduced2);

图像质量比直接去除像素要好很多,但也丢失了一些精致的细节。
另外,引出一个定理Nyquist-Shannon定理,即图像缩小一半,其可见的频率带宽也将减少一半。
cv::pyrDown实现图像缩减
OpenCV内置了cv::pyrDown函数实现了图像缩减,其原理是使用5*5的高斯滤波器,先对图像进行低通滤波再进行缩减(只能缩减到1/2)。
其参数分别为原图像、输出图像。
cv::Mat reduced3;
cv::pyrDown(image, reduced3);
cv::imshow("pyrdown", reduced3);

与此相反,还有一个cv::pyrUp函数可以实现图像尺寸的放大,原理是先在每两行/两列间插入像素值为0的像素,再对扩展后的图像应用5*5的高斯滤波器。
其参数分别为:原图像、输出图像。
cv::Mat enlarged;
cv::pyrUp(image, enlarged);
cv::imshow("pyrup", enlarged);

上述的两个内置函数只能实现两倍的缩减和放大,其实还有一个更通用的函数cv::resize,可以指定缩放后图像的尺寸。
cv::resize函数的使用
函数签名
CV_EXPORTS_W void resize( InputArray src, OutputArray dst,
Size dsize, double fx = 0, double fy = 0,
int interpolation = INTER_LINEAR );
- 参数分别为:原图像,输出图像,输出图像的大小,x方向放大系数,y方向放大系数,插值方法。
- 可以不指定输出图像的大小(指定为空cv::Size()),则必须指定x和y方向的放大系数,如为2就是放大两倍。
- 可以不指定放大系数(默认为0),则必须指定输出图像的大小。
- 此处的插值方法若不指定为双线性插值
cv::INTER_LINEAR,可以结合多个邻近像素的值,效果较好。此外还有最邻近插值cv::INTER_LINEAR,会选区最近的像素点进行插值。
边栏推荐
- Replace convolution with full connection layer -- repmlp
- 针对tqdm和print的顺序问题
- [CVPR‘22 Oral2] TAN: Temporal Alignment Networks for Long-term Video
- 【MobileNet V3】《Searching for MobileNetV3》
- C # connect to MySQL database
- Simply test the two different data transmission methods of content length and chunked
- Eklavya -- infer the parameters of functions in binary files using neural network
- 【Sparse-to-Dense】《Sparse-to-Dense:Depth Prediction from Sparse Depth Samples and a Single Image》
- How to back up the configuration before the idea when reinstalling the idea
- 业务架构图
猜你喜欢
![[learning notes] numerical differentiation of back error propagation](/img/1c/e28e31d7cc5ccc38607c7839ccc5f0.png)
[learning notes] numerical differentiation of back error propagation

open3d学习笔记三【采样与体素化】

【Sparse-to-Dense】《Sparse-to-Dense:Depth Prediction from Sparse Depth Samples and a Single Image》

【DIoU】《Distance-IoU Loss:Faster and Better Learning for Bounding Box Regression》

【Random Erasing】《Random Erasing Data Augmentation》

利用超球嵌入来增强对抗训练

Where do you find the materials for those articles that have read 10000?

What if the laptop task manager is gray and unavailable

Principes fondamentaux de la théorie musicale (brève introduction)

Hystrix dashboard cannot find hystrix Stream solution
随机推荐
应对长尾分布的目标检测 -- Balanced Group Softmax
用MLP代替掉Self-Attention
Yolov3 trains its own data set (mmdetection)
Nacos service registration in the interface
多站点高可用部署
open3d学习笔记二【文件读写】
What if the laptop can't search the wireless network signal
Open3d learning note 5 [rgbd fusion]
用C# 语言实现MYSQL 真分页
How to wrap qstring strings
针对tqdm和print的顺序问题
【MobileNet V3】《Searching for MobileNetV3》
[binocular vision] binocular correction
Cvpr19 deep stacked hierarchical multi patch network for image deblurring paper reproduction
Graph Pooling 简析
【学习笔记】Matlab自编高斯平滑器+Sobel算子求导
力扣方法总结:查找类
Business architecture diagram
【FastDepth】《FastDepth:Fast Monocular Depth Estimation on Embedded Systems》
Brief introduction of prompt paradigm