当前位置:网站首页>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
,会选区最近的像素点进行插值。
边栏推荐
- 使用C#语言来进行json串的接收
- Open3d learning note 3 [sampling and voxelization]
- Timeout docking video generation
- Handwritten call, apply, bind
- Gensim如何冻结某些词向量进行增量训练
- Summary of solving the Jetson nano installation onnx error (error: failed building wheel for onnx)
- Open3d learning notes II [file reading and writing]
- 【MobileNet V3】《Searching for MobileNetV3》
- 简易打包工具的安装与使用
- The internal network of the server can be accessed, but the external network cannot be accessed
猜你喜欢
Open3d learning notes 1 [first glimpse, file reading]
What if the notebook computer cannot run the CMD command
Income in the first month of naked resignation
【Random Erasing】《Random Erasing Data Augmentation》
Sequence problem for tqdm and print
【MobileNet V3】《Searching for MobileNetV3》
Several methods of image enhancement and matlab code
Network metering - transport layer
图像增强的几个方法以及Matlab代码
【Cascade FPD】《Deep Convolutional Network Cascade for Facial Point Detection》
随机推荐
EKLAVYA -- 利用神经网络推断二进制文件中函数的参数
Data reverse attack under federated learning -- gradinversion
【TCDCN】《Facial landmark detection by deep multi-task learning》
Using super ball embedding to enhance confrontation training
Cvpr19 deep stacked hierarchical multi patch network for image deblurring paper reproduction
I'll show you why you don't need to log in every time you use Taobao, jd.com, etc?
稀疏矩阵存储
Open3d learning note 3 [sampling and voxelization]
open3d学习笔记二【文件读写】
Summary of open3d environment errors
我的vim配置文件
浅谈深度学习中的对抗样本及其生成方法
关于原型图的深入理解
【Hide-and-Seek】《Hide-and-Seek: A Data Augmentation Technique for Weakly-Supervised Localization xxx》
[learning notes] matlab self compiled image convolution function
【双目视觉】双目矫正
【MobileNet V3】《Searching for MobileNetV3》
Find and rfind methods in string
【MagNet】《Progressive Semantic Segmentation》
MySQL优化