目录
- 基本显示
- 平移
- 缩放
- 旋转
- gray-value transform
- 彩色图像的缩放
- transform stop
- histogram
import cv2
import numpy as np
from matplotlib import pyplot as plt
基本显示
img=cv2.imread('../image/lena-gray.png',-1)
cv2.namedWindow('input_image',cv2.WINDOW_AUTOSIZE)
cv2.imshow('input_imgage',img)
print('image size:',img.shape)
print(img.dtype)
print(img[9][10])
image size: (512, 512)
uint8
156
平移
a = 100
b = 50
img1 = np.zeros((img.shape))
for i in range(img.shape[0]):
for j in range(img.shape[1]):
if i+a<img1.shape[0] and j+b<img1.shape[1]:
img1[i+a][j+b] = img[i][j]
plt.figure()
plt.subplot(121),plt.imshow(img,cmap = 'gray')
plt.title('input Image'), plt.xticks([]), plt.yticks([])
plt.subplot(122),plt.imshow(img1, cmap = 'gray')
plt.title('translate Image'), plt.xticks([]), plt.yticks([])
plt.show()
缩放
a = 2
b = 2
img1 = np.zeros(img.shape)
for i in range(img.shape[0]):
for j in range(img.shape[1]):
if(i*a<img1.shape[0] and j*b<img1.shape[1]):
img1[i*a][i*b] = img[i][j]
plt.figure()
plt.subplot(121),plt.imshow(img,cmap = 'gray')
plt.title('input Image'), plt.xticks([]), plt.yticks([])
plt.subplot(122),plt.imshow(img1, cmap = 'gray')
plt.title('translate Image'), plt.xticks([]), plt.yticks([])
plt.show()
旋转
img3 = np.zeros((img.shape[0],img.shape[1]))
for i in range(img.shape[0]):
for j in range(img.shape[1]):
if img.shape[0]-i< img.shape[0]:
img3[img.shape[0]-i][j] = img[i][j]
plt.subplot(121),plt.imshow(img,cmap = 'gray')
plt.title('input Image'), plt.xticks([]), plt.yticks([])
plt.subplot(122),plt.imshow(img3, cmap = 'gray')
plt.title('flip Image'), plt.xticks([]), plt.yticks([])
plt.show()
gray-value transform
- if value>mid then value=1, else value=0
img1 = np.array(img)
mid = 0
sum = 0
for i in range(img.shape[0]):
for j in range(img.shape[1]):
sum = sum + img[i][j]
mid = sum/(img.size)
for i in range(img.shape[0]):
for j in range(img.shape[1]):
if img[i][j]>mid:
img1[i][j]=1
else:
img1[i][j]=0
plt.subplot(121),plt.imshow(img,cmap = 'gray')
plt.title('input Image'), plt.xticks([]), plt.yticks([])
plt.subplot(122),plt.imshow(img1, cmap = 'gray')
plt.title('gray-value Image'), plt.xticks([]), plt.yticks([])
plt.show()
彩色图像的缩放
img = cv2.imread('../image/lena.jpg',-1)
a = 0.5
b = 2
img2 = np.zeros((round(img.shape[0]*a),round(img.shape[1]*b),3),dtype='uint8')
print(img2.shape)
for i in range(img2.shape[0]):
for j in range(img2.shape[1]):
if round(i/a) < img.shape[0] and round(j/b) < img.shape[1]:
img2[i][j] = img[round(i/a)][round(j/b)]
b,g,r = cv2.split(img)
img1 = cv2.merge([r, g, b])
b,g,r = cv2.split(img2)
img2 = cv2.merge([r, g, b])
plt.subplot(121),plt.imshow(img1)
plt.title('input Image'), plt.xticks([]), plt.yticks([])
plt.subplot(122),plt.imshow(img2)
plt.title('zoom Image'), plt.xticks([]), plt.yticks([])
plt.show()
(320, 600, 3)
transform stop
histogram
hist = np.zeros(256)
for i in range(img.shape[0]):
for j in range(img.shape[1]):
hist[img[i][j]]=hist[img[i][j]]+1
plt.subplot(121),plt.imshow(img,cmap = 'gray')
plt.title('input Image'), plt.xticks([]), plt.yticks([])
plt.subplot(122),plt.plot(hist)
plt.title("Grayscale Histogram"),plt.xlabel("Bins")
plt.ylabel("# of Pixels"),plt.xlim([0,256])
plt.show()