Open CV缩放图像
缩放图像是图像处理中需要经常使用的操作。太小的图像在图像识别中不能很好的处理,需要将其放大,太大的图像不方便储存,需要将其缩小,下面记录OpenCV图片缩放方法。
缩放函数
void resize(InputArray src, OutputArray dst, Size dsize, double fx=0, double fy=0, int interpolation=INTER_LINEAR ) 参数说明: src – 原图像 dst – 输出图像 dsize – 输出图像大小,如果为0 则通过此公式计算得到:Size(round(fx*src.cols), round(fy*src.rows)),dsize和fx,fy不能全为空 fx – x方向缩放比例 fy – y方向缩放比例 interpolation – 插值方法: – INTER_NEAREST - a nearest-neighbor interpolation – INTER_LINEAR - a bilinear interpolation (used by default) – INTER_AREA - resampling using pixel area relation. It may be a preferred method for image decimation, as it gives moire’-free results. But when the image is zoomed, it is similar to the INTER_NEAREST method. – INTER_CUBIC - a bicubic interpolation over 4x4 pixel neighborhood – INTER_LANCZOS4 - a Lanczos interpolation over 8x8 pixel neighborhood
使用示例(放大图像)
下面代码演示使用不同的缩放方法放大同一张图片的结果。
int main( int argc, char** argv ) { Mat matSrc = imread("oripic.jpg",1); Mat matDst_INTER_NEAREST,matDst_INTER_LINEAR,matDst_INTER_AREA,matDst_INTER_CUBIC,matDst_INTER_LANCZOS4; //原图使用不同的方法放大5倍后的结果 resize(matSrc,matDst_INTER_NEAREST,Size(0,0),5,5,INTER_NEAREST); resize(matSrc,matDst_INTER_LINEAR,Size(0,0),5,5,INTER_LINEAR); resize(matSrc,matDst_INTER_AREA,Size(0,0),5,5,INTER_AREA); resize(matSrc,matDst_INTER_CUBIC,Size(0,0),5,5,INTER_CUBIC); resize(matSrc,matDst_INTER_LANCZOS4,Size(0,0),5,5,INTER_LANCZOS4); namedWindow("OriPicture"); imshow("OriPicture",matSrc); namedWindow("INTER_NEAREST"); imshow("INTER_NEAREST",matDst_INTER_NEAREST); namedWindow("INTER_LINEAR"); imshow("INTER_LINEAR",matDst_INTER_LINEAR); namedWindow("INTER_AREA"); imshow("INTER_AREA",matDst_INTER_AREA); namedWindow("INTER_CUBIC"); imshow("INTER_CUBIC",matDst_INTER_CUBIC); namedWindow("INTER_LANCZOS4"); imshow("INTER_LANCZOS4",matDst_INTER_LANCZOS4); waitKey(0); }
放大结果:
使用示例(缩小图像)
下面代码将指定图片缩小为当前的0.3
int main( int argc, char** argv ) { Mat matSrc = imread("oripic1.png",1); Mat matDst_INTER_NEAREST,matDst_INTER_LINEAR,matDst_INTER_AREA,matDst_INTER_CUBIC,matDst_INTER_LANCZOS4; //原图使用不同的方法放大5倍后的结果 resize(matSrc,matDst_INTER_NEAREST,Size(0,0),0.3,0.3,INTER_NEAREST); resize(matSrc,matDst_INTER_LINEAR,Size(0,0),0.3,0.3,INTER_LINEAR); resize(matSrc,matDst_INTER_AREA,Size(0,0),0.3,0.3,INTER_AREA); resize(matSrc,matDst_INTER_CUBIC,Size(0,0),0.3,0.3,INTER_CUBIC); resize(matSrc,matDst_INTER_LANCZOS4,Size(0,0),0.3,0.3,INTER_LANCZOS4); namedWindow("OriPicture",WINDOW_AUTOSIZE); imshow("OriPicture",matSrc); namedWindow("INTER_NEAREST",WINDOW_AUTOSIZE); imshow("INTER_NEAREST",matDst_INTER_NEAREST); namedWindow("INTER_LINEAR"); imshow("INTER_LINEAR",matDst_INTER_LINEAR); namedWindow("INTER_AREA"); imshow("INTER_AREA",matDst_INTER_AREA); namedWindow("INTER_CUBIC"); imshow("INTER_CUBIC",matDst_INTER_CUBIC); namedWindow("INTER_LANCZOS4"); imshow("INTER_LANCZOS4",matDst_INTER_LANCZOS4); waitKey(0); }缩小效果(方法名没有在图像中显示,可以自行运行代码查看效果)