当前位置:网站首页>Opencv3 6.3 reduced pixel sampling with filters
Opencv3 6.3 reduced pixel sampling with filters
2022-07-02 08:08:00 【Villanelle#】
Reduce pixel sampling
The process of reducing image accuracy is called Reduce pixel sampling (downsampling), The process of improving image accuracy is called Improve pixel sampling (upsampling).
Spatial pseudofrequency
If you want to zoom out an image , Just eliminate the middle rows and columns of the image , Then splice the remaining pixels , The image effect is often unsatisfactory .
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);
}
}
// Display each pixel four times its original size ( Use nearest point interpolation )
cv::resize(reduced1, reduced1, cv::Size(), 4, 4, cv::INTER_NEAREST);
cv::imshow("badly reduced", reduced1);

In this case , First traverse and reserve one of the four pixels , Re pass cv::resize() Function amplification . It can be seen that , The quality of the image is significantly reduced , Obvious sawtooth deformation can be seen , Is due to Spatial pseudofrequency Caused by the .
When trying to include high-frequency components in the image and the image is too small to include , Will appear Spatial pseudofrequency , The solution is Remove the high-frequency components before reducing the image , In this example, Gaussian filtering in the previous section is used .
cv::Mat reduced2(image.rows / 4, image.cols / 4, CV_8UC3);
// First, Gaussian filtering is used to remove high-frequency components
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);
}
}
// Display each pixel four times its original size ( Use nearest point interpolation )
cv::resize(reduced2, reduced2, cv::Size(), 4, 4, cv::INTER_NEAREST);
cv::imshow("reduced image", reduced2);

Image quality is much better than directly removing pixels , But also lost some delicate details .
in addition , Lead to a theorem Nyquist-Shannon Theorem , That is, the image is reduced by half , Its visible frequency bandwidth will also be reduced by half .
cv::pyrDown Achieve image reduction
OpenCV Built in cv::pyrDown Function realizes image reduction , The principle is to use 5*5 Gauss filter , First, low-pass filter the image and then reduce it ( It can only be reduced to 1/2).
Its parameters are the original image 、 Output image .
cv::Mat reduced3;
cv::pyrDown(image, reduced3);
cv::imshow("pyrdown", reduced3);

On the contrary , One more cv::pyrUp Function can enlarge the image size , The principle is first on every two lines / The pixel value inserted between two columns is 0 The pixel , Then apply to the expanded image 5*5 Gauss filter .
The parameters are : Original image 、 Output image .
cv::Mat enlarged;
cv::pyrUp(image, enlarged);
cv::imshow("pyrup", enlarged);

The above two built-in functions can only achieve twice the reduction and amplification , In fact, there is a more general function cv::resize, You can specify the size of the scaled image .
cv::resize Use of functions
Function signature
CV_EXPORTS_W void resize( InputArray src, OutputArray dst,
Size dsize, double fx = 0, double fy = 0,
int interpolation = INTER_LINEAR );
- The parameters are : Original image , Output image , The size of the output image ,x Directional amplification factor ,y Directional amplification factor , Interpolation method .
- You can not specify the size of the output image ( The specified value is empty cv::Size()), Must be specified x and y Amplification factor of direction , If 2 Just double the magnification .
- You may not specify the magnification factor ( The default is 0), You must specify the size of the output image .
- If the interpolation method here is not specified as bilinear interpolation
cv::INTER_LINEAR, You can combine the values of multiple adjacent pixels , It is better to . In addition, there is nearest neighbor interpolationcv::INTER_LINEAR, The nearest pixel will be selected for interpolation .
边栏推荐
- Daily practice (19): print binary tree from top to bottom
- Carsim-问题Failed to start Solver: PATH_ID_OBJ(X) was set to Y; no corresponding value of XXXXX?
- Replace self attention with MLP
- Using transformer for object detection and semantic segmentation
- 用MLP代替掉Self-Attention
- STM32疑难杂症之ST-LINK Connection error INVALID ROM TABLE
- Carsim-实时仿真的动画同步问题
- AR system summary harvest
- Prompt 范式简述
- Jupyter Notebook常用快捷键(在命令模式中按H也可查看)
猜你喜欢
![[learning notes] numerical differentiation of back error propagation](/img/1c/e28e31d7cc5ccc38607c7839ccc5f0.png)
[learning notes] numerical differentiation of back error propagation

浅谈深度学习中的对抗样本及其生成方法

SQLyog远程连接centos7系统下的MySQL数据库

Several methods of image enhancement and matlab code

Data reverse attack under federated learning -- gradinversion

jetson nano安装tensorflow踩坑记录(scipy1.4.1)

Matlab数学建模工具

图像增强的几个方法以及Matlab代码
![Open3d learning note 5 [rgbd fusion]](/img/0a/41d38e4b0295b6674143f3f74a4a8d.png)
Open3d learning note 5 [rgbd fusion]

樂理基礎(簡述)
随机推荐
How gensim freezes some word vectors for incremental training
Simply test the two different data transmission methods of content length and chunked
OpenCV常用方法出处链接(持续更新)
Get the width and height of the screen in real time (adaptive)
【学习笔记】Matlab自编高斯平滑器+Sobel算子求导
Open3d learning notes 1 [first glimpse, file reading]
Prompt 范式简述
Eklavya -- infer the parameters of functions in binary files using neural network
【学习笔记】Matlab自编图像卷积函数
静态库和动态库
Wang extracurricular words
Replace convolution with full connection layer -- repmlp
针对语义分割的真实世界的对抗样本攻击
笔记本电脑卡顿问题原因
[binocular vision] binocular stereo matching
Specification for package drawing
業務架構圖
MySQL optimization
稀疏矩阵存储
我的vim配置文件