---
# System prepended metadata

title: '*2022/03/08 影像處理 HW02*'
tags: [影像處理]

---

###### tags: `影像處理`
# *2022/03/08 影像處理 HW02*

## Coding Part
### 實戰(1)
#### 題目:
![](https://i.imgur.com/YuaFCcm.png)

```
#實戰(1)
import numpy as np
import cv2
from matplotlib import pyplot as plt
from google.colab import drive
from google.colab.patches import *
drive.mount('/content/drive')#掛載Google Drive
image = cv2.imread('/content/drive/My Drive/Colab Notebooks/imgs/Lenna.bmp',-1) #以原始狀態讀取圖片
print(image.shape)

nr,nc = image.shape[:2]
x0 = nr//2
y0 = nc//2
sigma = 75
illumination = np.zeros([nr,nc],dtype = 'float32')
print(illumination.shape)
for x in range(nr):
    for y in range(nc):
        illumination[x,y]=np.exp(-((x - x0)**2+(y-y0)**2)/(2*sigma*sigma)) #公式套入

new_image = image.copy()
for x in range(nr):
    for y in range(nc):
        val = round(illumination[x,y]*image[x,y])
        new_image[x,y] = np.uint8(val)
        
output_image = [image,illumination,new_image]
titles = ['Original','Gaussian','New_image']
plt.figure(figsize=(15,15))
for i in range(3):
    plt.subplot(1,3,i+1),plt.imshow(output_image[i],'gray') #Gray output
    plt.title(titles[i])
    plt.xticks([]),plt.yticks([]) 
plt.tight_layout()
plt.show()
```
#### 輸出結果:
![](https://i.imgur.com/e3v8BnT.png)


### 實戰(2)
#### 題目:
![](https://i.imgur.com/5y8UpzM.png)

```
#實戰(2)
import numpy as np
import cv2
from matplotlib import pyplot as plt
from google.colab import drive
from google.colab.patches import *
drive.mount('/content/drive')#掛載Google Drive
image = cv2.imread('/content/drive/My Drive/Colab Notebooks/imgs/stairs.jpg',-1) #以原始狀態讀取圖片
print(image.shape)

def image_quantization(img,bits):
    nr,nc = img.shape[:2]
    retImg = img.copy()
    levels = 2**bits
    interval = 256/levels
    gray_level_interval = 255/levels
    table = np.zeros(256)
    for k in range(256):
        for l in range(levels):
            if k >= l*interval and l < (l+1)* interval:
                table[k]=round(l*gray_level_interval)
        for x in range(nr):
            for y in range(nc):
                retImg[x,y] = np.uint8(table[img[x,y]])
    return retImg
    
image_bit_5 = image_quantization(image,5)
image_bit_2 = image_quantization(image,2)
images = [image,image_bit_5,image_bit_2]
titles = ['Original','5 bit','2 bit']
plt.figure(figsize=(25,15)) #設定圖片區域大小
for i in range(3):
    plt.subplot(1,3,i+1),plt.imshow(images[i],'gray')
    plt.title(titles[i],fontsize=30)
    plt.xticks([]),plt.yticks([]) 
plt.tight_layout()
plt.show()
```
#### 輸出結果:
![](https://i.imgur.com/rRXYkIh.png)


![](https://i.imgur.com/WXRQGo2.jpg)
範例圖片(stairs)

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

解答：取樣定理：假設原始訊號的最高頻率為f_1，則如果我們取樣用的頻率大於2倍的f_1，就可以保證原始訊號不失真混疊現象：當取樣頻率不足時，容易產生失真的情形，無法還原出原有的訊號

