---
# System prepended metadata

title: '*2022/03/22 影像處理 HW04*'
tags: [影像處理]

---

###### tags: `影像處理`
# *2022/03/22 影像處理 HW04*
## Coding Part
### 實戰(1)
#### 題目:
![](https://i.imgur.com/NQmpPMF.png)

```
import numpy as np
import cv2
import scipy.special as special
from matplotlib import pyplot as plt
from google.colab import *
from google.colab.patches import *
drive.mount('/content/drive')
image = cv2.imread('/content/drive/My Drive/Colab Notebooks/imgs/Taipei.jpeg',-1)
image1 = cv2.cvtColor(image,cv2.COLOR_BGR2RGB)

def beta_correction(f,a=2.0,b=2.0):
    g=f.copy()
    nr,nc=f.shape[:2]
    x=np.linspace(0,1,256)
    table = np.round(special.betainc(a,b,x)*255,0)
    if f.ndim !=3:
        for x in range(nr):
            for y in range(nc):
                g[x,y]=table[f[x,y]]
    else:
        for x in range(nr):
            for y in range(nc):
                for k in range(3):
                    g[x,y,k]=table[f[x,y,k]]
    return g

def image_negative(f):
    g = 255 - f
    return g

def gamma_correction(f,gamma=2):
    g = f.copy()
    nr,nc = f.shape[:2]
    c = 255/(255**gamma)
    table = np.zeros(256)
    for i in range(256):
        table[i] = round(i**gamma*c,0)
    if f.ndim !=3:
        for x in range(nr):
            for y in range(nc):
                g[x,y] = table[f[x,y]]
    else:
        for x in range(nr):
            for y in range(nc):
                for k in range(3):
                    g[x,y,k] = table[f[x,y,k]]
    return g

newImage1 = image_negative(image1)
newImage2 = gamma_correction(image1,0.5)
newImage3 = beta_correction(image1,a=0.5,b=0.5)

images = [image1,newImage1,newImage2,newImage3]
titles = ['Original','Negative','Gamma=0.5','Beta=0.5']
plt.figure(figsize=(20,20))
for i in range(4):
    plt.subplot(2,2,i+1),plt.imshow(images[i])
    plt.title(titles[i],fontsize=20,color='r')
    plt.xticks([]),plt.yticks([])
plt.tight_layout()
plt.show()
```
#### 輸出結果:
![](https://i.imgur.com/GnQKotg.png)


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

### 實戰(2)
#### 題目:
![](https://i.imgur.com/UeJqCJW.png)
```
import numpy as np
import cv2
from matplotlib import pyplot as plt
from google.colab import *
from google.colab.patches import *

image1 = np.array([[0,0,1,1,1,3,3],[0,1,1,1,3,3,3],[1,1,1,3,3,3,6],[1,1,3,3,3,6,6],[1,3,3,3,6,6,6],[3,3,3,6,6,7,7],[3,3,6,6,6,7,7]])
image1 = np.float32(image1) #Make the picture into float32

def histogram(img):
    if img.ndim!=3:
        hist = cv2.calcHist([img],[0],None,[256],[0,256])
    else:
        gray_img = cv2.cvtColor(img,cv.COLOR_BGR2GRAY)
        hist = cv2.calHist([gray_img],[0],None,[256],[0,256])

    return hist

hist1 = histogram(image1)

x = np.arange(0,8)
y = np.array([3,12,0,19,0,0,11,4])
PDF = y/np.sum(y)
CDF = np.cumsum(PDF)

#Histogram
titles=['Original','Histogram','PDF&CDF','Histogram Equalization']
plt.figure(figsize=(15,5))
plt.subplot(131)
plt.title(titles[0],fontsize=15,color='r')
plt.imshow(image1,'gray')
plt.subplot(132)
plt.title(titles[1],fontsize=15,color='r')
plt.plot(hist1)
plt.xlim([0,10])
plt.xlabel('Intensity')
plt.ylabel('#Intensities')
plt.subplot(133)
plt.title(titles[2],fontsize=15,color='r')
plt.plot(x,PDF,marker='o',label='PDF')
plt.plot(x,CDF,marker='o',label='CDF')
plt.xlim(0,7)
plt.ylim(0,1.2)
plt.xlabel("Value")
plt.ylabel("Probability Values")

```
#### 輸出結果:
![](https://i.imgur.com/4p4De9E.png)
![](https://i.imgur.com/HKONkrx.png)



## 簡答題
#### 題目:
![](https://i.imgur.com/tgCcnAK.png)
#### 解答:
1.用來完成特定需求，通常是希望增強影像的品質，包括增強對比、去除雜訊......etc，常用的方法:
* 強度轉換
* 直方圖處理
* 影像慮波

2.直方圖的目的是用來統計數位影像中像素強度的分布情形，常用來處理的演算法:
* 機率密度函數(PDF)
* 累積密度函數(CDF)



