图像缩放之最近邻插值

基于OpenCV的代码:

#include "stdafx.h"
#include<cv.h>
#include<highgui.h>


void main(int argc, char * argv[])
{
	
	printf("%s\n",argv[1]);
	
	IplImage *pSrcGrayImg = cvLoadImage(argv[1],CV_LOAD_IMAGE_GRAYSCALE);
	if(!pSrcGrayImg)
	{
		printf("Load Image failed!\n");
		return ;
	}

    int dstImgHeight,dstImgWidth;
	//scanf("%d%d",&dstImgHeight,&dstImgWidth);
	dstImgHeight = 500;
	dstImgWidth = 500;
	IplImage *pDstGrayImg = cvCreateImage(cvSize(dstImgWidth,dstImgHeight),pSrcGrayImg->depth,1);

	int srcImgHeight,srcImgWidth;
	srcImgHeight = pSrcGrayImg->height;
	srcImgWidth = pSrcGrayImg->width;

	float heightRatio = (float)srcImgHeight/dstImgHeight;
	float widthRatio = (float)srcImgWidth/dstImgWidth;

	unsigned char *srcImgData = (unsigned char *)pSrcGrayImg->imageData;
	unsigned char *dstImgData = (unsigned char *)pDstGrayImg->imageData;
	int srcWidthStep = pSrcGrayImg->widthStep;
	int dstWidthStep = pDstGrayImg->widthStep;

	for (int i=0;i<dstImgHeight;i++)
	{
		int tempHeight = int(i*heightRatio+0.5); //目标图像中i在原图像中对应的tempHeight
		if (tempHeight>=srcImgHeight)
		{
			tempHeight = srcImgHeight-1;
		}
		for (int j=0;j<dstImgWidth;j++)
		{
			int tempWidth = int(j*widthRatio+0.5);
			if (tempWidth>=srcImgWidth)
			{
				tempWidth = srcImgWidth-1;
			}
			dstImgData[i*dstWidthStep+j]=srcImgData[tempHeight*srcWidthStep+tempWidth];
		}
	}

	cvNamedWindow("SrcImage",CV_WINDOW_AUTOSIZE);
	cvNamedWindow("DstImage",CV_WINDOW_AUTOSIZE);
	// 显示图像pSrc
	cvShowImage("SrcImage",pSrcGrayImg);
	cvShowImage("DstImage",pDstGrayImg);
	cvWaitKey(0);
	cvDestroyAllWindows();
	cvReleaseImage(&pSrcGrayImg);
	cvReleaseImage(&pDstGrayImg);
	
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值