# Struggling with Processing this Image for Detection

**URL:** <https://forum.opencv.org/t/struggling-with-processing-this-image-for-detection/7687>\
**Category:** C++\
**Tags:** object-detection\
**Created:** [February 22, 2022, 9:50pm UTC](https://forum.opencv.org/t/struggling-with-processing-this-image-for-detection/7687 "2022-02-22T21:50:49Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Falcon](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/falcon/32/4407_2.png) [@Falcon](https://forum.opencv.org/u/Falcon)\
**Post date:** [February 22, 2022, 9:50pm UTC](https://forum.opencv.org/t/struggling-with-processing-this-image-for-detection/7687/1 "2022-02-22T21:50:49Z")

</div>

![testedit](https://us1.discourse-cdn.com/flex020/uploads/opencv/original/2X/7/78b403fa8e35d78551442efe1ec2aeff916b099c.jpeg)

This is the major culprit, I am not sure how to go about processing these images, do note, I’m still pretty new to the OpenCV libraries. I’ve tried binary color but that still leaves some really large gaps. Afterwards I found some interesting code that fills and tries to fill out objects but that uhhh hasn’t been working to well in my case. Any tips or ideas?

```auto
void fillHoles(cv::Mat& img)
{
    if (img.channels() > 1)
    {
        std::cout << "fillHoles !!! Image must be single channel" << std::endl;
        return;
    }

    cv::Mat holes = img.clone();
    cv::floodFill(holes, cv::Point2i(0,0), cv::Scalar(1));

    for (int i = 0; i < (img.rows * img.cols); i++)
        if (holes.data[i] == 0)
            img.data[i] = 0;
}

int main()
{
    // Load input image
    cv::Mat input = cv::imread("testedit2.png");
    if (input.empty())
    {
        std::cout << "!!! Failed to open image" << std::endl;
        return -1;
    }

    // Convert it to grayscale
    cv::Mat gray;
    cv::cvtColor(input, gray, cv::COLOR_BGR2GRAY);

    // Threshold the grayscale image for segmentation purposes
    cv::Mat thres;
    cv::threshold(gray, thres, 110, 255, cv::THRESH_BINARY);
    //cv::imwrite("threhsold.jpg", thres);
    imshow("test", thres);
    waitKey(0);

    // Dirty trick to join nearby segments
    cv::Mat element = cv::getStructuringElement(cv::MORPH_ELLIPSE, cv::Size(15, 15));
    cv::morphologyEx(thres, thres, cv::MORPH_OPEN, element);
    //cv::imwrite("morph.jpg", thres);
    imshow("test", thres);
    waitKey(0);

    // Fill the holes inside the segments
    fillHoles(thres);
    bitwise_not(thres, thres);
    //cv::imwrite("filled.jpg", thres);
    imshow("test", thres);
    waitKey(0);

```

---

<div class="post-metadata">

**Author:** ![berak](https://avatars.discourse-cdn.com/v4/letter/b/85f322/32.png) [@berak](https://forum.opencv.org/u/berak)\
**Post date:** [February 23, 2022, 7:34am UTC](https://forum.opencv.org/t/struggling-with-processing-this-image-for-detection/7687/2 "2022-02-23T07:34:19Z")

</div>

_what_ are you trying to detect ?

also, can you try to change the lighting, so you get less “specular highlights”

---

<div class="post-metadata">

**Author:** ![Falcon](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/falcon/32/4407_2.png) [@Falcon](https://forum.opencv.org/u/Falcon)\
**Post date:** [March 4, 2022, 10:25pm UTC](https://forum.opencv.org/t/struggling-with-processing-this-image-for-detection/7687/3 "2022-03-04T22:25:31Z")

</div>

I’m trying to detect those spheres within the channel, and yes I have been working to see if I can fix the lighting. I’d help a great deal I’m sure

---

<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:** [March 5, 2022, 10:28am UTC](https://forum.opencv.org/t/struggling-with-processing-this-image-for-detection/7687/4 "2022-03-05T10:28:33Z")

</div>

each “sphere” reflects that light ring from around the camera.

take a crop of one such instance, but not too closely cropped, with enough area around it of course.

`matchTemplate` should be able to find all the instances.

---

<div class="post-metadata">

**Author:** ![Falcon](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/falcon/32/4407_2.png) [@Falcon](https://forum.opencv.org/u/Falcon)\
**Post date:** [March 8, 2022, 11:23pm UTC](https://forum.opencv.org/t/struggling-with-processing-this-image-for-detection/7687/5 "2022-03-08T23:23:06Z")

</div>

That’s a good idea, but I was thinking about using frame by frame image subtraction to find it as I will want to be doing it for each frame of the video. I’m worried about the image template requiring some pretty aggressive calibration in the practical application. I’ve been testing subtracting two MAT’s based on a frame difference but that hasn’t been giving me too much over time
