首页 > 代码库 > 图像放大快速算法

图像放大快速算法

图像放大快速算法实现的原理主要有以下两点:

1、横向缩放与纵向缩放分开,而先后顺序取决于缩放前后的图像比例:

   若SrcHeight/DstWidth>SrcWidth/DstWidth,则先纵向缩放,否则横向缩放。

2、利用整数运算代替浮点运算,由于是分步缩放,以横向缩放为例,利用二次线性插值公式,再变形一下如下:

f(i,j+v) = (1-v)f(i,j) +vf(i,j+1);

其中i为第i行,j为第j列,v表示求得放大后坐标的小数部分。

这里最重要的并没有直接用小数运算,而是通过将小数区间(0,1)映射到整数区间(0,128),    

再利用整数来代替小数运算,最后只需要相应的处理一下就可以了。

 

误差分析:

首先二次线性算法本来就有误差,这里主要讨论利用整数代替小数的误差,可以看到每一个整数能代替的小数范围为1/128,所以误差范围为[-1/256,1/256]之间。


代码实现:

//ImageScale.h

#define IN
#define OUT
#define _DWORD int
#define _BYTE byte
#define _WORD short

struct ImageInfo
{
	byte* pRGBData;
	int nHeight;
	int nStepValue;
	int nWidth;
//	int nFlag;
	ImageInfo()
	{
		pRGBData=http://www.mamicode.com/NULL;>
//ImageScale.cpp


#include "StdAfx.h"
#include "ImageScale.h"

short g_pHSDataByte[]=
{
	0x4000, 0x0000, 0x3F80, 0x0080, 0x3F00, 0x0100, 0x3E80, 0x0180,
	0x3E00, 0x0200, 0x3D80, 0x0280, 0x3D00, 0x0300, 0x3C80, 0x0380,
	0x3C00, 0x0400, 0x3B80, 0x0480, 0x3B00, 0x0500, 0x3A80, 0x0580,
	0x3A00, 0x0600, 0x3980, 0x0680, 0x3900, 0x0700, 0x3880, 0x0780,
	0x3800, 0x0800, 0x3780, 0x0880, 0x3700, 0x0900, 0x3680, 0x0980,
	0x3600, 0x0A00, 0x3580, 0x0A80, 0x3500, 0x0B00, 0x3480, 0x0B80,
	0x3400, 0x0C00, 0x3380, 0x0C80, 0x3300, 0x0D00, 0x3280, 0x0D80,
	0x3200, 0x0E00, 0x3180, 0x0E80, 0x3100, 0x0F00, 0x3080, 0x0F80,
	0x3000, 0x1000, 0x2F80, 0x1080, 0x2F00, 0x1100, 0x2E80, 0x1180,
	0x2E00, 0x1200, 0x2D80, 0x1280, 0x2D00, 0x1300, 0x2C80, 0x1380,
	0x2C00, 0x1400, 0x2B80, 0x1480, 0x2B00, 0x1500, 0x2A80, 0x1580,
	0x2A00, 0x1600, 0x2980, 0x1680, 0x2900, 0x1700, 0x2880, 0x1780,
	0x2800, 0x1800, 0x2780, 0x1880, 0x2700, 0x1900, 0x2680, 0x1980,
	0x2600, 0x1A00, 0x2580, 0x1A80, 0x2500, 0x1B00, 0x2480, 0x1B80,
	0x2400, 0x1C00, 0x2380, 0x1C80, 0x2300, 0x1D00, 0x2280, 0x1D80,
	0x2200, 0x1E00, 0x2180, 0x1E80, 0x2100, 0x1F00, 0x2080, 0x1F80,
	0x2000, 0x2000, 0x1F80, 0x2080, 0x1F00, 0x2100, 0x1E80, 0x2180,
	0x1E00, 0x2200, 0x1D80, 0x2280, 0x1D00, 0x2300, 0x1C80, 0x2380,
	0x1C00, 0x2400, 0x1B80, 0x2480, 0x1B00, 0x2500, 0x1A80, 0x2580,
	0x1A00, 0x2600, 0x1980, 0x2680, 0x1900, 0x2700, 0x1880, 0x2780,
	0x1800, 0x2800, 0x1780, 0x2880, 0x1700, 0x2900, 0x1680, 0x2980,
	0x1600, 0x2A00, 0x1580, 0x2A80, 0x1500, 0x2B00, 0x1480, 0x2B80,
	0x1400, 0x2C00, 0x1380, 0x2C80, 0x1300, 0x2D00, 0x1280, 0x2D80,
	0x1200, 0x2E00, 0x1180, 0x2E80, 0x1100, 0x2F00, 0x1080, 0x2F80,
	0x1000, 0x3000, 0x0F80, 0x3080, 0x0F00, 0x3100, 0x0E80, 0x3180,
	0x0E00, 0x3200, 0x0D80, 0x3280, 0x0D00, 0x3300, 0x0C80, 0x3380,
	0x0C00, 0x3400, 0x0B80, 0x3480, 0x0B00, 0x3500, 0x0A80, 0x3580,
	0x0A00, 0x3600, 0x0980, 0x3680, 0x0900, 0x3700, 0x0880, 0x3780,
	0x0800, 0x3800, 0x0780, 0x3880, 0x0700, 0x3900, 0x0680, 0x3980,
	0x0600, 0x3A00, 0x0580, 0x3A80, 0x0500, 0x3B00, 0x0480, 0x3B80,
	0x0400, 0x3C00, 0x0380, 0x3C80, 0x0300, 0x3D00, 0x0280, 0x3D80,
	0x0200, 0x3E00, 0x0180, 0x3E80, 0x0100, 0x3F00, 0x0080, 0x3F80,
	0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000
};


ImageScale::ImageScale(void)
{
}


ImageScale::~ImageScale(void)
{

}

void ImageScale::Run(BYTE* pSrcImage, int nSrcWidth, int nSrcHeight,  BYTE* pTagImage, int nTagWidth, int nTagHeight, int nChannels)
{

	if (!(nChannels==1||nChannels==4))
		return;
	if(nSrcWidth<=0 || nSrcHeight<=0 )
		return;
	if(nTagWidth<nSrcWidth || nTagHeight<nSrcHeight)
		return;
	int nSrcStepValue=http://www.mamicode.com/(nSrcWidth+15) & ~15;>


图像放大快速算法