今天就跟大家聊聊有關(guān)利用Python+OpenCV圖像處理功能實(shí)現(xiàn)輪廓發(fā)現(xiàn),可能很多人都不太了解,為了讓大家更加了解,小編給大家總結(jié)了以下內(nèi)容,希望大家根據(jù)這篇文章可以有所收獲。
代碼如下:
import cv2 as cv import numpy as np def contours_demo(image): dst = cv.GaussianBlur(image, (3, 3), 0) #高斯模糊去噪 gray = cv.cvtColor(dst, cv.COLOR_RGB2GRAY) ret, binary = cv.threshold(gray, 0, 255, cv.THRESH_BINARY | cv.THRESH_OTSU) #用大律法、全局自適應(yīng)閾值方法進(jìn)行圖像二值化 cv.imshow("binary image", binary) cloneTmage, contours, heriachy = cv.findContours(binary, cv.RETR_EXTERNAL, cv.CHAIN_APPROX_SIMPLE) for i, contour in enumerate(contours): cv.drawContours(image, contours, i, (0, 0, 255), 2) print(i) cv.imshow("contours", image) for i, contour in enumerate(contours): cv.drawContours(image, contours, i, (0, 0, 255), -1) cv.imshow("pcontours", image) src = cv.imread('E:/imageload/coins.jpg') cv.namedWindow('input_image', cv.WINDOW_NORMAL) #設(shè)置為WINDOW_NORMAL可以任意縮放 cv.imshow('input_image', src) contours_demo(src) cv.waitKey(0) cv.destroyAllWindows()