---
# System prepended metadata

title: '*2022/05/10 影像處理 HW09*'
tags: [影像處理]

---

###### tags: `影像處理`
# *2022/05/10 影像處理 HW09*
## Coding Part
### 實戰(3)
#### 題目：
![](https://i.imgur.com/7a3wQfX.png)
```
import numpy as np
import cv2
import math
from numpy.fft import *
from numpy.random import *
from matplotlib import pyplot as plt
from google.colab import drive
from google.colab.patches import *

drive.mount('/content/drive')
image = cv2.imread("/content/drive/My Drive/Colab Notebooks/imgs/Barbara.bmp",-1)

def gaussian_noise(f,scale):
    g = f.copy()
    nr,nc = f.shape[:2]
    for x in range(nr):
        for y in range(nc):
            value = f[x,y] + normal(0,scale)
            g[x,y] = np.uint8(np.clip(value,0,255))
    return g

def wiener_filtering(f,cutoff,Ｋ):
    nr,nc=f.shape[:2]

    fp = np.zeros([nr,nc])
    for x in range(nr):
        for y in range(nc):
            fp[x,y] = pow(-1,x+y)*f[x,y]

    F = fft2(fp)
    G = F.copy()

    for u in range(nr):
        for v in range(nc):
            dist = np.sqrt((u -nr/2)*(u-nr/2)+(v-nc/2)*(v-nc/2))
            H = np.exp(-(dist*dist)/(2*cutoff*cutoff))
            H = H/(H*H+Ｋ)
            G[u,v]*=H

    gp = ifft2(G)

    gp2 = np.zeros([nr,nc])
    for x in range(nr):
        for y in range(nc):
            gp2[x,y]=round(pow(-1,x+y)*np.real(gp[x,y]),0)
    g = np.uint8(np.clip(gp2,0,255))

    return g

def PSNR(f,g):
    nr,nc = f.shape[:2]
    MSE = 0
    for x in range(nr):
        for y in range(nc):
            MSE += (float(f[x,y])-float(g[x,y]))**2
    MSE /= (nr*nc)
    PSNR = 10 * np.log10((255*255)/MSE)
    return PSNR

image_noise = gaussian_noise(image,15)
image_noise_PSNR = PSNR(image,image_noise)
image_noise_Inverse_1 = wiener_filtering(image_noise,50,0.05)
image_noise_Inverse_2 = wiener_filtering(image_noise,50,0.01)
image_noise_Inverse__1_PSNR = PSNR(image,image_noise_Inverse_1)
image_noise_Inverse__2_PSNR = PSNR(image,image_noise_Inverse_2)

images = [image,image_noise,image_noise_Inverse_1,image_noise_Inverse_2]
titles = ['Original','gaussian_noise scale=15','Wiener (K=0.05)','Wiener (K=0.01)']
labels = ['','PSNR='+str(image_noise_PSNR,),'PSNR='+str(image_noise_Inverse__1_PSNR),'PSNR='+str(image_noise_Inverse__2_PSNR)]
plt.figure(figsize=(15,10))
for i in range(4):
    plt.subplot(1,4,i+1),plt.imshow(images[i],cmap='gray')
    plt.title(titles[i],fontsize=15,color='r')
    plt.xlabel(labels[i],fontsize=15,color='b')
plt.tight_layout()
plt.show()
```
#### 輸出結果：
![](https://i.imgur.com/5mPRZOl.png)

### 實戰(4)

![](https://i.imgur.com/2EOwSuj.png)
#### 程式碼：
```
import numpy as np
import cv2
from numpy.fft import *
from numpy.random import *
from matplotlib import pyplot as plt
from google.colab import drive
from google.colab.patches import *

drive.mount('/content/drive')
imag_1 = cv2.imread("/content/drive/My Drive/Colab Notebooks/imgs/Japan.jpg")
imag_1 = cv2.cvtColor(imag_1,cv2.COLOR_BGR2RGB)
imag_2 = cv2.imread("/content/drive/My Drive/Colab Notebooks/imgs/Japan_4.jpg")

def imgMask(f):
    nr,nc = f.shape[:2]
    mask = np.zeros([nr,nc],dtype='uint8')
    for x in range(nr):
        for y in range(nc):
              if f[x,y,0] >= 0 and f[x,y,0] <50  and f[x,y,1] <= 255 and f[x,y,1] >200 and f[x,y,2] <= 255 and f[x,y,2] >200:
                mask[x,y] = 255

    return mask

img_msk = imgMask(imag_2)
img_inpaint = cv2.inpaint(imag_1,img_msk,3,cv2.INPAINT_TELEA)
#img_inpaint = cv2.cvtColor(img_inpaint,cv2.COLOR_BGR2RGB)
imag_2 = cv2.cvtColor(imag_2,cv2.COLOR_BGR2RGB)
images = [imag_1,imag_2,img_msk,img_inpaint]
titles = ['Original','Img with Mask','Img Mask','Img Inpaint']
plt.figure(figsize=(15,10))
for i in range(4):
    plt.imshow(images[i],cmap='gray')
    plt.title(titles[i],fontsize=15,color='r')
plt.tight_layout()
plt.show()

```
#### 輸出結果：
![](https://i.imgur.com/OXQg7Eq.png)

## 簡答題：
![](https://i.imgur.com/GrxKhA2.png)

解答:
1. 分析方法:
    透過直方圖，將影相作分析，將分析出來的影像找出可能的對應雜訊模型，套用濾波器還原影像，或  是透過訊號雜訊比來評估影像，訊號雜訊比包含計算:總誤差、均分誤差(MSE)、均分跟誤差(RMSE)、峰直雜訊比(PSNR)來做運算，最常見的是PSNR

2. 挑戰:
* 雜訊函數N(x,y)無法事先得到，所以會影響影像還原的效果
* 如果失真函式H(u,v)太小，會造成 數值過大，干擾原始影像的資訊
→透過H(u,v)數值較小的區域設定處理範圍

3. 補繪的時機:
    將影像中，多餘的部分，透過演算法與補繪的方式，去除掉，例如一張風景照中多了一根電線桿，可以透過補繪技術，將其抹除掉，Google Pixel 6系列開始就有推出魔術橡皮擦功能，就是透過類似技術去處理，更早之前的HUAWEI、Xiaomi也有推出類似功能，可以將影像中多餘的人物去除

4. 課程重點:

    |S.O.P.| 
    | -------- |
    | 透過分析→選取適合的濾波器→套用濾波器→比對是否良好還原影像→優化影像→輸出|

    這堂課總體來言，就是透過以上的S.O.P.去完成影像的還原，搭配不同的演算法，可以滿足不同類型的影像達到更好的影像還原。

