检测中心

检测每个形状的轮廓并计算中心:

转换为灰度:cv2.cvtColor()→高斯滤波cv2.GaussianBlur()→图像二元化cv2.thresholod()

 1 import imutils
 2 import cv2
 3 
 4 #通过cv2.imread()直接获取函数
 5 img = cv2.imread("C:/Users/15212/Desktop/python/example_shapes.png")
 6 
 7 #转换为灰度图
 8 gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
 9 
10 #高斯滤波->模糊以减少高频噪音
11 blurred = cv2.GaussianBlur(gray, (5,5), 0)
12 
13 #图像二元化
14 thresh = cv2.threshold(blurred, 60, 255, cv2.THRESH_BINARY)[1]
15 
16 #显示图像
17 cv2.namedWindow("sss",cv2.WINDOW_NORMAL)
18 cv2.imshow("sss",thresh)
19 
20 cv2.waitKey(0)
21 cv2.destroyAllWindows()

输入不同的图像有不同的结果:

 

 

复现代码后:

 

 

 提问:为何自己的图片达不到这种效果?

 

posted @ 2021-07-17 18:09  樱桃窝窝头  阅读(73)  评论(0编辑  收藏  举报