复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47import cv2 import numpy as np img = cv2.imread('6.png') #img = cv2.imread('3.jpg') imgray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) ret, thresh = cv2.threshold(imgray, 127, 255, 0) '''image, contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) 上句会出现下面的错误 ValueError: not enough values to unpack (expected 3, got 2)''' #修正代码如下 contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) cv2.imshow('imageshow', img) # 显示返回值image,其实与输入参数的thresh原图没啥区别 #cv2.waitKey(0) #注释掉就可以同时显示 '''核心 cv2.drawContours(image, contours, contourIdx, color, thickness=None, lineType=None, hierarchy=None, maxLevel=None, offset=None) 第一个参数image表示目标图像, 第二个参数contours表示输入的轮廓组,每一组轮廓由点vector构成, 第三个参数contourIdx指明画第几个轮廓,如果该参数为负值,则画全部轮廓, 第四个参数color为轮廓的颜色, 第五个参数thickness为轮廓的线宽,如果为负值或CV_FILLED表示填充轮廓内部, 第六个参数lineType为线型, 第七个参数为轮廓结构信息, 第八个参数为maxLevel''' #img = cv2.drawContours(img, contours, -1, (225, 225, 0), 5) # img为三通道才能显示轮廓 for c in contours: if cv2.contourArea(c) > 1200: (x, y, w, h) = cv2.boundingRect(c) #img = cv2.drawContours(img, contours, -1, (225, 225, 0), 5) # img为三通道才能显示轮廓 img = cv2.drawContours(img, [c], -1, (255, 225, 0), 5) ''' cv2.rectangle(img, pt1, pt2, color[, thickness[, lineType[, shift]]]) img:要画的圆所在的矩形或图像 pt1:矩形左上角的点 pt2:矩形右下角的点 color:线条颜色,如 (0, 0, 255) 红色,BGR thickness:线条宽度 lineType:8 (or omitted) : 8-connected line 4:4-connected line CV_AA - antialiased line shift:坐标点小数点位数 ''' img = cv2.rectangle(img, (x, y), (x + w, y + h), (0, 255, 0), 1) #img = cv2.fillPoly(img, [c], (255, 255, 255)) # 填充 cv2.imshow('drawimg', img) cv2.waitKey(0) cv2.destroyAllWindows()
结果:
最后
以上就是朴素板凳最近收集整理的关于OpenCv绘制轮廓+边框的全部内容,更多相关OpenCv绘制轮廓+边框内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复