# Ball Detection Noise

**URL:** <https://forum.opencv.org/t/ball-detection-noise/1498>\
**Category:** Uncategorized\
**Tags:** imgproc\
**Created:** [February 11, 2021, 9:25pm UTC](https://forum.opencv.org/t/ball-detection-noise/1498 "2021-02-11T21:25:32Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Galactic3D](https://avatars.discourse-cdn.com/v4/letter/g/bbe5ce/32.png) [@Galactic3D](https://forum.opencv.org/u/Galactic3D)\
**Post date:** [February 11, 2021, 9:25pm UTC](https://forum.opencv.org/t/ball-detection-noise/1498/1 "2021-02-11T21:25:32Z")

</div>

Hello,

I am attempting to detect a yellow ball ([as seen here](https://drive.google.com/file/d/1dwLtPg-jt9CweEzB9I46n0ajSj0DZ7dh/view?usp=sharing)) using the hough circle transform. However, after applying my mask using inRange, I still have a lot of noise and the hough circle transform is detecting non-existent circles.

[Here is an image with the mask applied.](https://drive.google.com/file/d/18vBZXX6KsPs5GhtFaFht3BJZFQosvTKI/view?usp=sharing)

My HSV values are:  
lower - 20, 0, 100  
upper - 40, 255, 255

Thank you in advanced for your help!

---

<div class="post-metadata">

**Author:** ![naser\_piltan](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/naser_piltan/32/391_2.png) [@naser\_piltan](https://forum.opencv.org/u/naser_piltan)\
**Post date:** [February 11, 2021, 10:56pm UTC](https://forum.opencv.org/t/ball-detection-noise/1498/2 "2021-02-11T22:56:31Z")

</div>

Hi.  
I couldn’t reproduce your binary image with lower - 20, 0, 100 and upper - 40, 255, 255, I think you have done some mistakes. Please tell us what was your right values.

Btw i did these steps on your provided binary image :  
1 - I did erosion on your binary image , so it removed most of noises from your image.  
There is a good example for erosion operation in [official docs](https://www.ccoderun.ca/programming/doxygen/opencv/tutorial_erosion_dilatation.html)

 ![erosion_screenshot_12.02.2021](https://us1.discourse-cdn.com/flex020/uploads/opencv/original/1X/13fae51c4f3d1eb1fe127d8a4781cb5f5a59b76e.png)

2- You can now find the ball circle with hough transform :

 ![erosion_screenshot_12.0212](https://us1.discourse-cdn.com/flex020/uploads/opencv/original/1X/196579cca2297533146e37a0ba41278d0dced6a5.png)

This is my my code . Hope it would be helpful.

> Mat img = imread(“/home/eaglesoft/Downloads/Screenshot (58).png”);  
> Mat gray\_img;  
> cvtColor(img,gray\_img,COLOR\_BGR2GRAY);  
> Mat erosion\_dst;  
> int erosion\_type = MORPH\_RECT;  
> int erosion\_size = 2;  
> Mat element = getStructuringElement( erosion\_type,  
> Size( 2_erosion\_size + 1, 2_erosion\_size+1 ),  
> Point( erosion\_size, erosion\_size ) );  
> erode( gray\_img, erosion\_dst, element );  
> vector circles;  
> HoughCircles(erosion\_dst, circles, HOUGH\_GRADIENT, 1, img.rows/64, 200, 10,75, 120);  
> for(size\_t i=0; i\<circles.size(); i++) {  
> Point center(cvRound(circles[i][0]), cvRound(circles[i][1]));  
> int radius = cvRound(circles[i][2]);  
> circle(img, center, radius, Scalar(0, 0, 255), 2, 8, 0);  
> }  
> imshow(“erosion”,img);  
> waitKey(0);

---

<div class="post-metadata">

**Author:** ![crackwitz](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/crackwitz/32/14_2.png) [@crackwitz](https://forum.opencv.org/u/crackwitz)\
**Post date:** [February 12, 2021, 12:33am UTC](https://forum.opencv.org/t/ball-detection-noise/1498/3 "2021-02-12T00:33:17Z")

</div>

I investigated your picture some.

the issue here is that the background (wood) is very similar in color to your ball. you need a different ball or different background.

---

<div class="post-metadata">

**Author:** ![FilipBaas](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/filipbaas/32/802_2.png) [@FilipBaas](https://forum.opencv.org/u/FilipBaas)\
**Post date:** [February 12, 2021, 11:39am UTC](https://forum.opencv.org/t/ball-detection-noise/1498/4 "2021-02-12T11:39:37Z")

</div>

Do not forget, that HoughCircles also works with grayscale image so binarisation of image si not necessary.

Result:

 ![ballRes](https://us1.discourse-cdn.com/flex020/uploads/opencv/original/2X/0/02d18a377aa0b288757916cc98482a59e15e2a15.jpeg)

CPP example:

```
// Read ball image
cv::Mat ball = cv::imread("ball.png");

// Convert ball image to grayscale
cv::Mat gray;
cv::cvtColor(ball, gray, cv::COLOR_BGR2GRAY);

// Detect ball
vector<cv::Vec3f> circles;
cv::HoughCircles(gray, circles, cv::HOUGH_GRADIENT, 1,
    gray.cols, // Minimal circle distance set to width of frame to find only one ball
    100, 20, 
    70, // Minimal radius of ball [px]
    100 // Maximal radius of ball [px]
);

// Draw found circles
for (const auto& c: circles)
{
    cv::Point center = cv::Point(c[0], c[1]);
    // Circle center
    cv::circle(ball, center, 1, cv::Scalar(0, 0, UCHAR_MAX), 3, cv::LINE_AA);

    // Circle outline
    int radius = c[2];
    cv::circle(ball, center, radius, cv::Scalar(0, 0, 0), 2, cv::LINE_AA);
    cv::circle(ball, center, radius, cv::Scalar(UCHAR_MAX, UCHAR_MAX, UCHAR_MAX), 1, cv::LINE_AA);
}
```
