图像处理之基础---图像缩放中的双线性插值c实现
在进入频域变换之前, 我们还是轻松一下,再搞点平面上的变化来看看。这把选了一个双线性插值(Bilinear interpolation)来实现是源于看到了csdn上别人的问题, 权且实现一个函数,方便大家的使用吧。
双线性插值简单的说,就是扩展了之后的图像像素坐标映射回原来的坐标空间的时候, 如果出现了没有对应到整数点的情况。这时候需要做2次线性的插值计算出新的坐标的像素值,比如说:

这里可以看到这个P点落在了ABCD区间内, 如果我们本着最朴素的这个P点最靠近谁权重就越大的加权平均的思想, 我们很容易得到这样的论断:
A点对P的影响就是Sa的面积, B点的影响就是Sb, C点就Sc, d就是Sd。这样越近就是权重越大,基本上就是这样的逻辑。
这样P的像素可以简单的用 (A*Sa+B*Sb+C*Sc+D*Sd )/(Sa+Sb+Sc+Sd);来得到了。以我的雷厉风行,马上写出了如下的代码:
- /**
- ** method to remove sharp the raw image with unsharp mask
- * @param src input grayscale binary array
- * @param dst output grayscale result, the memory need to be allocated outside of the function
- * @param srcWidth width of the input grayscale image
- * @param srcHeight height of the input grayscale image
- * @param scalePercent, scale percentage (0-xxx)
- */
- void stretchImage (const unsigned char* src, unsigned char* dst, int srcWidth, int srcHeight, int scalePercent)
- {
- if (scalePercent < 0)
- return;
- int x, y;
- int ox, oy;
- int tmpx,tmpy;
- int ratio = (100 << 8)/scalePercent;
- int dstWidth = srcWidth * scalePercent / 100;
- int dstHeight = srcHeight * scalePercent / 100;
- unsigned char color[2][2];
- for (int j = 0; j < dstHeight; j ++)
- {
- for (int i = 0; i < dstWidth; i ++)
- {
- tmpx = i * ratio;
- tmpy = j * ratio;
- ox = tmpx >> 8;
- oy = tmpy >> 8;
- x = tmpx & 0xFF;
- y = tmpy & 0xFF;
- color[0][0] = src[ oy*srcWidth + ox ];
- color[1][0] = src[ oy*srcWidth + ox +1 ];
- color[0][1] = src[ (oy+1)*srcWidth + ox ];
- color[1][1] = src[ (oy+1)*srcWidth + ox+1 ];
- int final = (0x100 - x)*(0x100 - y)*color[0][0] + x*(0x100 - y)*color[1][0] + (0x100-x)*y*color[0][1] + x*y*color[1][1];
- final = final >> 16;
- if (final>255)
- final = 255;
- if (final<0)
- final = 0;
- dst [ j*dstWidth + i] = (unsigned char)final;
- }
- }
- }
/**
** method to remove sharp the raw image with unsharp mask
* @param src input grayscale binary array
* @param dst output grayscale result, the memory need to be allocated outside of the function
* @param srcWidth width of the input grayscale image
* @param srcHeight height of the input grayscale image
* @param scalePercent, scale percentage (0-xxx)
*/
void stretchImage (const unsigned char* src, unsigned char* dst, int srcWidth, int srcHeight, int scalePercent)
{
if (scalePercent < 0)
return;
int x, y;
int ox, oy;
int tmpx,tmpy;
int ratio = (100 << 8)/scalePercent;
int dstWidth = srcWidth * scalePercent / 100;
int dstHeight = srcHeight * scalePercent / 100;
unsigned char color[2][2];
for (int j = 0; j < dstHeight; j ++)
{
for (int i = 0; i < dstWidth; i ++)
{
tmpx = i * ratio;
tmpy = j * ratio;
ox = tmpx >> 8;
oy = tmpy >> 8;
x = tmpx & 0xFF;
y = tmpy & 0xFF;
color[0][0] = src[ oy*srcWidth + ox ];
color[1][0] = src[ oy*srcWidth + ox +1 ];
color[0][1] = src[ (oy+1)*srcWidth + ox ];
color[1][1] = src[ (oy+1)*srcWidth + ox+1 ];
int final = (0x100 - x)*(0x100 - y)*color[0][0] + x*(0x100 - y)*color[1][0] + (0x100-x)*y*color[0][1] + x*y*color[1][1];
final = final >> 16;
if (final>255)
final = 255;
if (final<0)
final = 0;
dst [ j*dstWidth + i] = (unsigned char)final;
}
}
}
需要说明的事情是, 浮点数需要引入效率上一定的损失, 当然我们这里就用大数来和谐。但是只是随便写写的代码, 我们没有加入超出int范围的检查或者说明,暂时也只能这样了:)。用了这个函数的效果还是不错的, 我们来看看在75%,125%和250%时候的效果:
原图:

%75效果图:

125%效果图:

250%效果图:

