编了一个检测轮廓,计算中心坐标的小程序,目标是显示所有轮廓中心坐标,运行结果只能显示一个
程序如下:
import cv2 import imutils import argparse img=cv2.imread("C:\Users\RULER\Desktop\image.jpg") res1=cv2.resize(img,None,fx=2,fy=2,interpolation=cv2.INTER_CUBIC) #图象缩放 gray=cv2.cvtColor(res1,cv2.COLOR_BGR2GRAY) blurred = cv2.GaussianBlur(gray,(5,5),0) #滤波 ret,binary=cv2.threshold(blurred,60,255,cv2.THRESH_BINARY) #阈值处理 contours=cv2.findContours(binary.copy(),cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE) contours=contours[0] for c in contours: M=cv2.moments(c) cX=int(M["m10"]/M["m00"]) cY=int(M["m01"]/M["m00"]) cv2.drawContours(gray,[c],-1,(0,255,0),2) cv2.circle(gray,(cX,cY),7,(255,255,255),-1) print"坐标",(cX,cY) cv2.imshow("img",gray) cv2.waitKey(0)
运行结果
原图片
参考文章:http://chuansong.me/n/4256235...
第一次发帖,非常感谢大家的讨论
注意for语句的缩进~