Image scaling / interpolation

Image scaling an enlarged, reduced, non-geometric scaling and geometric scaling of several
CV2 Import 
IMG = cv2.imread ( '../ IMG / zidan.jpg')
imgInfo = img.shape
Print (imgInfo)
height = imgInfo [0]
width = imgInfo [. 1]
# image scaling an enlarged, reduced, geometric scaling and non-proportional scaling of several
dstHeight = int (0.5 * height)
dstWidth = int (0.5 * width)
dstImg = cv2.resize (IMG, (dstWidth, dstHeight))
cv2.imshow ( 'double-scaling' dstImg)
cv2.waitKey (0)
renderings:

 

 Interpolation methods: nearest neighbor interpolation pixel relationship resampling cubic interpolation bilinear interpolation (default)

 

 

Target image x-coordinate of a point corresponding to the original image coordinate = x * coordinate of the target image (the width of the original width of the picture / target picture)

Bilinear interpolation : When a certain pixel in the target image corresponding to the original image pixel value is a decimal;

as the picture shows:

A1 = A2 = 0.2 * 15 + 16 * 0.8

B1 = B2 = 22*0.3 + 23*0.7

The final value = A1 * 0.3 + A2 * 0.7 or B1 * 0.2 + B2 * 0.8

Zoom the picture by bilinear interpolation:

import cv2
import numpy as np
img = cv2.imread('../img/zidan.jpg',1)
imgInfo = img.shape
print(imgInfo)
width = imgInfo[0]
height = imgInfo[1]
dstHeight = int(height*0.5)
dstWidth = int(width*0.5)
newImg = np.zeros((dstWidth,dstHeight,3),np.uint8)
for i in range(0,dstWidth):
for j in range(0,dstHeight):
newWidth = int(i*(width*1.0/dstWidth))
newHeight = int(j*(height*1.0/dstHeight))
newImg[i,j] = img[newWidth,newHeight]
cv2.imshow('newimg',newImg)
cv2.waitKey(0)

效果图:

 

Guess you like

Origin www.cnblogs.com/cxxBoo/p/11447558.html