1.cv2.findContours() 函数检测轮廓
import cv2
img = cv2.imread('C:/Users/Administrator/Desktop/New_Study/IMAGE/Pictures/1.jpg')
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
ret, binary = cv2.threshold(gray,127,255,cv2.THRESH_BINARY)
contours, hierarchy = cv2.findContours(binary,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
cv2.drawContours(img,contours,-1,(0,0,255),3)
=
cv2.imshow("img", img)
cv2.imwrite('C:/Users/Administrator/Desktop/New_Study/IMAGE/Pictures/findContours.jpg',img)
cv2.waitKey(0)
2.cv2.Canny()函数检测轮廓
import cv2
import numpy as np
from matplotlib import pyplot as plt
img = cv2.imread('C:/Users/Administrator/Desktop/New_Study/IMAGE/Pictures/1.jpg',1)
edges = cv2.Canny(img,100,200)
#Matplotlib显示
plt.figure(figsize=(12,8))
plt.subplot(121)
plt.imshow(img,cmap='gray')
plt.title('original')
plt.xticks([])
plt.yticks([])
plt.subplot(122)
plt.imshow(edges,cmap='gray')
plt.title('edge')
plt.xticks([])
plt.yticks([])
plt.show()
#OpenCV 显示
img = cv2.imread('C:/Users/Administrator/Desktop/New_Study/IMAGE/Pictures/1.jpg')
edges = cv2.Canny(img,100,200)
cv2.namedWindow('img',cv2.WINDOW_NORMAL)
cv2.imshow('img',img)
cv2.namedWindow('edges',cv2.WINDOW_NORMAL)
cv2.imshow('edges',edges)
cv2.waitKey()
cv2.destroyAllWindows()
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/351a2326080d8c66dd91424de162cfb9.jpeg#pic_center)
不建议用Matplotlib展示图像,失真严重,效果如下图
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/b341af5780452961f548fb8b453279c2.png#pic_center)
特别注明:本文属于Python OpenCV学习笔记,不以盈利为目的,纯手工码字不容易,若整理的笔记中,对您有所助益,麻烦点个赞或者收藏,万分感谢!如有构成侵权的地方,请联系作者删除,谢谢合作!