openCV中插值cv2.resize——python\C++

openCV中插值算法

void resize(InputArray src, OutputArray dst, Size dsize, double fx=0, double fy=0, int interpolation=INTER_LINEAR)

  1. src 输入图
  2. dst 输出图
  3. dsize 输出尺寸
  4. fx 水平缩放比例
  5. fy 垂直缩放比例
  6. interpolation 内插方式:(INTER_NEAREST:最邻近插值,INTER_LINEAR双线性插值,INTER_AREA邻域像素再取样插补,INTER_CUBIC双立方插补 )

通常,缩小使用cv.INTER_AREA,放缩使用cv.INTER_CUBIC(较慢)和cv.INTER_LINEAR(较快效果也不错)。默认情况下,所有的放缩都使用cv.INTER_LINEAR。

C++

#include
#include 

using namespace cv;
using namespace std;

void main()
{
	Mat img = imread("D:\\男人.png");
	if (img.empty())
	{
		printf("Could not find the image!\n");
		return ;
	}
	//读取图片高度和宽度
	int height = img.rows;
	int width = img.cols;
	//对图像的size进行修改为dsize
	Size dsize = Size(round(0.2 * width), round(0.2 * height));
	//创建对象作为output,用于插值处理
	Mat Nearnest_shrink;
	Mat Liner_shrink;
	//使用最近邻插值
	resize(img, Nearnest_shrink, dsize, 0, 0, INTER_NEAREST);
	//使用双线性插值
	resize(img, Liner_shrink, dsize, 0, 0, INTER_LINEAR);


	// 在缩小图像的基础上,放大图像,比例为(1.5, 1.5)
	float fx = 1.5;
	float fy = 1.5;
	Mat enlarge1, enlarge2;
	resize(Nearnest_shrink, enlarge1, Size(), fx, fy, INTER_NEAREST);
	resize(Liner_shrink, enlarge2, Size(), fx, fy, INTER_LINEAR);

	// 显示
	imshow("src", img);
	imshow("Nearnest_shrink", Nearnest_shrink);
	imshow("Liner_shrink", Liner_shrink);

	imshow("INTER_NEAREST", enlarge1);
	imshow("INTER_LINEAR", enlarge2);
	waitKey(0);


}

可以看到效果
openCV中插值cv2.resize——python\C++_第1张图片
 
 
 

python

import cv2
#"IMREAD_UNCHANGED"指定用图片的原来格式打开
#"IMREAD_GRAYSCALE"指定用灰度图像的方式打开图片,即将原始图像转化为灰度图像再打开
#"IMREAD_COLOR"指定用彩色图像打开图片
#以上也可以用整数1 0 -1 代替
img = cv2.imread("D:/man.png", cv2.IMREAD_UNCHANGED)
print('Original Dimensions : ', img.shape)

##缩放比例
scale_percent = 0.2
width = int(img.shape[1] * scale_percent)
height = int(img.shape[0] * scale_percent )
dim = (width, height)

# resize image
resized = cv2.resize(img, dim, interpolation=cv2.INTER_LINEAR)

fx = 1.5
fy = 1.5

resized1 = cv2.resize(resized, dsize=None, fx=fx, fy=fy, interpolation=cv2.INTER_NEAREST)

resized2 = cv2.resize(resized, dsize=None, fx=fx, fy=fy, interpolation=cv2.INTER_LINEAR)
print('Resized Dimensions : ', resized.shape)

cv2.imshow("Resized image", resized)
cv2.imshow("INTER_NEAREST image", resized1)
cv2.imshow("INTER_LINEAR image", resized2)
cv2.waitKey(0)
cv2.destroyAllWindows()

两个语言的效果是一致的

你可能感兴趣的:(CV,opencv,计算机视觉,python,图像识别)