1

## 如何把圖像中的 【不正常扭曲】處 標示 出來

1. 經過 Gray 後的

2. 接著我處理了 Erosion.

3. 對第一次 Erosion 後的, 做 Threshold 處理:

4. 對 Threshold 後的, 再 Erosion 一次 :

# 定義一個顯示圖片的 function

def displayIMG(img, windowName):
cv2.namedWindow(windowName, cv2.WINDOW_NORMAL)
cv2.imshow(windowName, img)
cv2.imwrite(windowName + '.jpg', img)

# 1.讀取圖檔：讀取圖片檔並顯示：

displayIMG(image, "Original")

# 2.轉換為灰階

gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
name = "Gray"
draw = gray
displayIMG(draw, name)

# 3.做 Erosion 處理

kernel = np.ones((3, 3), np.uint8)
erosion = cv2.erode(gray, kernel, iterations=2)
name = "Gary_Erosion"
draw = erosion
hist = plotHistogram(draw, name)
max_gray = max(hist)
print('Max Gray = ', max_gray)
displayIMG(draw, name)

# 4.做 Threshold

(T, thresh) = cv2.threshold(gray, 120, 255, cv2.THRESH_TOZERO)
name = "Threshold_Gray"
draw = thresh
displayIMG(draw, name)
plotHistogram(draw, name, max_gray)

# 5. 對 Threshold 後的, 再 Erosion 一次.

kernel = np.ones((3, 3), np.uint8)
erosion2 = cv2.erode(draw, kernel, iterations=5)
name = "Gary_Threshold_Erosion"
draw = erosion2
hist = plotHistogram(draw, name, max_gray)
displayIMG(draw, name)

50男 iT邦新手 4 級 ‧ 2021-03-12 10:17:02 檢舉