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)
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
- 120
- 121
- 122
- 123
- 124
- 125
- 126
- 127
- 128
- 129
- 130
- 131
- 132
- 133
- 134
- 135
- 136
- 137
- 138
- 139
- 140
- 141
- 142
- 143
- 144
- 145
- 146
- 147
- 148
- 149
- 150
- 151
- 152
- 153
- 154
- 155
- 156
- 157
- 158
- 159
- 160
- 161
- 162
- 163
- 164
- 165
- 166
- 167
- 168
- 169
- 170
- 171
- 172
- 173
- 174
- 175
- 176
- 177
- 178
- 179
- 180
- 181
- 182
- 183
- 184
- 185
- 186
- 187
- 188
- 189
- 190
- 191
- 192
- 193
- 194
- 195
- 196
- 197
- 198
- 199
- 200
- 201
- 202
- 203
- 204
- 205
- 206
- 207
- 208
- 209
- 210
- 211
- 212
- 213
- 214
- 215
- 216
- 217
- 218
- 219
- 220
- 221
- 222
- 223
- 224
- 225
- 226
- 227
- 228
- 229
- 230
- 231
- 232
- 233
- 234
- 235
- 236
- 237
- 238
- 239
- 240
- 241
- 242
- 243
- 244
- 245
- 246
- 247
- 248
- 249
- 250
- 251
- 252
- 253
- 254
- 255
- 256
- 257
- 258
- 259
- 260
- 261
- 262
- 263
- 264
- 265
- 266
- 267
- 268
- 269
- 270
- 271
- 272
- 273
- 274
- 275
- 276
- 277
- 278
- 279
- 280
- 281
- 282
- 283
- 284
- 285
- 286
- 287
- 288
- 289
- 290
- 291
- 292
- 293
- 294
- 295
- 296
- 297
- 298
- 299
- 300
- 301
- 302
- 303
- 304
- 305
- 306
- 307
- 308
- 309
- 310
- 311
- 312
- 313
- 314
- 315
- 316
- 317
- 318
- 319
- 320
- 321
- 322
- 323
- 324
- 325
- 326
- 327
- 328
- 329
- 330
- 331
- 332
- 333
- 334
- 335
- 336
- 337
- 338
- 339
- 340
- 341
- 342
- 343
- 344
- 345
- 346
- 347
- 348
- 349
- 350
- 351
- 352
- 353
- 354
- 355
- 356
- 357
- 358
- 359
- 360
- 361
- 362
- 363
- 364
- 365
- 366
- 367
- 368
- 369
- 370
- 371
- 372
- 373
- 374
- 375
- 376
- 377
- 378
- 379
- 380
- 381
- 382
- 383
- 384
- 385
- 386
- 387
- 388
- 389
- 390
- 391
- 392
- 393
- 394
- 395
- 396
- 397
- 398
- 399
- 400
- 401
- 402
- 403
- 404
- 405
- 406
- 407
- 408
- 409
- 410
- 411
- 412
- 413
- 414
- 415
- 416
- 417
- 418
- 419
- 420
- 421
- 422
- 423
- 424
- 425
- 426
- 427
- 428
- 429
- 430
- 431
- 432
- 433
- 434
- 435
- 436
- 437
- 438
- 439
- 440
- 441
- 442
- 443
- 444
- 445
- 446
- 447
- 448
- 449
- 450
- 451
- 452