基本的数字图像处理-基于python实现

目录

        • 基本显示
        • 平移
        • 缩放
        • 旋转
        • 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)
# cv2.waitKey(0)
# cv2.destroyAllWindows()
#cv2.imwrite('lena1.png',img)

# print value
print('image size:',img.shape) # 显示尺寸-resolution
print(img.dtype)               # gray-level
print(img[9][10])              # value
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()

基本的数字图像处理-基于python实现_第1张图片

缩放

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()

基本的数字图像处理-基于python实现_第2张图片

旋转

  • flip x1=M-x,y1=y
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()

基本的数字图像处理-基于python实现_第3张图片

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()

基本的数字图像处理-基于python实现_第4张图片

彩色图像的缩放

img = cv2.imread('../image/lena.jpg',-1)
## zoom x1=ax,y1=by
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)]
## the follow two codes is used to solve the problem of plt imshow
#  because of the cv.imread make the image channels is B G R
#  so,need to split channels and merge
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)

基本的数字图像处理-基于python实现_第5张图片

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()

基本的数字图像处理-基于python实现_第6张图片

你可能感兴趣的:(数字图像处理)