转载自https://blog.csdn.net/weixin_39940512/article/details/105343418
@(Aaron) [图像处理, 图像插值算法]
1.1 简介
在图像处理中,平移变换、旋转变换以及放缩变换是一些基础且常用的操作。这些几何变换并不改变图象的象素值,只是在图象平面上进行象素的重新排列。在一幅输入图象 [ u , v ] [ u , v ] [ u , v ] [u,v][u,v] [u,v] [u,v][u,v][u,v]f(x,y)=[f(1,0)−f(0,0)]x+[f(0,1)−f(0,0)]y+[f(1,1)+f(0,0)−f(0,1)−f(1,0)]xy+f(0,0)
1.4.3 映射方法
向前映射法
可以将几何运算想象成一次一个象素地转移到输出图象中。如果一个输入象素被映射到四个输出象素之间的位置,则其灰度值就按插值算法在4个输出象素之间进行分配。称为向前映射法,或象素移交影射。
注:从原图象坐标计算出目标图象坐标镜像、平移变换使用这种计算方法
向后映射法
向后映射法(或象素填充算法)是输出象素一次一个地映射回到输入象素中,以便确定其灰度级。如果一个输出象素被映射到4个输入象素之间,则其灰度值插值决定,向后空间变换是向前变换的逆。
注:从结果图象的坐标计算原图象的坐标
- 旋转、拉伸、放缩可以使用
- 解决了漏点的问题,出现了马赛克
1.5 基于OpenCV的实现
1.5.1 C++
函数原型:
void cv::resize(InputArray src, OutputArray dst, Size dsize, double fx=0, double fy=0, int interpolation=INTER_LINEAR )
src:输入图像
dst:输出图像
dsize:输出图像尺寸
fx、fy:x,y方向上的缩放因子
INTER_LINEAR:插值方法,总共五种
1. INTER_NEAREST - 最近邻插值法
2. INTER_LINEAR - 双线性插值法(默认)
3. INTER_AREA - 基于局部像素的重采样(resampling using pixel area relation)。对于图像抽取(image decimation)来说,这可能是一个更好的方法。但如果是放大图像时,它和最近邻法的效果类似。
4. INTER_CUBIC - 基于4x4像素邻域的3次插值法
5. INTER_LANCZOS4 - 基于8x8像素邻域的Lanczos插值
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
代码实践:
#include <opencv2/opencv.hpp>
#include <iostream>
using namespace cv;
using namespace std;
int main(int argc, char* argv[])
{
Mat img = imread(“D:/image/yuner.jpg”);
if (img.empty())
{
cout << “无法读取图像” << endl;
return 0;
}
<span class="token keyword">int</span> height <span class="token operator">=</span> img<span class="token punctuation">.</span>rows<span class="token punctuation">;</span>
<span class="token keyword">int</span> width <span class="token operator">=</span> img<span class="token punctuation">.</span>cols<span class="token punctuation">;</span>
<span class="token comment">// 缩小图像,比例为(0.2, 0.2)</span>
Size dsize <span class="token operator">=</span> <span class="token function">Size</span><span class="token punctuation">(</span><span class="token function">round</span><span class="token punctuation">(</span><span class="token number">0.2</span> <span class="token operator">*</span> width<span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token function">round</span><span class="token punctuation">(</span><span class="token number">0.2</span> <span class="token operator">*</span> height<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
Mat shrink<span class="token punctuation">;</span>
<span class="token comment">//使用双线性插值</span>
<span class="token function">resize</span><span class="token punctuation">(</span>img<span class="token punctuation">,</span> shrink<span class="token punctuation">,</span> dsize<span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> INTER_LINEAR<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment">// 在缩小图像的基础上,放大图像,比例为(1.5, 1.5)</span>
<span class="token keyword">float</span> fx <span class="token operator">=</span> <span class="token number">1.5</span><span class="token punctuation">;</span>
<span class="token keyword">float</span> fy <span class="token operator">=</span> <span class="token number">1.5</span><span class="token punctuation">;</span>
Mat enlarge1<span class="token punctuation">,</span> enlarge2<span class="token punctuation">;</span>
<span class="token function">resize</span><span class="token punctuation">(</span>shrink<span class="token punctuation">,</span> enlarge1<span class="token punctuation">,</span> <span class="token function">Size</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span> fx<span class="token punctuation">,</span> fy<span class="token punctuation">,</span> INTER_NEAREST<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token function">resize</span><span class="token punctuation">(</span>shrink<span class="token punctuation">,</span> enlarge2<span class="token punctuation">,</span> <span class="token function">Size</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span> fx<span class="token punctuation">,</span> fy<span class="token punctuation">,</span> INTER_LINEAR<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment">// 显示</span>
<span class="token function">imshow</span><span class="token punctuation">(</span><span class="token string">"src"</span><span class="token punctuation">,</span> img<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token function">imshow</span><span class="token punctuation">(</span><span class="token string">"shrink"</span><span class="token punctuation">,</span> shrink<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token function">imshow</span><span class="token punctuation">(</span><span class="token string">"INTER_NEAREST"</span><span class="token punctuation">,</span> enlarge1<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token function">imshow</span><span class="token punctuation">(</span><span class="token string">"INTER_LINEAR"</span><span class="token punctuation">,</span> enlarge2<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token function">waitKey</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">return</span> <span class="token number">0</span><span class="token punctuation">;</span>
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
原图
0.2倍缩小,双线性插值
1.5倍放大,最近邻插值
1.5倍放大,双线性插值
1.5.2 Python
函数原型:
cv2.resize(src, dsize[, dst[, fx[, fy[, interpolation]]]])
参数:
参数 | 描述 |
---|---|
src | 【必需】原图像 |
dsize | 【必需】输出图像所需大小 |
fx | 【可选】沿水平轴的比例因子 |
fy | 【可选】沿垂直轴的比例因子 |
interpolation | 【可选】插值方式 |
插值方式:
cv.INTER_NEAREST | 最近邻插值 |
cv.INTER_LINEAR | 双线性插值 |
cv.INTER_CUBIC | 基于4x4像素邻域的3次插值法 |
cv.INTER_AREA | 基于局部像素的重采样 |
通常,缩小使用cv.INTER_AREA,放缩使用cv.INTER_CUBIC(较慢)和cv.INTER_LINEAR(较快效果也不错)。默认情况下,所有的放缩都使用cv.INTER_LINEAR。
代码实践:
import cv2
if name == “main”:
img = cv2.imread(‘D:/image/yuner.jpg’, cv2.IMREAD_UNCHANGED)
<span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">'Original Dimensions : '</span><span class="token punctuation">,</span>img<span class="token punctuation">.</span>shape<span class="token punctuation">)</span>
scale_percent <span class="token operator">=</span> <span class="token number">30</span> <span class="token comment"># percent of original size</span>
width <span class="token operator">=</span> <span class="token builtin">int</span><span class="token punctuation">(</span>img<span class="token punctuation">.</span>shape<span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span> <span class="token operator">*</span> scale_percent <span class="token operator">/</span> <span class="token number">100</span><span class="token punctuation">)</span>
height <span class="token operator">=</span> <span class="token builtin">int</span><span class="token punctuation">(</span>img<span class="token punctuation">.</span>shape<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span> <span class="token operator">*</span> scale_percent <span class="token operator">/</span> <span class="token number">100</span><span class="token punctuation">)</span>
dim <span class="token operator">=</span> <span class="token punctuation">(</span>width<span class="token punctuation">,</span> height<span class="token punctuation">)</span>
<span class="token comment"># resize image</span>
resized <span class="token operator">=</span> cv2<span class="token punctuation">.</span>resize<span class="token punctuation">(</span>img<span class="token punctuation">,</span> dim<span class="token punctuation">,</span> interpolation <span class="token operator">=</span> cv2<span class="token punctuation">.</span>INTER_LINEAR<span class="token punctuation">)</span>
fx <span class="token operator">=</span> <span class="token number">1.5</span>
fy <span class="token operator">=</span> <span class="token number">1.5</span>
resized1 <span class="token operator">=</span> cv2<span class="token punctuation">.</span>resize<span class="token punctuation">(</span>resized<span class="token punctuation">,</span> dsize<span class="token operator">=</span><span class="token boolean">None</span><span class="token punctuation">,</span> fx<span class="token operator">=</span>fx<span class="token punctuation">,</span> fy<span class="token operator">=</span>fy<span class="token punctuation">,</span> interpolation <span class="token operator">=</span> cv2<span class="token punctuation">.</span>INTER_NEAREST<span class="token punctuation">)</span>
resized2 <span class="token operator">=</span> cv2<span class="token punctuation">.</span>resize<span class="token punctuation">(</span>resized<span class="token punctuation">,</span> dsize<span class="token operator">=</span><span class="token boolean">None</span><span class="token punctuation">,</span> fx<span class="token operator">=</span>fx<span class="token punctuation">,</span> fy<span class="token operator">=</span>fy<span class="token punctuation">,</span> interpolation <span class="token operator">=</span> cv2<span class="token punctuation">.</span>INTER_LINEAR<span class="token punctuation">)</span>
<span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">'Resized Dimensions : '</span><span class="token punctuation">,</span>resized<span class="token punctuation">.</span>shape<span class="token punctuation">)</span>
cv2<span class="token punctuation">.</span>imshow<span class="token punctuation">(</span><span class="token string">"Resized image"</span><span class="token punctuation">,</span> resized<span class="token punctuation">)</span>
cv2<span class="token punctuation">.</span>imshow<span class="token punctuation">(</span><span class="token string">"INTER_NEAREST image"</span><span class="token punctuation">,</span> resized1<span class="token punctuation">)</span>
cv2<span class="token punctuation">.</span>imshow<span class="token punctuation">(</span><span class="token string">"INTER_LINEAR image"</span><span class="token punctuation">,</span> resized2<span class="token punctuation">)</span>
cv2<span class="token punctuation">.</span>waitKey<span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">)</span>
cv2<span class="token punctuation">.</span>destroyAllWindows<span class="token punctuation">(</span><span class="token punctuation">)</span>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
0.3倍缩小,双线性插值
1.5倍放大,最近邻插值
1.5倍放大,双线性插值
- 推荐书籍:学习OpenCV中文版
- 推荐博客:https://blog.csdn.net/hongbin_xu/category_6936122.html
1.6 总结
插值算法是很多几何变换的基础和前置条件,对插值算法细节的掌握有助于对其他算法的理解,为自己的学习打下坚实的基础。
Task01 OpenCV框架与图像插值算法 END.
— By: Aaron
博客:https://sandy1230.github.io/
博客:https://blog.csdn.net/weixin_39940512
关于Datawhale:
Datawhale是一个专注于数据科学与AI领域的开源组织,汇集了众多领域院校和知名企业的优秀学习者,聚合了一群有开源精神和探索精神的团队成员。Datawhale以“for the learner,和学习者一起成长”为愿景,鼓励真实地展现自我、开放包容、互信互助、敢于试错和勇于担当。同时Datawhale 用开源的理念去探索开源内容、开源学习和开源方案,赋能人才培养,助力人才成长,建立起人与人,人与知识,人与企业和人与未来的联结。
</div>
<link href="https://csdnimg.cn/release/phoenix/mdeditor/markdown_views-60ecaf1f42.css" rel="stylesheet">
<div data-report-view="{"mod":"popu_387","dest":"https://blog.csdn.net/weixin_39940512/article/details/105343418","extend1":"pc","ab":"new"}"></div>
<div class="person-messagebox">
<div class="left-message"><a href="https://blog.csdn.net/weixin_39940512">
<img src="https://profile.csdnimg.cn/4/1/5/3_weixin_39940512" class="avatar_pic" username="weixin_39940512">
</a></div>
<div class="middle-message">
<div class="title"><span class="tit "><a href="https://blog.csdn.net/weixin_39940512" data-report-click="{"mod":"popu_379","ab":"new"}" target="_blank">此心安处是吾乡-Aaron</a></span>
<!-- 等级,level -->
<img class="identity-icon" src="https://csdnimg.cn/identity/blog1.png"> </div>
<div class="text"><span>原创文章 7</span><span>获赞 3</span><span>访问量 347</span></div>
</div>
<div class="right-message">
<a class="btn btn-sm bt-button personal-watch" data-report-click="{"mod":"popu_379","ab":"new"}">关注</a>
<a href="https://im.csdn.net/im/main.html?userName=weixin_39940512" target="_blank" class="btn btn-sm bt-button personal-letter">私信
</a>
</div>
</div>
</div>