23 lines
614 B
Python
23 lines
614 B
Python
# -*- coding: UTF-8 -*-
|
|
import cv2 as cv
|
|
import numpy
|
|
# 1.读入图片
|
|
img = cv.imread('demo/171.png')
|
|
img_gray = cv.cvtColor(img, cv.COLOR_BGR2GRAY)
|
|
contours, thresh = cv.threshold(img_gray, 0, 255, cv.THRESH_BINARY + cv.THRESH_OTSU)
|
|
|
|
# 2.寻找轮廓
|
|
contours, hierarchy = cv.findContours(thresh, cv.RETR_LIST, 2)
|
|
|
|
print(len(contours),hierarchy)
|
|
|
|
# 3.绘制轮廓
|
|
cv.drawContours(img, contours, -1, (0, 0, 255), 2)
|
|
|
|
# cv.imshow('result',img)
|
|
# cv.resizeWindow('result', 640, 480);
|
|
cv.namedWindow("boundary",0);
|
|
cv.resizeWindow("boundary", 640, 480);
|
|
cv.imshow("boundary",img)
|
|
cv.waitKey(0)
|
|
cv.destroyAllWindows() |