您的位置:首页 > 编程语言 > Qt开发

图像缩放实现(Qt)

2016-06-30 16:46 375 查看
1、自己算法实现:

近邻插值,根据坐标变换(由newX得到X),然后在原图(x,y)找到像素点值确定(newX,newY)。

结果:



2、Qt的QImage函数成员:

scaled,查询help即可。

结果:



3、结果比对:

QT实现的方法明显不一样。目前来看,他的效果要好一些。当然,除了近邻插值,还有双线性等一些其他改进算法。

4、代码:

我在实现过程中,使用了对话框来确定缩放实现模式,mode=0代表自己写的,mode=1代表QT的。

if(mode==0)
{
int  heightOut,widthOut,coordinateX=0,coordinateY=0;
if(k!=0)
{
heightOut=k*img->height();
widthOut=k*img->width();
}
else
{
heightOut=height;
widthOut=width;
k=double(width)/img->width();
}
int lineByteIn=img->bytesPerLine(),lineByteOut=((widthOut*img->depth()+31)/32)*4,
pixelByte=img->depth()/8;
BYTE *pImgIn=img->bits(),*pImgOut=new BYTE[lineByteOut*heightOut];
for(int j=0;j<heightOut;j++)
{
for(int i=0;i<widthOut;i++)
{
//输出坐标(i,j)映射到原图坐标coordinateX,coordinateY
coordinateX=i/k+0.5;    //缩放比列ratioX
coordinateY=j/k+0.5;

//若插值位置在输入图像范围内,则近邻插值
if(0<=coordinateX&&coordinateX<img->width()
&&0<=coordinateY&&coordinateY<img->height())
{
for(int t=0;t<pixelByte;t++)        //每个像素占字节数pixelByte
*(pImgOut+j*lineByteOut+i*pixelByte+t)
=*(pImgIn+coordinateY*lineByteIn+coordinateX*pixelByte+t);
}
//如果不在则赋值255
else
{
for(int t=0;t<pixelByte;t++)
*(pImgOut+j*lineByteOut+i*pixelByte+t)=255;
}
}
}
QImage *imgScaled = new QImage(pImgOut,widthOut,heightOut,
lineByteOut,QImage::Format_ARGB32_Premultiplied);
delete img;
img=imgScaled;
}
else if(mode==1)
{
QImage *imgScaled = new QImage;
if(k!=0)
{
*imgScaled=img->scaled(img->width()*k,img->height()*k,
Qt::KeepAspectRatio);
}
else
{
*imgScaled=img->scaled(width,height,
Qt::KeepAspectRatio);
}
delete img;
img=imgScaled;
}
update();
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  图像缩放 qt