为什么“圈子检测”检测到的圈子比那里的多?

时间:2019-02-01 04:27:35

标签: opencv

我正在使用以下功能来检测图像。但是,它检测到数千个圆圈,而不是16个。如何确定它只能检测到我看到的东西?改变半径或相对强度没有任何区别。 我使用的图像是这样的:enter image description here

Bitmap ImageBitmap = (Bitmap)pictureBox1.Image;

            var filter = new FiltersSequence(new IFilter[]
            {
                      Grayscale.CommonAlgorithms.BT709,
                      new Threshold(0x40)
            });
            var binaryImage = filter.Apply(ImageBitmap);

       //     for (int i = 0; i < 10000; i++)
            {

          //      System.Drawing.Image image = System.Drawing.Image.FromFile(imagePath);
                //      GrayBMP_File.CreateGrayBitmapFile(image, "c:/path/to/8bpp/image.bmp");




                //    Bitmap ImageBitmap = Convert.Gra ImageBitmap.Con

                HoughCircleTransformation circleTransform = new HoughCircleTransformation(50);
                // apply Hough circle transform
                circleTransform.ProcessImage(binaryImage);
                Bitmap houghCirlceImage = circleTransform.ToBitmap();
                // get circles using relative intensity
                HoughCircle[] circles = circleTransform.GetCirclesByRelativeIntensity(0.9);
                int numCircles = circleTransform.CirclesCount;
                label1.Text = numCircles.ToString();
                pictureBox1.Image = houghCirlceImage;
                System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(ImageBitmap);
                foreach (HoughCircle circle in circles)
                {
                    g.DrawEllipse(Pens.Green, circle.X, circle.Y, 10,10);
                }
                pictureBox1.Image = ImageBitmap;
//                ImageBitmap.Dispose();
//                binaryImage.Dispose();
            }

1 个答案:

答案 0 :(得分:1)

尝试使用here中的此python解决方案:

import cv2
import numpy as np

img = cv2.imread('test.jpg',0)
cimg = cv2.cvtColor(img,cv2.COLOR_GRAY2BGR)

circles = cv2.HoughCircles(img,cv2.HOUGH_GRADIENT,1,20,
                            param1=50,param2=30,minRadius=0,maxRadius=0)

circles = np.uint16(np.around(circles))
d=1
for i in circles[0,:]:
    # draw the outer circle
    cv2.circle(cimg,(i[0],i[1]),i[2],(0,255,0),2)
    # draw the center of the circle
    cv2.circle(cimg,(i[0],i[1]),2,(0,0,255),3)

    crop_img=img[i[0]-i[2]-2:i[0]+i[2]+2,i[1]-i[2]-2:i[1]+i[2]+2]
    cv2.imshow('cropped circle',crop_img)
    cv2.imwrite('test_%d.png'%d,crop_img)
    cv2.waitKey(0)
    d+=1

cv2.imshow('detected circles',cimg)
print(len(circles[0,:]))
cv2.waitKey(0)
cv2.destroyAllWindows()

输出:

16