import cv2 as cv
import numpy as np
def edge_demo(image):
blurred = cv.GaussianBlur(image, (3, 3), 0)
gray = cv.cvtColor(blurred, cv.COLOR_BGR2GRAY)
edge_output = cv.Canny(gray, 30, 100)
cv.imshow("Canny Edge", edge_output)
return edge_output
def edge_contours_demo(image):
binary = edge_demo(image)
cv.imshow("canny binary", binary)
cloneImage, 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)#最后一个参数为-1时表示填充
approxCurve = cv.approxPolyDP(contour, 4, True)
if approxCurve.shape[0] > 6:
cv.drawContours(image, contours, i, (255, 0, 0), 2)
if approxCurve.shape[0] == 4:
cv.drawContours(image, contours, i, (0, 255, 0), 2)
#print(approxCurve.shape[0])
#print(i)
cv.imshow("edge detect contours", image)
def contours_demo(image):
dst = cv.GaussianBlur(image, (3, 3), 0)
gray = cv.cvtColor(dst, cv.COLOR_BGR2GRAY)
ret, binary = cv.threshold(gray, 0, 255, cv.THRESH_BINARY | cv.THRESH_OTSU)
cv.imshow("binary image", binary)
# binary = edge_demo(image)
cloneImage, 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)#最后一个参数为-1时表示填充
approxCurve = cv.approxPolyDP(contour, 4, True)
if approxCurve.shape[0] > 6:
cv.drawContours(image, contours, i, (255, 0, 0), 2)
if approxCurve.shape[0] == 4:
cv.drawContours(image, contours, i, (0, 255, 0), 2)
#print(approxCurve.shape[0])
#print(i)
cv.imshow("detect contours", image)
src = cv.imread("3.png")
cv.namedWindow("input image", cv.WINDOW_AUTOSIZE)
cv.imshow("input image", src)
src1=src.copy()
src2=src.copy()
contours_demo(src1)
edge_contours_demo(src2)
cv.waitKey(0)
cv.destroyAllWindows()