---
# System prepended metadata

title: '*2022/03/15 影像處理 HW03*'
tags: [影像處理]

---

###### tags: `影像處理`
# *2022/03/15 影像處理 HW03*
## Coding Part
### 實戰(1)
#### 題目:
![](https://i.imgur.com/VfweT9N.png)
##### 最近鄰內插法

```
#最近鄰內插法
from numpy.matrixlib.defmatrix import matrix
import numpy as np
import cv2
import math
from matplotlib import pyplot as plt
from google.colab.patches import *

matrix=np.array([[10,20,30],[40,50,60],[70,80,90]])
print(matrix.shape)

def NN_interpolation(img):
    srcH,srcW = img.shape
    dstH, dstW = 5,5
    retImg = np.zeros([5,5],dtype='uint8')
    for i in range(5):
        for j in range(5):
            srcX = int(round(i)*(3/5)) #Force to int type
            srcY = int(round(j)*(3/5)) #Force to int type
            if srcX >= srcW:
                scrX = srcW -1
            if srcY >= srcH:
                scrY = srcH -1
            retImg[i,j] = img[srcX,srcY]
    return retImg

newImg = NN_interpolation(matrix)
print(newImg.shape)

print(newImg)
```
#### 輸出結果:
![](https://i.imgur.com/AkHpmCj.png)
所以A=50,B=50

##### 雙線性內插法
```
#雙線性內插法
import numpy as np
import cv2
import math
from google.colab import drive
from google.colab.patches import *

matrix=np.array([[10,20,30],[40,50,60],[70,80,90]])
print(matrix.shape)

def double_linear(input_signal,zoom_multiples):
    input_row,input_col = input_signal.shape
    output_row = 5
    output_col = 5
    output_signal = np.zeros((output_row,output_col))
    for i in range(output_row):
        for j in range(output_col):
            temp_x = i / output_row * input_row
            temp_y = j / output_col * input_col
            x1 = int(temp_x)
            y1 = int(temp_y)
            x2 = x1;y2 = y1 + 1
            x3 = x1 + 1;y3 = y1
            x4 = x1 + 1;y4 = y1 + 1
            t = temp_x;u = temp_y - y1
            if x4 >= input_row:
                x4 = input_row -1 
                x2 = x4
                x1 = x4 -1 
                x3 = x4 -1 
            if y4 >= input_col:
                y4 = input_col -1 
                y3 = y4
                y1 = y4 -1 
                y2 = y4 -1 
            output_signal[i,j]=(1-t)*(1-u)*input_signal[x1,y1] + (1-t)*u*input_signal[x2,y2] + t*(1-u)*input_signal[x3,y3]+t*u*input_signal[x4,y4]
    return output_signal

dstImg = double_linear(matrix,(5/3)).astype(np.uint8)
print(dstImg.shape)

print(dstImg)
```
#### 輸出結果:
![](https://i.imgur.com/pvUE7Q7.png)
所以A=88,B=112

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

```
import numpy as np
import cv2
import math
from google.colab import drive
from google.colab.patches import *

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

scale = eval(input('Please enter scale:'))
nr,nc=image.shape[:2]
nr2 = int(nr*scale)
nc2 = int(nc*scale)

newImg = cv2.resize(image,(nr2,nc2),interpolation=cv2.INTER_NEAREST)
newImg_2 = cv2.resize(image,(nr2,nc2),interpolation=cv2.INTER_LINEAR)
newImg_3 = cv2.resize(image,(nr2,nc2),interpolation=cv2.INTER_CUBIC)

images = [image,newImg,newImg_2,newImg_3]
titles = ['Original','NN_interpolation_Rescaling_'+str(scale),'Double_Linear_Rescaling_'+str(scale),'CUBIC_Rescaling_'+str(scale)]
plt.figure(figsize=(100,100))
for i in range(4):
    plt.subplot(1,4,i+1),plt.imshow(images[i],'gray')
    plt.title(titles[i],fontsize=100,color='r')
    plt.xticks([]),plt.yticks([])
plt.tight_layout()
plt.show()
```
#### 輸出結果:
![](https://i.imgur.com/4ZWoFvX.png)

##### 說明:
從照片中看起來，雙立方內插法的影像塗抹感比較嚴重，在細節表現沒有那麼出色，相較起來線性的亮暗表現最為突出(可能是因為選的照片本身是黑白，解析度也沒有那麼高)

![](https://i.imgur.com/iBhi8mL.png)
範例圖片(landscape)

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

解答：
1.	空間轉換：(x’,y’)=T{(x,y)}，(x,y)表示輸入的空間座標，(x’,y’)表示輸出的空間座標，T{}表示空間轉換函數
* 	正向映射：容易有破洞問題
* 	反向映射：不會有破洞問題
2.	幾何轉換：改變數位影像中像素空間座標的幾何關係，但不改變像素的灰階或色彩值
* 	仿射轉換：縮放、旋轉、平移、翻轉、偏移
* 	透視轉換
* 	其他轉換
3.	常用的數位影像內插法：
* 	最鄰近內插法
* 	雙線性內插法
* 	雙立方內插法
