opencv for python 之 圖像處理 閥值轉換 侵蝕

import cv2.cv as cv
#load image
filename = "../Video/cat.jpg"
image = cv.LoadImage(filename)
win_name = "test"
cv.NamedWindow(win_name)
win2_name = "test2"
cv.NamedWindow(win2_name)

#set created image
size = cv.GetSize(image)#(100, 100)
depth = 8
channels = 1
grey = cv.CreateImage(size, depth, channels)#create one 100x100 single channels image
cv.CvtColor(image, grey, cv.CV_BGR2GRAY)

閥值變化

#Thresholdind operations
threshold = 100
colour = 255
#cv.Threshold(image, image, threshold, colour,  cv.CV_THRESH_BINARY)
cv.Threshold(grey, grey, threshold, colour,  cv.CV_THRESH_BINARY)
cv.ShowImage(win2_name, grey)

cv.Threshold(grey, grey, threshold, colour, cv.CV_THRESH_OTSU)
cv.ShowImage(win_name, grey)
'''

形態學變換
element_shape = cv.CV_SHAPE_RECT
pos = 1
element = cv.CreateStructuringElementEx(pos*2+1, pos*2+1, pos, pos, element_shape)

擴張變化
cv.Dilate(grey, grey, element, 2)

侵蝕變化
cv.Erode(grey, grey, element, 2)
cv.ShowImage(win_name, grey)
cv.ShowImage(win2_name, grey)
'''
cv.WaitKey()

閥值變化結果

擴展膨脹變化

侵蝕:翻轉的的擴展

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章