###### tags: `影像處理` # *2022/05/24 影像處理 HW10* ## Coding Part ### 實戰(2) #### 題目: ![](https://i.imgur.com/SzD2cOK.png) ``` #實戰(2) from IPython.core.pylabtools import figsize import numpy as np import cv2 import math from numpy.random import * from matplotlib import pyplot as plt from google.colab import drive from google.colab.patches import * drive.mount('/content/drive') img = cv2.imread("/content/drive/My Drive/Colab Notebooks/imgs/IMG_3643.JPG",-1) print(img.shape) img1 = cv2.cvtColor(img,cv2.COLOR_BGR2RGB) img = cv2.cvtColor(img1,cv2.COLOR_BGR2GRAY) new_img1 = cv2.applyColorMap(img,cv2.COLORMAP_AUTUMN) new_img2 = cv2.applyColorMap(img,cv2.COLORMAP_JET) new_img3 = cv2.applyColorMap(img,cv2.COLORMAP_BONE) plt.figure(figsize=(20,5)) titles = ['Original','Gray','Autumn','Jet','Bone'] plt.subplot(151) plt.title(titles[0],fontsize=15,color='r') plt.imshow(img1) plt.subplot(152) plt.title(titles[1],fontsize=15,color='r') plt.imshow(img,cmap='gray') plt.subplot(153) plt.title(titles[2],fontsize=15,color='r') plt.imshow(new_img1) plt.subplot(154) plt.title(titles[3],fontsize=15,color='r') plt.imshow(new_img2) plt.subplot(155) plt.title(titles[4],fontsize=15,color='r') plt.imshow(new_img3) plt.tight_layout() plt.show() ``` ##### 輸出結果: ![](https://i.imgur.com/kb8Yr37.png) ### 實戰(3) #### 題目: ![](https://i.imgur.com/0GduBF4.png) ``` #實戰(2) from IPython.core.pylabtools import figsize import numpy as np import cv2 import math from numpy.random import * from matplotlib import pyplot as plt from google.colab import drive from google.colab.patches import * drive.mount('/content/drive') img = cv2.imread("/content/drive/My Drive/Colab Notebooks/imgs/IMG_3644.JPG",-1) img1 = cv2.cvtColor(img,cv2.COLOR_BGR2RGB) #色彩校正函數 def RGB_gamma_correction(f,channel,gamma): g = f.copy() nr,nc = f.shape[:2] c = 255.0/(255.0**gamma) table = np.zeros(256) for i in range(256): table[i] = round(i**gamma*c,0) if channel ==1: k=2 elif channel ==2: k=1 else: k=0 for x in range(nr): for y in range(nc): g[x,y,k]=table[f[x,y,k]] return g #直方圖校正 def RGB_histogram_equalization(f): g=f.copy() for k in range(3): g[:,:,k]=cv2.equalizeHist(f[:,:,k]) return g new_img1 = RGB_gamma_correction(img1,1,0.5) new_img2 = RGB_gamma_correction(img1,2,0.5) new_img3 = RGB_gamma_correction(img1,3,0.5) new_img4 = RGB_histogram_equalization(img1) new_img5 = cv2.GaussianBlur(img1,(5,5),0) plt.figure(figsize=(15,15)) titles = ['Original','R Channel(r=0.5)','G Channel(r=0.5)','B Channel(r=0.5)','Histogram_equalization','Gaussian Filter'] plt.subplot(3,3,1) plt.title(titles[0],fontsize=15,color='r') plt.imshow(img1) plt.subplot(3,3,4) plt.title(titles[1],fontsize=15,color='r') plt.imshow(new_img1) plt.subplot(3,3,5) plt.title(titles[2],fontsize=15,color='r') plt.imshow(new_img2) plt.subplot(3,3,6) plt.title(titles[3],fontsize=15,color='r') plt.imshow(new_img3) plt.subplot(3,3,7) plt.title(titles[4],fontsize=15,color='r') plt.imshow(new_img4) plt.subplot(3,3,8) plt.title(titles[5],fontsize=15,color='r') plt.imshow(new_img5) plt.tight_layout() plt.show() ``` ##### 輸出結果: ![](https://i.imgur.com/qQGe0aD.png) ### 實戰(4) #### 題目: ![](https://i.imgur.com/iCA5fxC.png) ``` #實戰(3) from IPython.core.pylabtools import figsize import numpy as np import cv2 import math from numpy.random import * from matplotlib import pyplot as plt from google.colab import drive from google.colab.patches import * drive.mount('/content/drive') img = cv2.imread("/content/drive/My Drive/Colab Notebooks/imgs/IMG_3646.JPG",-1) img1 = cv2.cvtColor(img,cv2.COLOR_BGR2RGB) def RGB_to_HSI(R,G,B): r = R/255 g = G/255 b = B/255 if R==G and G==B: H = -1.0 S = 0.0 I = (r+g+b)/3 else: x = (0.5*((r-g)+(r-b)))/np.sqrt((r-g)**2+(r-b)*(g-b)) if x < -1.0: x = -1.0 if x > 1.0: x = 1.0 theta = np.arccos(x)*180/np.pi if B <= G: H = theta else: H = 360.0 - theta S = 1.0-3.0/(r+g+b)*min(r,g,b) I = (r+g+b)/3 return H,S,I def HSI_to_RGB(H,S,I): if H==-1.0: r = I g = I b = I elif H>=0 and H <120: HH = H b = I*(1-S) r = I*(1+(S*np.cos(HH*np.pi/180))/np.cos((60-HH)*np.pi/180)) g = 3.0*I-(r+b) elif H>=120 and H <240: HH = H - 120.0 r = I*(1-S) g = I*(1+(S*np.cos(HH*np.pi/180))/np.cos((60-HH)*np.pi/180)) b = 3*I-(r+g) else: HH = H - 240 g = I*(1 - S) b = I * (1+(S*np.cos(HH*np.pi/180))/np.cos((60-HH)*np.pi/180)) r = 3 * I-(g+b) rr = round(r*255) gg = round(g*255) bb = round(b*255) R = np.uint8(np.clip(rr,0,255)) G = np.uint8(np.clip(gg,0,255)) B = np.uint8(np.clip(bb,0,255)) return R,G,B def HSI_processing(f,angle = 0,saturation = 100,intensity = 100): g = f.copy() nr,nc = f.shape[:2] for x in range(nr): for y in range(nc): H,S,I = RGB_to_HSI(f[x,y,2],f[x,y,1],f[x,y,0]) H = H+angle if H>360: H = H-360 S = S*saturation/100 I = I*intensity/100 R,G,B = HSI_to_RGB(H,S,I) g[x,y,0] = B g[x,y,1] = G g[x,y,2] = R return g new_img1 = HSI_processing(img1,180,100,100) new_img2 = HSI_processing(img1,0,50,100) new_img3 = HSI_processing(img1,0,100,50) new_img4 = HSI_processing(img1,0,100,100) plt.figure(figsize=(20,5)) titles = ['Original','Hue 180','Saturation 50%','Intensity 50%','Saturation 100% && Intensity 100%'] plt.subplot(151) plt.title(titles[0],fontsize=15,color='r') plt.imshow(img1) plt.subplot(152) plt.title(titles[1],fontsize=15,color='r') plt.imshow(new_img1) plt.subplot(153) plt.title(titles[2],fontsize=15,color='r') plt.imshow(new_img2) plt.subplot(154) plt.title(titles[3],fontsize=15,color='r') plt.imshow(new_img3) plt.subplot(155) plt.title(titles[4],fontsize=15,color='r') plt.imshow(new_img4) plt.tight_layout() plt.show() ``` ##### 輸出結果: ![](https://i.imgur.com/74SuDYo.png) ### 實戰(5) #### 題目: ![](https://i.imgur.com/ggkEqrx.png) ![](https://i.imgur.com/xLYX8qx.png) ``` from IPython.core.pylabtools import figsize import numpy as np import cv2 import math from numpy.random import * from matplotlib import pyplot as plt from google.colab import drive from google.colab.patches import * drive.mount('/content/drive') img = cv2.imread("/content/drive/My Drive/Colab Notebooks/imgs/Baboon.bmp",-1) img1 = cv2.cvtColor(img,cv2.COLOR_BGR2RGB) img2 = cv2.cvtColor(img,cv2.COLOR_BGR2HSV) img3 = cv2.imread("/content/drive/My Drive/Colab Notebooks/imgs/Jenny.bmp",-1) img4 = cv2.cvtColor(img3,cv2.COLOR_BGR2RGB) img5 = cv2.cvtColor(img3,cv2.COLOR_BGR2HSV) def IMG_Process(f1,f2): hsv = f1.copy() nr,nc = f1.shape[:2] hsv = cv2.cvtColor(f1,cv2.COLOR_BGR2HSV) for x in range(nr): for y in range(nc): hsv[x,y,0] = f1[x,y,0] hsv[x,y,1] = f2[x,y,1] hsv[x,y,2] = f2[x,y,2] hsv = cv2.cvtColor(hsv,cv2.COLOR_HSV2BGR) return hsv output = IMG_Process(img5,img2) output2 = IMG_Process(img2,img5) plt.figure(figsize=(15,15)) titles = ['Baboon Original','Baboon Output','Jenny Original','Jenny Output'] plt.subplot(2,2,1) plt.title(titles[0],fontsize=15,color='r') plt.imshow(img1) plt.subplot(2,2,2) plt.title(titles[1],fontsize=15,color='r') plt.imshow(output) plt.subplot(2,2,3) plt.title(titles[2],fontsize=15,color='r') plt.imshow(img4) plt.subplot(2,2,4) plt.title(titles[3],fontsize=15,color='r') plt.imshow(output2) plt.tight_layout() plt.show() ``` ##### 輸出結果: ![](https://i.imgur.com/ZNw9fAW.jpg) ## 簡答題: ![](https://i.imgur.com/9Hdf6jR.png) ##### a. * 身體部分變為**黑色**,因為紅色是M+Y,青色是C,所以C+M+Y根據調色盤是黑色,以下為Adobe Color官方調色盤工具驗證 ![](https://i.imgur.com/pLfjvhH.png) * 鈴鐺會變為**紅色**,因為紅色本身就是M+Y,所以再加黃色仍舊是紅色,以下是Adobe Color官方調色工具驗證 ![](https://i.imgur.com/8CSCkNC.png) ##### b. * H, S, I = 60, 0.8, 0.7:因為360除以6得60,0為紅色,60為黃色,而S、I是飽和度與強度,不影響色調,所以答案是**黃色** * H, S, I = 120, 0.8, 0.8:承上題,120是**綠色** * H, S, I = 240, 0.8, 0.6:承上題,240是**藍色** ![](https://i.imgur.com/8yKGfaY.png) ##### c. 影像濾波套用在色彩影像,是將RGB三通道視為**獨立**的數位影像,再分別套用影像濾波技術,例如平均濾波、高斯慮波