---
# System prepended metadata

title: Python學習筆記 0003 (灰階、模糊、邊緣檢測、擴展、侵蝕)

---

# Python學習筆記 0003 (灰階、模糊、邊緣檢測、擴展、侵蝕)
## 輸出灰階
### 方法1 (cv2.imread(path,0))
```javascript=
import cv2
frameWidth = 400
frameHeight = 400

path = "img12.jpg"
img = cv2.imread(path)
img = cv2.resize(img, (frameWidth, frameHeight))

cv2.imshow("234",img)
cv2.waitKey(0)
```
![](https://i.imgur.com/YSjpK7T.png)

要把彩色轉成灰階圖

```javascript=
import cv2
frameWidth = 400
frameHeight = 400

path = "img12.jpg"
img = cv2.imread(path,0)
img = cv2.resize(img, (frameWidth, frameHeight))

cv2.imshow("234",img)
cv2.waitKey(0)
```
![](https://i.imgur.com/TPkvDVy.png)

- img = cv2.imread(path,0)
這裡多了一個0

### 方法2(cv2.cvtColor(img, cv2.COLOR_BGR2GRAY))
```javascript=
import cv2
frameWidth = 400
frameHeight = 400

path = "img12.jpg"
img = cv2.imread(path)
img = cv2.resize(img, (frameWidth, frameHeight))
img1Gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

cv2.imshow("234", img1Gray)
cv2.imshow("123", img)
cv2.waitKey(0)
```
![](https://i.imgur.com/QZ5KJ8J.png)



## 高斯模糊 (cv2.GaussianBlur)
```javascript=
import cv2
frameWidth = 200
frameHeight = 200

path = "img12.jpg"
img = cv2.imread(path)
img = cv2.resize(img, (frameWidth, frameHeight))
imgGray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
imgBlur1 = cv2.GaussianBlur(imgGray, (5, 5), 0)
imgBlur2 = cv2.GaussianBlur(imgGray, (11, 11), 0)

cv2.imshow("Gray", imgGray)
cv2.imshow("Blur1", imgBlur1)
cv2.imshow("Blur2", imgBlur2)
cv2.waitKey(0)
```

![](https://i.imgur.com/YpXjFFy.png)

- cv2.GaussianBlur(imgGray, (5, 5), 0)
    cv2.GaussianBlur(imgGray, (11, 11), 0)
    
    (5, 5)、(11, 11) 只能奇數
    數字越大越模糊
## 邊緣檢測(Canny)
```javascript=
import cv2
frameWidth = 200
frameHeight = 200

path = "img12.jpg"
img = cv2.imread(path)
img = cv2.resize(img, (frameWidth, frameHeight))
imgCanny = cv2.Canny(img, 100, 100 )
imgCanny2 = cv2.Canny(img, 200, 200 )

cv2.imshow("nor", img)
cv2.imshow("Can1", imgCanny)
cv2.imshow("Can2", imgCanny2)
cv2.waitKey(0)
```
![](https://i.imgur.com/UFhpUWU.png)

- cv2.Canny(img, 100, 100 )
- cv2.Canny(img, 200, 200 )
100、200
數字越大細節越少

## 陣列(np.ones)
```javascript=
import cv2
import numpy as np
kernel = np.ones((5,5),np.uint8)
print(kernel)
```
![](https://i.imgur.com/4j1C7RQ.png)

### 擴展(Dilate)
```javascript=
import cv2
import numpy as np
kernel = np.ones((5,5),np.uint8)
print(kernel)
frameWidth = 200
frameHeight = 200

path = "img12.jpg"
img = cv2.imread(path)
img = cv2.resize(img, (frameWidth, frameHeight))
imgCanny = cv2.Canny(img, 100, 100 )
imgDilation = cv2.dilate(imgCanny,kernel,iterations = 1)
imgDilation2 = cv2.dilate(imgCanny,kernel,iterations = 2)
imgDilation3 = cv2.dilate(imgCanny,kernel,iterations = 5)
cv2.imshow("nor", imgCanny)
cv2.imshow("Dil", imgDilation)
cv2.imshow("Dil2", imgDilation2)
cv2.imshow("Dil3", imgDilation3)
cv2.waitKey(0)
```
![](https://i.imgur.com/P5ia7YD.png)

- cv2.dilate(imgCanny,kernel,iterations =x)
用kernel 然後疊代x次數 x=自然整數
- 可以看到 iterations 疊代1、2、5次的變化

 ### 陣列累增
```javascript=
import cv2
import numpy as np
kernel = np.ones((3,3),np.uint8)
kernel1 = np.ones((7,7),np.uint8)
kernel2 = np.ones((10,10),np.uint8)
kernel3 = np.ones((50,50),np.uint8)
frameWidth = 200
frameHeight = 200
path = "img12.jpg"
img = cv2.imread(path)
img = cv2.resize(img, (frameWidth, frameHeight))
imgCanny = cv2.Canny(img, 100, 100 )
imgDilation = cv2.dilate(imgCanny,kernel,iterations = 1)
imgDilation2 = cv2.dilate(imgCanny,kernel1,iterations = 1)
imgDilation3 = cv2.dilate(imgCanny,kernel2,iterations = 1)
imgDilation4 = cv2.dilate(imgCanny,kernel3,iterations = 1)
cv2.imshow("nor", imgCanny)
cv2.imshow("Dil", imgDilation)
cv2.imshow("Dil2", imgDilation2)
cv2.imshow("Dil3", imgDilation3)
cv2.imshow("Dil4", imgDilation4)
cv2.waitKey(0)
```
![](https://i.imgur.com/GoMpeyK.png)

- kernel = np.ones((x,x),np.uint8)
x=自然數 8位元
- 可以看到陣列(3,3)、(7,7)、(10,10)、(50,50)的變化

## 微粒侵蝕(erode)
```javascript=
import cv2
import numpy as np
kernel = np.ones((3,3),np.uint8)
frameWidth = 200
frameHeight = 200
path = "img12.jpg"
img = cv2.imread(path)
img = cv2.resize(img, (frameWidth, frameHeight))
imgCanny = cv2.Canny(img, 100, 100 )
imgDilation = cv2.dilate(imgCanny,kernel,iterations = 2)
imgEroded0 =cv2.erode(img,kernel,iterations = 1)
imgEroded1 =cv2.erode(imgCanny,kernel,iterations = 1)
imgEroded2 =cv2.erode(imgDilation,kernel,iterations = 1)
cv2.imshow("img", img)
cv2.imshow("nor", imgCanny)
cv2.imshow("Dil", imgDilation)
cv2.imshow("Ero0", imgEroded0)
cv2.imshow("Ero1", imgEroded1)
cv2.imshow("Ero2", imgEroded2)
cv2.waitKey(0)
```
![](https://i.imgur.com/STxZX0w.png)

- 這裡分別用了 原始、canny、dilate 來做erode(侵蝕)
### 侵蝕 疊代累增
```javascript=
import cv2
import numpy as np
kernel = np.ones((3,3),np.uint8)
frameWidth = 200
frameHeight = 200
path = "img12.jpg"
img = cv2.imread(path)
img = cv2.resize(img, (frameWidth, frameHeight))
imgCanny = cv2.Canny(img, 100, 100 )
imgDilation = cv2.dilate(imgCanny,kernel,iterations = 2)

imgEroded1 =cv2.erode(imgDilation,kernel,iterations = 1)
imgEroded2 =cv2.erode(imgDilation,kernel,iterations = 3)
imgEroded3 =cv2.erode(imgDilation,kernel,iterations = 7)
imgEroded4 =cv2.erode(imgDilation,kernel,iterations = 20)

cv2.imshow("Ero1", imgEroded1)
cv2.imshow("Ero2", imgEroded2)
cv2.imshow("Ero3", imgEroded3)
cv2.imshow("Ero4", imgEroded4)
cv2.waitKey(0)
```
![](https://i.imgur.com/RKjVb5a.png)

- 這裡可以看到 erode(侵蝕)疊代 iterations 次數的變化

### 侵蝕 擴展(dilate)疊代累增
```javascript=
import cv2
import numpy as np
kernel = np.ones((3,3),np.uint8)
frameWidth = 200
frameHeight = 200
path = "img12.jpg"
img = cv2.imread(path)
img = cv2.resize(img, (frameWidth, frameHeight))
imgCanny = cv2.Canny(img, 100, 100 )
imgDilation1 = cv2.dilate(imgCanny,kernel,iterations = 2)
imgDilation2 = cv2.dilate(imgCanny,kernel,iterations = 5)
imgDilation3 = cv2.dilate(imgCanny,kernel,iterations = 7)

imgEroded1 =cv2.erode(imgDilation1,kernel,iterations = 1)
imgEroded2 =cv2.erode(imgDilation2,kernel,iterations = 1)
imgEroded3 =cv2.erode(imgDilation3,kernel,iterations = 1)

cv2.imshow("Dil1", imgDilation1)
cv2.imshow("Dil2", imgDilation2)
cv2.imshow("Dil3", imgDilation3)
cv2.imshow("Ero1", imgEroded1)
cv2.imshow("Ero2", imgEroded2)
cv2.imshow("Ero3", imgEroded3)
cv2.waitKey(0)
```
![](https://i.imgur.com/tuYUV3l.png)


:::spoiler 一篇一個OpenCV函式(imshow、waitKey)
cv2.imshow
原型：imshow(winname,mat)
作用：建立一個影象視窗
引數：winname，視窗名稱。mat，影象矩陣。

cv2.waitKey
原型：waitKey(delay=None)
作用；接受一個按鍵事件並返回按鍵的ASCII碼。
引數：delay為按下後返回的延遲時間。

https://www.itread01.com/content/1550310670.html
:::


:::spoiler 一篇一個numpy函式(squeeze)


當我們要從數組形狀中刪除一個維度
把 shape 中为 1 的維度去掉

- 用法：
        numpy.squeeze(arr, axis=None )

  arr :[數組]輸入數組。
axis :[無，整數或整數元組，可選]
選擇 shape 中維度為 1 的軸
如果選擇的形狀輸入 > 1 的軸，則會引發錯誤

- Return :
        squeezed[ndarray]輸入 array
        刪除了長度為 1 的部分。

- 例題1 刪除第2軸
```javascript=
import numpy as np
e = np.arange(6)
h = e.reshape(6,1,1)
print(h.shape)
print(h)
print("---")
# 刪去第二軸
m = np.squeeze(h, 1)
print(m.shape)
print(m)
```

- 例題1輸出

        (6, 1, 1)
        [[[0]]

         [[1]]

         [[2]]

         [[3]]

         [[4]]

         [[5]]]
        ---
        (6, 1)
        [[0]
         [1]
         [2]
         [3]
         [4]
         [5]]
         
         
- 例題2  刪除第3軸

```javascript=
import numpy as np
c = np.arange(6)
h = c.reshape(3,2,1)
print(h.shape)
print(h)
print("---刪除第3軸")
m = np.squeeze(h,2)
print(m.shape)
print(m)
```
- 例題2輸出

        (3, 2, 1)
        [[[0]
          [1]]

         [[2]
          [3]]

         [[4]
          [5]]]
        ---刪除第3軸
        (3, 2)
        [[0 1]
         [2 3]
         [4 5]]



- 例題3

```javascript=
import numpy as np
h =np.array([[1,4,9,16,25]])
print(h.shape)
print(h)
print("---刪除第1軸")
m = np.squeeze(h,0)
print(m.shape)
print(m)
```
- 例題2輸出

        (1, 5)
        [[ 1  4  9 16 25]]
        ---刪除第1軸
        (5,)
        [ 1  4  9 16 25]

https://hackmd.io/@yillkid/rJp5h90s8/https%3A%2F%2Fhackmd.io%2F%40yillkid%2FHy5a_5A-F#array
https://blog.csdn.net/zenghaitao0128/article/details/78512715
https://numpy.org/doc/stable/reference/routines.array-manipulation.html
:::

