完整的代码,实现了最小的矩形,圆形,随意矩形
来源
https://zhuanlan.zhihu.com/p/38739563
import cv2
import numpy as np
"""
REFER: https://hub.packtpub.com/opencv-detecting-edges-lines-shapes/
2018-06-30 Yonv1943
2018-07-01 comment to test.png
2018-07-01 gray in threshold, hierarchy
2018-07-01 draw_approx_hull_polygon() no [for loop]
2018-11-24
"""
def draw_contours(img, cnts): # conts = contours
img = np.copy(img)
img = cv2.drawContours(img, cnts, -1, (0, 255, 0), 2)
return img
def draw_min_rect_circle(img, cnts): # conts = contours
# img = np.copy(img)
img = np.zeros(img.shape, dtype=np.uint8)
for cnt in cnts:
x, y, w, h = cv2.boundingRect(cnt)
# cv2.rectangle(img, (x, y), (x + w, y + h), (255, 0, 0), 2)
if w * h <= 16:
print(1)
for i in range(1, w):
for j in range(1, h):
print(x, y)
print(i, j)
img[x + i, y + j] = [0, 0, 0]
# print("success")
# msg1 = "定点x,y为" + str(x) + ' ' + str(y)
# msg2 = "长宽w,h为" + str(w) + " " + str(h)<