其实从效果图多少可以看出一些的问题就是, 随着图像的拉伸, 图像的锐度其实降低了, 这个比较容易想象的,因为我们这个拉伸的办法本身就是线性的,无疑来扩大的时候把锐利的边缘模糊化了,所以自然在图像扩大很多倍的时候效果不是很好了。
http://blog.csdn.net/hhygcy/article/details/4434870#comments
图像处理之基础---图像缩放中的双线性插值c实现的更多相关文章
- opencv学习笔记——图像缩放函数resize
opencv提供了一种图像缩放函数 功能:实现对输入图像缩放到指定大小 函数原型: void cv::resize ( InputArray src, OutputArray dst, Size ds ...
- opencv3 图像处理(一)图像缩放( python与c++ 实现)
opencv3 图像处理 之 图像缩放( python与c++实现 ) 一. 主要函数介绍 1) 图像大小变换 Resize () 原型: void Resize(const CvArr* src,C ...
- 邻近双线性插值图像缩放的Python实现
最近在查找有关图像缩放之类的算法,因工作中需要用到诸如此类的图像处理算法就在网上了解了一下相关算法,以及其原理,并用Python实现,且亲自验证过,在次与大家分享. 声明:本文代码示例针对的是plan ...
- c#数字图像处理(十)图像缩放
图像几何变换(缩放.旋转)中的常用的插值算法 在图像几何变换的过程中,常用的插值方法有最邻近插值(近邻取样法).双线性内插值和三次卷积法. 最邻近插值: 这是一种最为简单的插值方法,在图像中最小的单位 ...
- 实现基于最近邻内插和双线性内插的图像缩放C++实现
平时我们写图像处理的代码时,如果需要缩放图片,我们都是直接调用图像库的resize函数来完成图像的缩放.作为一个机器视觉或者图像处理算法的工作者,图像缩放代码的实现应该是必须掌握的.在众多图像缩放算法 ...
- OpenCV计算机视觉学习(11)——图像空间几何变换(图像缩放,图像旋转,图像翻转,图像平移,仿射变换,镜像变换)
如果需要处理的原图及代码,请移步小编的GitHub地址 传送门:请点击我 如果点击有误:https://github.com/LeBron-Jian/ComputerVisionPractice 图像 ...
- 图像缩放_OpenCv
图像缩放是一种比较简单的图像处理操作,这里给出opencv中的代码, opencv的版本C语言接口 int resize_c() { const char *pstrImageName = " ...
- opencv2 矩阵方式 resize图像缩放代码(转载)
http://blog.sina.com.cn/s/blog_74a459380101r0yx.html opencv2 矩阵方式 resize图像缩放代码(转载) (2014-05-16 09:55 ...
- OpenCV探索之路(二):图像处理的基础知识点串烧
opencv图像初始化操作 #include<opencv2\opencv.hpp> #include<opencv2\highgui\highgui.hpp> using n ...
随机推荐
- hdfs深入:09、获取分布式文件系统客户端的几种方式
FileSystem是一个抽象类:获取一个抽象类有两种方式:第一种:看这个抽象类有没有提供什么方法返回他本身第二种:找子类 具体代码如下: /** * 通过url注册的方式访问hdfs,了解,不会用到 ...
- python selenium等待特定网页元素加载完毕
selenium等待特定元素加载完毕 is_disappeared = WebDriverWait(driver, 8, 0.5, ignored_exceptions=TimeoutExceptio ...
- viewDidLoad等相关函数调用
viewDidLoad 此方法只有当view从nib文件初始化的时候才被调用.viewDidLoad用于初始化,加载时用到的. loadView 此方法在控制器的view为nil的时候被调用.虽然经常 ...
- 小甲鱼Python学习笔记
一 isdigit()True: Unicode数字,byte数字(单字节),全角数字(双字节),罗马数字False: 汉字数字Error: 无 isdecimal()True: Unicode数字, ...
- 利用端口转发来访问virtualbox虚拟机centos6的jupyter notebook
1.除了在virtualbox中设置常规的端口转发外,还需要在windows上打开cmd,输入ssh -N -f -L localhost:8888:localhost:8889 -p 22 root ...
- 杭电 1862 EXCEL排序(sort+结构体)
Description Excel可以对一组纪录按任意指定列排序.现请你编写程序实现类似功能. Input 测试输入包含若干测试用例.每个测试用例的第1行包含两个整数 N (<=100000 ...
- STM32F407 串口通信实验 第26节 个人笔记
前言 这篇笔记对应正点原子STM32F407探索者 ,教学视频第26节,网址如下: https://ke.qq.com/webcourse/index.html#cid=279403&term ...
- js变量类型详解
<html> <title>js变量类型详解</title> <meta http-equiv="content-type" conten ...
- 在docker下面安装Nginx PHP mysql let's encrypt
最近自己在弄了个Nginx PHP Mysql Let's encrypt的docker,下面记录一下 1)先装 Let's encrypt docker run --rm -p 80:80 -p 4 ...
- [K/3Cloud]将JSON字符串反序列化为C#动态对象
using Kingdee.BOS.Util; string errString="{/"Row/":1,/"PageId/":/"1234 ...