1. 程式人生 > >macOS python opencv 影象膨脹、必運算 開運算

macOS python opencv 影象膨脹、必運算 開運算

python opencv 影象膨脹

1,opencv 影象 形態變換

#! /usr/local/bin/python3
# coding:utf-8
"""
影象二值化 全域性閥值 
"""
from PIL import Image
import cv2
import numpy as np

src = "/root/captcha_png.png"
import cv2
img = cv2.imread(src , 0)
#高斯濾波
gauss = cv2.GaussianBlur(
img,(5,5),0) #全域性二值化 ret , thresh1 = cv2.threshold(gauss ,127, 255, cv2.THRESH_BINARY) cv2.imwrite("/root/captcha_gauss.png", thresh1) kernel = np.ones((3,3), np.uint8) #erosion = cv2.erode(thresh1, kernel, iterations=1) dilation = cv2.dilate(thresh1, kernel, iterations=1) cv2.imwrite("/root/captcha_dilation.png"
, dilation) cv2.imshow("dilation", dilation) cv2.waitKey()
  • 二值化 高斯濾波
    在這裡插入圖片描述
  • 膨脹後圖片
    在這裡插入圖片描述

2,閉運算 開運算

#! /usr/local/bin/python3
# coding:utf-8
"""
影象二值化 全域性閥值 
"""
from PIL import Image
import cv2
import numpy as np

src = "/root/captcha_png.png"
import cv2
img = cv2.imread(src , 0)
#高斯濾波
gauss = cv2.GaussianBlur(
img,(5,5),0) #全域性二值化 ret , thresh1 = cv2.threshold(gauss ,127, 255, cv2.THRESH_BINARY) cv2.imwrite("/root/captcha_gauss.png", thresh1) kernel = np.ones((3,3), np.uint8) #erosion = cv2.erode(thresh1, kernel, iterations=1) dilation = cv2.dilate(thresh1, kernel, iterations=1) cv2.imwrite("/root/captcha_dilation.png", dilation) #cv2.imshow("dilation", dilation) #cv2.waitKey() #定義結構元素 rect = cv2.getStructuringElement(cv2.MORPH_RECT, (2,2)) #閉運算 closed = cv2.morphologyEx(dilation, cv2.MORPH_CLOSE, rect) cv2.imshow("close", closed) cv2.waitKey() opened = cv2.morphologyEx(dilation, cv2.MORPH_OPEN, rect) cv2.imshow("open", opened) cv2.waitKey()
  • 閉運算
    在這裡插入圖片描述
  • 開運算
    在這裡插入圖片描述
    參考:
  1. OpenCV-Python教程:14.形態變換
  2. opencv for python (14) 形態學轉換(影象腐蝕、影象膨脹、開運算、閉運算、形態學梯度、禮帽、黑帽)
  3. python opencv形態學變換:腐蝕與膨脹
  4. OpenCV-Python教程(4、形態學處理)開運算和閉運算