OpenCV点目标检测不能找到所有目标,并且发现的圆偏移 [英] OpenCV dot target detection not finding all targets, and found circles are offset
问题描述
我试图检测黑/白点目标的中心,如图所示。
我试图使用cv2.HoughCircles方法,但是1,我只能检测2到3个目标,和2,当我将找到的圆形绘制回图像时,它们总是略微偏移。 / p>
我使用错误的方法吗?我应该使用findContours还是完全不同的?
以下是我的代码:
import cv2
from cv2 import cv
import os
import numpy as np
def showme(pic):
cv2.imshow ',pic)
cv2.waitKey()
cv2.destroyAllWindows()
im = cv2.imread('small_test.jpg')
gray = cv2.cvtColor(im,cv2.COLOR_BGR2GRAY)
#我试过blur,bw,tr ...都给我不好的结果。
blur = cv2.GaussianBlur(gray,(3,3),0)
n,bw = cv2.threshold(blur,120,255,cv2.THRESH_BINARY)
tr = cv2 .adaptiveThreshold(blur,255,0,1,11,2)
circles = cv2.HoughCircles(gray,cv.CV_HOUGH_GRADIENT,3,100,None,200,100,5,16)
try:
n = np.shape(circles)
circles = np.reshape(circles,(n [1],n [2]))
打印圆
for circle in circles:
cv2.circle(im,(circle [0],circle [1]),circle [2],(0,0,255))
showme
except:
printno cicles found
输出:
播放我在另一个帖子,我可以实现更好的结果:
这是关于参数。
在这个程序中有三个重要的函数,你应该尝试: cvSmooth()
, cvCanny()
和 cvHoughCircles()
。
这里是C代码:
IplImage * img = NULL;
if((img = cvLoadImage(argv [1]))== 0)
{
printf(cvLoadImage failed\\\
);
}
IplImage * grey = cvCreateImage(cvGetSize(img),IPL_DEPTH_8U,1);
CvMemStorage * storage = cvCreateMemStorage(0);
cvCvtColor(img,gray,CV_BGR2GRAY);
//这样做是为了防止检测到大量的虚假圆圈
cvSmooth(gray,gray,CV_GAUSSIAN,7,9);
IplImage * canny = cvCreateImage(cvGetSize(img),IPL_DEPTH_8U,1);
IplImage * rgbcanny = cvCreateImage(cvGetSize(img),IPL_DEPTH_8U,3);
cvCanny(gray,canny,40,240,3);
CvSeq * circles = cvHoughCircles(gray,storage,CV_HOUGH_GRADIENT,2,gray-> height / 8,120,10,2,25);
cvCvtColor(canny,rgbcanny,CV_GRAY2BGR);
for(size_t i = 0; i< circles-> total; i ++)
{
//舍入浮点数到int
float * p =(float *)cvGetSeqElem(circles,i);
cv :: point center(cvRound(p [0]),cvRound(p [1]));
int radius = cvRound(p [2]);
//画圆圈中心
cvCircle(rgbcanny,center,3,CV_RGB(0,255,0),-1,8,0);
//绘制圆形轮廓
cvCircle(rgbcanny,center,radius + 1,CV_RGB(0,0,255),2,8,0);
printf(x:%d y:%d r:%d \\\
,center.x,center.y,radius);
}
cvNamedWindow(circles,1);
cvShowImage(circles,rgbcanny);
cvSaveImage(out.png,rgbcanny);
cvWaitKey(0);
我相信你有掌握Python的能力。
I'm trying to detect the center of black/white dot targets, like in this picture. I've tried to use the cv2.HoughCircles method but 1, am only able to detect 2 to 3 targets, and 2, when I plot the found circles back onto the image, they're always offset slightly.
Am I using the wrong method? Should I be using the findContours or something completely different?
Here is my code:
import cv2
from cv2 import cv
import os
import numpy as np
def showme(pic):
cv2.imshow('window',pic)
cv2.waitKey()
cv2.destroyAllWindows()
im=cv2.imread('small_test.jpg')
gray=cv2.cvtColor(im,cv2.COLOR_BGR2GRAY)
#I've tried blur,bw,tr... all give me poor results.
blur = cv2.GaussianBlur(gray,(3,3),0)
n,bw = cv2.threshold(blur,120,255,cv2.THRESH_BINARY)
tr=cv2.adaptiveThreshold(blur,255,0,1,11,2)
circles = cv2.HoughCircles(gray, cv.CV_HOUGH_GRADIENT, 3, 100, None, 200, 100, 5, 16)
try:
n = np.shape(circles)
circles=np.reshape(circles,(n[1],n[2]))
print circles
for circle in circles:
cv2.circle(im,(circle[0],circle[1]),circle[2],(0,0,255))
showme(im)
except:
print "no cicles found"
And this is my current output:
Playing the code I wrote in another post, I was able to achieve a slightly better result:
It's all about the parameters. It always is.
There are 3 important functions that are called in this program that you should experiment with: cvSmooth()
, cvCanny()
, and cvHoughCircles()
. Each of them has the potential to change the result drastically.
And here is the C code:
IplImage* img = NULL;
if ((img = cvLoadImage(argv[1]))== 0)
{
printf("cvLoadImage failed\n");
}
IplImage* gray = cvCreateImage(cvGetSize(img), IPL_DEPTH_8U, 1);
CvMemStorage* storage = cvCreateMemStorage(0);
cvCvtColor(img, gray, CV_BGR2GRAY);
// This is done so as to prevent a lot of false circles from being detected
cvSmooth(gray, gray, CV_GAUSSIAN, 7, 9);
IplImage* canny = cvCreateImage(cvGetSize(img),IPL_DEPTH_8U,1);
IplImage* rgbcanny = cvCreateImage(cvGetSize(img),IPL_DEPTH_8U,3);
cvCanny(gray, canny, 40, 240, 3);
CvSeq* circles = cvHoughCircles(gray, storage, CV_HOUGH_GRADIENT, 2, gray->height/8, 120, 10, 2, 25);
cvCvtColor(canny, rgbcanny, CV_GRAY2BGR);
for (size_t i = 0; i < circles->total; i++)
{
// round the floats to an int
float* p = (float*)cvGetSeqElem(circles, i);
cv::Point center(cvRound(p[0]), cvRound(p[1]));
int radius = cvRound(p[2]);
// draw the circle center
cvCircle(rgbcanny, center, 3, CV_RGB(0,255,0), -1, 8, 0 );
// draw the circle outline
cvCircle(rgbcanny, center, radius+1, CV_RGB(0,0,255), 2, 8, 0 );
printf("x: %d y: %d r: %d\n",center.x,center.y, radius);
}
cvNamedWindow("circles", 1);
cvShowImage("circles", rgbcanny);
cvSaveImage("out.png", rgbcanny);
cvWaitKey(0);
I trust you have the skills to port this to Python.
这篇关于OpenCV点目标检测不能找到所有目标,并且发现的圆偏移的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!