資源簡介
Python沒有MATLAB中的imfill函數,要實現空洞填充必須自己寫函數。文件包含代碼以及測試圖片,Python3的語法,親測可用
代碼片段和文件信息
import?cv2;
import?numpy?as?np;
#?Read?image
im_in?=?cv2.imread(“test.png“?cv2.IMREAD_GRAYSCALE);
#?Threshold.
#?Set?values?equal?to?or?above?220?to?0.
#?Set?values?below?220?to?255.
th?im_th?=?cv2.threshold(im_in?220?255?cv2.THRESH_BINARY_INV);
#?Copy?the?thresholded?image.
im_floodfill?=?im_th.copy()
#?Mask?used?to?flood?filling.
#?Notice?the?size?needs?to?be?2?pixels?than?the?image.
h?w?=?im_th.shape[:2]
mask?=?np.zeros((h+2?w+2)?np.uint8)
#?Floodfill?from?point?(0?0)
cv2.floodFill(im_floodfill?mask?(00)?255);
#?Invert?floodfilled?image
im_floodfill_inv?=?cv2.bitwise_not(im_floodfill)
#?Combine?the?two?images?to?get?the?foreground.
im_out?=?im_th?|?im_floodfill_inv
#?Display?images.
cv2.imshow(“Thresholded?Image“?im_th)
cv2.imshow(“Floodfilled?Image“?im_floodfill)
cv2.imshow(“Inverted?Floodfilled?Image“?im_floodfill_inv)
cv2.imshow(“Foreground“?im_out)
cv2.waitKey(0)
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????文件?????????944??2018-06-13?10:22??imfill.py
?????文件????????9443??2018-06-13?10:18??test.png
- 上一篇:冗余偽線開發實現
- 下一篇:重慶大學2013算法分析與設計試卷A
評論
共有 條評論