打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
python

I have an image to process.I need detect all the circles in the image.Here is it.

And here is my code.

import cv2import cv2.cv as cvimg = cv2.imread(imgpath)cv2.imshow("imgorg",img)gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)cv2.imshow("gray",gray)ret,thresh = cv2.threshold(gray, 199, 255, cv.CV_THRESH_BINARY_INV)cv2.imshow("thresh",thresh)cv2.waitKey(0)cv2.destrotAllWindows()

Then,I got a image like this.

And I tried to use erode and dilate to divided them into single.But it doesnt work.My question is how to divide these contacted circles into single,so i can detect them.

According to @Micka's idea,I tried to process the image in following way,and here is my code.

import cv2import cv2.cv as cvimport numpy as npdef findcircles(img,contours):    minArea = 300;    minCircleRatio = 0.5;    for  contour  in contours:        area = cv2.contourArea(contour)        if area < minArea:             continue        (x,y),radius = cv2.minEnclosingCircle(contour)        center = (int(x),int(y))        radius = int(radius)        circleArea = radius*radius*cv.CV_PI;        if area/circleArea < minCircleRatio:             continue;        cv2.circle(img, center, radius, (0, 255, 0), 2)        cv2.imshow("imggg",img)img = cv2.imread("a.png")cv2.imshow("org",img)gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)ret,threshold = cv2.threshold(gray, 199, 255,cv. CV_THRESH_BINARY_INV)cv2.imshow("threshold",threshold)blur = cv2.medianBlur(gray,5)cv2.imshow("blur",blur)laplacian=cv2.Laplacian(blur,-1,ksize = 5,delta = -50)cv2.imshow("laplacian",laplacian)kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE,(7,7))dilation = cv2.dilate(laplacian,kernel,iterations = 1)cv2.imshow("dilation", dilation)result= cv2.subtract(threshold,dilation) cv2.imshow("result",result)contours, hierarchy = cv2.findContours(result,cv2.RETR_LIST,cv2.CHAIN_APPROX_NONE)findcircles(gray,contours)

But I dont get the same effect as @Micka's.I dont know which step is wrong.

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
三个OpenCV目标分割计数实例(附源码)
OpenCV探索之路(十一):轮廓查找和多边形包围轮廓
python+opencv图像处理(二)
Python-OpenCV 10. 图像边缘算法
RotatedRect 校正带旋转角度的矩形
第五节、轮廓检测、直线和圆、多边形检测
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服