http://www.jb51.net/article/132217.htm
https://www.jianshu.com/p/4bc3349b4611
https://blog.csdn.net/sunny2038/article/details/12889059使用这个代码就可以直接使用
https://blog.csdn.net/sunny2038/article/details/12889059
https://docs.opencv.org/2.4/modules/imgproc/doc/structural_analysis_and_shape_descriptors.html?highlight=findcontours#findcontours
这两个是讲解的博客
输入给opencv的是灰度图,并且是一个经过处理的二值化的灰度图,EXTERNAL这种mode更适合我想要的得到外部轮廓
opencv的threshold函数是阈值处理函数,返回的第二个参数就是处理后的图像,127就是阈值
https://blog.csdn.net/on2way/article/details/46812121
https://blog.csdn.net/guduruyu/article/details/68059450
代码:
import cv2 img = cv2.imread('/home/sensetime/edgeBoxes-Cpp-version/output/img/000021_10.png') gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY) ret, binary = cv2.threshold(gray,127,255,cv2.THRESH_BINARY) contours, hierarchy = cv2.findContours(binary,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE) print len(contours) cv2.drawContours(img,contours,-1,(0,0,255),3) cv2.imwrite("EXTERNAL.png",img) cv2.imshow("img", img) cv2.waitKey(0)