import cv2
import numpy as np
import matplotlib.pyplot as plt
def Q1(img):
b,g,r = cv2.split(img)
h, w = r.shape
b_1 = img.copy()
g_1 = img.copy()
r_1 = img.copy()
for i in range(h):
for j in range(w):
r_1[i][j] = (img[i][j][2],0,0)
g_1[i][j] = (0,img[i][j][1],0)
b_1[i][j] = (0,0,img[i][j][0])
plt.rcParams['font.sans-serif'] = ['SimHei']
plt.subplot(3, 3, 1)
plt.title('原图')
plt.imshow(img[:,:,::-1])
plt.xticks([]), plt.yticks([])
plt.subplot(3, 3, 4)
plt.title('红色_调包')
plt.imshow(r)
plt.xticks([]), plt.yticks([])
plt.subplot(3, 3, 5)
plt.title('绿色_调包')
plt.imshow(g)
plt.xticks([]), plt.yticks([])
plt.subplot(3, 3, 6)
plt.title('蓝色_调包')
plt.imshow(b)
plt.xticks([]), plt.yticks([])
plt.subplot(3, 3, 7)
plt.title('红色_循环实现')
plt.imshow(r_1[:,:,:])
plt.xticks([]), plt.yticks([])
plt.subplot(3, 3, 8)
plt.title('绿色_循环实现')
plt.imshow(g_1[:,:,:])
plt.xticks([]), plt.yticks([])
plt.subplot(3, 3, 9)
plt.title('蓝色_循环实现')
plt.imshow(b_1[:,:,:])
plt.xticks([]), plt.yticks([])
plt.show()
def Q2(img):
img_new = img.copy()
h = np.shape(img)[0]
w = np.shape(img)[1]
B, G, R = cv2.split(img)
[B, G, R] = [i/255.0+0.001 for i in ([B, G, R])]
I = (R + G + B) / 3.0
H = np.zeros((h, w))
for i in range(h):
den = np.sqrt((R[i] - G[i]) ** 2 + (R[i] - B[i]) * (G[i] - B[i]))+0.001
thetha = np.arccos(0.5 * (R[i] - B[i] + R[i] - G[i]) / den)
temp = np.zeros(w)
temp[B[i] <= G[i]] = thetha[B[i] <= G[i]]
temp[G[i] < B[i]] = 2 * np.pi - thetha[G[i] < B[i]]
temp[den == 0] = 0
H[i] = temp / (2 * np.pi)
S = np.zeros((h, w))
for i in range(h):
min = []
for j in range(w):
arr = [B[i][j], G[i][j], R[i][j]]
min.append(np.min(arr))
min = np.array(min)
S[i] = 1 - (min * 3 / (R[i] + B[i] + G[i]))
S[i][R[i] + B[i] + G[i] == 0] = 0
img_new[:, :, 0] = H * 255
img_new[:, :, 1] = S * 255
img_new[:, :, 2] = I * 255
plt.rcParams['font.sans-serif'] = ['SimHei']
plt.subplot(1, 2, 1)
plt.title('原图')
plt.imshow(img[:,:,::-1])
plt.subplot(1, 2, 2)
plt.title('HSI图像')
plt.imshow(img_new[:,:,:])
plt.show()
def leiji(img):
x = [0] * 256
y = [0] * 256
prob = [0] * 256
for i in range(256):
x[i] = i
y[i] = 0
for rv in img:
for cv in rv:
y[cv] += 1
h, w = img.shape
for i in range(256):
prob[i] = y[i] / (h * w)
prob_sum = [0] * 256
prob_sum[0] = prob[0]
for i in range(1, 256):
prob_sum[i] = prob_sum[i - 1] + prob[i]
return prob_sum
def Q3(img1,img2):
gray_img1 = cv2.cvtColor(img1, cv2.COLOR_BGR2GRAY)
gray_img2 = cv2.cvtColor(img2, cv2.COLOR_BGR2GRAY)
x = range(256)
img1_leiji = leiji(gray_img1)
img2_leiji = leiji(gray_img2)
abs_chazhi = [[0 for i in range(256)] for j in range(256)]
for i in range(256):
for j in range(256):
abs_chazhi[i][j] = abs(img1_leiji[i] - img2_leiji[j])
img_map = [0] * 256
for i in range(256):
zuixiao_n = abs_chazhi[i][0]
index = 0
for j in range(256):
if zuixiao_n > abs_chazhi[i][j]:
zuixiao_n = abs_chazhi[i][j]
index = j
img_map[i] = ([i, index])
h, w = gray_img1.shape
img_new = gray_img1.copy()
for i in range(h):
for j in range(w):
img_new[i, j] = img_map[gray_img1[i, j]][1]
plt.rcParams['font.sans-serif'] = ['SimHei']
plt.subplot(4, 2, 1)
plt.title('原图1')
plt.imshow(img1[:,:,::-1])
plt.subplot(4, 2, 2)
plt.title('原图2')
plt.imshow(img2[:,:,::-1])
plt.subplot(4, 2, 3)
plt.title('灰度图1')
plt.imshow(gray_img1,'gray')
plt.subplot(4, 2, 4)
plt.title('灰度图2')
plt.imshow(gray_img2,'gray')
plt.subplot(4, 2, 5)
plt.title('累积直方图1')
plt.bar(x, img1_leiji, color='orange')
plt.subplot(4, 2, 6)
plt.title('累积直方图2')
plt.bar(x, img2_leiji, color='green')
plt.subplot(4, 2, 7)
plt.title('灰度图1')
plt.imshow(gray_img1,'gray')
plt.subplot(4, 2, 8)
plt.title('规定化后')
plt.imshow(img_new,'gray')
plt.show()
def jiayiquan(h,w,img):
a = []
for i in range(h + 2):
a.append([0] * (w + 2))
for i in range(h):
for j in range(w):
a[i + 1][j + 1] = img[i][j]
a[0][0] = a[1][1]
a[0][w + 1] = a[1][w]
a[h + 1][0] = a[h][1]
a[h + 1][w + 1] = a[h][w]
for i in range(1, h + 1):
a[i][0] = a[i][1]
a[i][w + 1] = a[i][w]
for j in range(1, w + 1):
a[0][j] = a[1][j]
a[h + 1][j] = a[h][j]
return a
def huifu(h,w,img):
a = np.zeros((h,w))
for i in range(h-2):
for j in range(w-2):
a[i][j]=img[i+1][j+1]
return a
def juanji(is_ditong,h,w,img_a,mod):
b = np.zeros((h+2,w+2))
if is_ditong:
ditong_sum = sum(mod)
else:
ditong_sum = 1
for i in range(1,h+1):
for j in range(1,w+1):
c = int((mod[0] * img_a[i-1][j-1] + mod[1] * img_a[i-1][j] + mod[2] * img_a[i-1][j+1] +
mod[3] * img_a[i][j - 1] + mod[4] * img_a[i][j] + mod[5] * img_a[i][j+1] +
mod[6] * img_a[i+1][j-1] + mod[7] * img_a[i+1][j] + mod[8] * img_a[i+1][j+1])/ditong_sum-0.5)
if c < 0:
b[i][j] = 0
elif c > 255:
b[i][j] = 255
else:
b[i][j] = c
b = huifu(h,w,b)
return b
def Q4(img):
img1 = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
h, w = img1.shape
model_d_1 = [1, 1, 1,
1, 1, 1,
1, 1, 1]
model_d_2 = [1, 2, 1,
2, 4, 2,
1, 2, 1]
model_g_1 = [-1, -1, -1,
-1, 9, -1,
-1, -1, -1]
model_g_2 = [-1, -1, -1,
-1, 8, -1,
-1, -1, -1]
a = jiayiquan(h,w,img1)
d1 = juanji(True,h,w,a,model_d_1)
d2 = juanji(True,h,w,a,model_d_2)
g1 = juanji(False,h,w,a,model_g_1)
g2 = juanji(False,h,w,a,model_g_2)
plt.rcParams['font.sans-serif'] = ['SimHei']
plt.subplot(3, 2, 1)
plt.title('原图')
plt.imshow(img[:,:,::-1])
plt.subplot(3, 2, 2)
plt.title('灰度图')
plt.imshow(img1,'gray')
plt.subplot(3, 2, 3)
plt.title('111 111 111滤波')
plt.imshow(d1,'gray')
plt.subplot(3, 2, 4)
plt.title('121 242 121滤波')
plt.imshow(d2, 'gray')
plt.subplot(3, 2, 5)
plt.title('-1绕9滤波')
plt.imshow(g1, 'gray')
plt.subplot(3, 2, 6)
plt.title('-1绕8滤波')
plt.imshow(g2, 'gray')
plt.show()
def Q5(img):
img1 = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
h,w = img1.shape
f = np.fft.fft2(img1)
fimg1 = np.log(np.abs(f))
fshift = np.fft.fftshift(f)
fimg2 = np.log(np.abs(fshift))
half_h = int(h / 2)
half_w = int(w / 2)
fshift1 = fshift.copy()
fshift1[half_h - 30: half_h + 30, half_w - 30: half_w + 30] = 0
fimg_g = np.abs(fshift1)
fimg_g = np.log(fimg_g)
ishift = np.fft.ifftshift(fshift1)
img_new1 = np.fft.ifft2(ishift)
img_new1 = np.abs(img_new1)
fshift2 = fshift.copy()
for i in range(h):
for j in range(w):
if (i<half_h-30 or i >half_h+30) or (j<half_w-30 or j>half_w+30):
fshift2[i][j]=0
fimg_d = np.abs(fshift2)
fimg_d = np.log(fimg_d)
ishift = np.fft.ifftshift(fshift2)
img_new2 = np.fft.ifft2(ishift)
img_new2 = np.abs(img_new2)
plt.rcParams['font.sans-serif'] = ['SimHei']
plt.subplot(2, 4, 1)
plt.title('原图')
plt.imshow(img[:,:,::-1])
plt.subplot(2, 4, 2)
plt.title('灰度图')
plt.imshow(img1,'gray')
plt.subplot(2, 4, 3)
plt.title('频谱图_1')
plt.imshow(fimg1,'gray')
plt.subplot(2, 4, 4)
plt.title('频谱图_2')
plt.imshow(fimg2,'gray')
plt.subplot(2, 4, 5)
plt.title('高通-频谱图')
plt.imshow(fimg_g,'gray')
plt.subplot(2, 4, 6)
plt.title('高通-处理后')
plt.imshow(img_new1,'gray')
plt.subplot(2, 4, 7)
plt.title('低通-频谱图')
plt.imshow(fimg_d,'gray')
plt.subplot(2, 4, 8)
plt.title('低通-处理后')
plt.imshow(img_new2,'gray')
plt.show()
def Q6(img):
img1 = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
gaussian = cv2.GaussianBlur(img1,(3,3),0)
Canny = cv2.Canny(gaussian, 50, 150)
kai = cv2.morphologyEx(Canny, cv2.MORPH_CLOSE, kernel=(3, 3), iterations=3)
bi = cv2.morphologyEx(Canny, cv2.MORPH_OPEN, kernel=(3, 3), iterations=3)
plt.rcParams['font.sans-serif'] = ['SimHei']
plt.subplot(2, 3, 1)
plt.title('原图')
plt.imshow(img[:,:,::-1])
plt.subplot(2, 3, 2)
plt.title('灰度图')
plt.imshow(img1,'gray')
plt.subplot(2, 3, 4)
plt.title('Canny边缘')
plt.imshow(Canny,'gray')
plt.subplot(2, 3, 5)
plt.title('开运算')
plt.imshow(kai,'gray')
plt.subplot(2, 3, 6)
plt.title('闭运算')
plt.imshow(bi,'gray')
plt.show()
img = cv2.imread('b.jpg',1)
Q1(img)
Q2(img)
img1 = cv2.imread('img1.png',1)
img2 = cv2.imread('img2.png',1)
Q3(img1,img2)
Q4(img)
Q5(img)
img3 = cv2.imread('jiaoyan2.png',1)
Q6(img3)