图像的基础知识
1.灰度量化级
import cv2
import numpy as np
import matplotlib.pyplot as plt
imag = cv2.imread("cameraman.tif", 0) #直接读取为灰度图像
heigth=imag.shape[0]
width=imag.shape[1] #图片尺寸
print('heigth=%f\nwidth=%f\n'%(heigth,width))
title = ["1","2","3","4","5","6"]
for i in range(1,6):
figure = imag[::(pow(2,i)),::pow(2,i)]
plt.subplot(2, 3, i)
plt.imshow(figure)
plt.title(title[i-1])
print(figure.shape)
plt.subplot(2, 3, 6)
plt.imshow(imag)
plt.title(6)
plt.show()
2.降采样
import cv2
import numpy as np
import matplotlib.pyplot as plt
imag = cv2.imread("cameraman.tif", 0) #直接读取为灰度图像
heigth=imag.shape[0]
width=imag.shape[1] #图片尺寸
figure = np.zeros([heigth,width],dtype=np.uint8)
print((figure.shape))
print('heigth=%f\nwidth=%f\n'%(heigth,width))
for i in range(1,6):
f = 256/(pow(2,i))
for j in range(0,heigth):
for k in range(0,width):
figure[j,k] = np.uint8(imag[j,k]/f)*f
plt.subplot(2,3,i)
plt.imshow(figure,cmap='gray')
plt.subplot(2,3,6)
plt.imshow(imag,cmap='gray')
plt.show()
温馨提示
图片相应路径问题可自行更正