图像处理之基础---图像缩放中的双线性插值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 ...
随机推荐
- MFCEditBox如何自动换行
设置该EditBox属性: 1.Auto HScroll False 2.OEM Convert False 3.Want Return ...
- Chrome插件:浏览器后台与页面间通信
content.js 与 background.js和popup.js 通信和 background.js与popup.js 这些通信都用 chrome.runtime.sendMessage 这个 ...
- vue props传值方法
<template> <div class="hello"> <ul> <li v-for="(item, index) in ...
- POJ - 2955 Brackets (区间DP)
题目: 给出一个有括号的字符串,问这个字符串中能匹配的最长的子串的长度. 思路: 区间DP,首先枚举区间长度,然后在每一个长度中通过枚举这个区间的分割点来更新这个区间的最优解.还是做的少. 代码: / ...
- crm项目之stark组件前戏(二)
stark组件的设计主要来源于django中admin的功能,在django admin中只需要将模型表进行注册,就可以在页面对该表进行curd的动作,那么django admin是如何做的呢? 在d ...
- super在python中有什么用
所属网站分类: python高级 > 面向对象 作者:阿里妈妈 链接:http://www.pythonheidong.com/blog/article/74/ 来源:python黑洞网 有什么 ...
- Hermite (埃尔米特)曲线
Hermite 曲线 已知曲线的两个端点坐标P0.P1,和端点处的切线R0.R1,确定的一条曲线. 参数方程 1. 几何形式 2. 矩阵形式 3. 推导 例子分析 如上图有四个点,假如P0.P2是端点 ...
- CookiesReader
CookiesReader "use strict"; /** * * @author xgqfrms * @license MIT * @copyright xgqfrms * ...
- 南邮CTF密码学,mixed_base64
# -*- coding:utf-8 -*- from base64 import * flag = open("code.txt").readline() # 读取密文 for ...
- Mysql Replace语句的使用
Mysql Replace语句的语法: REPLACE [LOW_PRIORITY | DELAYED] [INTO] tbl_name [(col_name,...)] VALUES ({expr ...