###### tags: `影像處理`
# *2022/03/29 影像處理 HW05*
## Coding Part
### 實戰(3)
#### 題目:

```
import numpy as np
x = np.array([1,2,3,1,1,1])
h = np.array([1,2,2,-1,1])
print('x=',x)
print('h=',h)
y = np.convolve(x,h,'same')
print("Convolution y = ",y)
```
#### 輸出結果:

### 實戰(4)
#### 題目:

```
import numpy as np
from scipy.signal import *
x = np.array([[1,2,3],[1,2,3],[1,2,3]])
h = np.array([[1,1,1],[1,2,1],[1,1,1]])
print("x=")
print(x)
print("h=")
print(h)
g = convolve2d(x,h,'same')
print('Convolution g=')
print(g)
```
#### 輸出結果:

### 實戰(5)
#### 題目:

```
import numpy as np
import cv2
from matplotlib import pyplot as plt
from google.colab.patches import cv2_imshow
array_1 = np.array([[1,1,1,3,3],[1,1,3,3,3],[1,3,3,3,6],[3,3,3,6,6],[3,3,6,6,7]])
image = np.float32(array_1)
def Sobel_gradient(f,direction = 1):
sobel_x = np.array([[-1,-2,-1],[0,0,0],[1,2,1]])
sobel_y = np.array([[-1,0,1],[-2,0,2],[-1,0,1]])
if direction ==1:
grad_x = cv2.filter2D(f,cv2.CV_32F,sobel_x)
gx = abs(grad_x)
g = np.uint8(np.clip(gx,0,255))
elif direction ==2:
grad_y = cv2.filter2D(f,cv2.CV_32F,sobel_y)
gy = abs(grad_y)
g = np.uint8(np.clip(gy,0,255))
else:
grad_x = cv2.filter2D(f,cv2.CV_32F,sobel_x)
grad_y = cv2.filter2D(f,cv2.CV_32F,sobel_y)
magnitude = abs(grad_x)+abs(grad_y)
g = np.uint8(np.clip(magnitude,0,255))
return g
g = Sobel_gradient(image,3)
print("G:")
print(g)
gx = Sobel_gradient(image,1)
print("Gx向:")
print(gx)
gy = Sobel_gradient(image,2)
print("Gy向:")
print(gy)
```
#### 輸出結果:

### 實戰(6)
#### 題目:

```
#HW06
import numpy as np
import cv2
from google.colab.patches import cv2_imshow
image = np.zeros([512,512],dtype="uint8")#Set a new black paper
for i in range(0,512):#Control the wide
for j in range(0,512):#Control the high
if (i//64+j//64)%2==0:#If wide//64 + high//64 is even, this pixel is black
image[i][j]=0;
else:
image[i][j]=255#If wide//64 + high//64 is odd, this pixel is white
def laplacian(f):
temp = cv2.Laplacian(f,cv2.CV_32F)+128
g = np.uint8(np.clip(temp,0,255))
return g
def composite_laplacian(f):
kernel = np.array([[0,-1,0],[-1,5,-1],[0,-1,0]])
temp = cv2.filter2D(f,cv2.CV_32F,kernel)
g = np.uint8(np.clip(temp,0,255))
return g
newImg1 = composite_laplacian(image)
images = [image,newImg1]
titles = ["Original","Composite_Laplacian"]
plt.figure(figsize=(15,10))
for i in range(2):
plt.subplot(1,2,i+1),plt.imshow(images[i],cmap='gray')
plt.title(titles[i],fontsize=15,color='r')
plt.xticks([]),plt.yticks([])
plt.tight_layout()
plt.show()
```
#### 輸出結果:

## 簡答題:
#### 題目:

#### 解答:
* Sobel:具有邊緣銳化的效果,但是會讓照片整體有明顯對比度被拉高的感覺,看起來沒有那麼的自然
* 拉普拉斯:一樣具有邊緣銳化的效果,但是他不會讓整張照片看起來對比度非常強烈,相較是比較正常的強化細節