OpenCV HoughCircile detection with refresh window

asked 2020-07-24 04:38:12 -0500

SlyDark gravatar image

updated 2020-08-02 13:02:13 -0500

I need some help for this code!

#include <opencv2/opencv.hpp>
#include <iostream>

using namespace std;
using namespace cv;

Mat src, adaptDst;
int block_size, C;

void adaptThreshAndShow()
{
    adaptiveThreshold(src, adaptDst, 255, ADAPTIVE_THRESH_MEAN_C, THRESH_BINARY, block_size, C);

    imshow("Adaptive Thresholding", adaptDst);
}

void adaptiveThresholding1(int, void*)
{
    static int prev_block_size = block_size;

    if ((block_size % 2) == 0)
    {
        if (block_size > prev_block_size)
        {
            block_size++;
        }

        if (block_size < prev_block_size)
        {
            block_size--;
        }
    }

    if (block_size <= 1)
    {
        block_size = 3;
    }

    adaptThreshAndShow();
}

void adaptativeThresholding2(int, void*)
{
    adaptThreshAndShow();
}

int main()
{
    src = imread("lena.png", IMREAD_GRAYSCALE);
    dst = src.clone();

    namedWindow("Source", WINDOW_FREERATIO);
    namedWindow("Adaptive Thresholding", WINDOW_FREERATIO);

    block_size = 11;
    C = 2;

    createTrackbar("block_size", "Adaptive Thresholding", &block_size, 25, adaptiveThresholding1);
    createTrackbar("C", "Adaptive Thresholding", &C, 255, adaptativeThresholding2);

    adaptiveThresholding1(block_size, nullptr);
    adaptativeThresholding2(C, nullptr);

    vector<Vec3f> circles;
    HoughCircles(adaptDst, circles, HOUGH_GRADIENT, 1,
        adaptDst.rows / 16,  // change this value to detect circles with different distances to each other
        1, 1, 5, 5
    );
    for (size_t i = 0; i < circles.size(); i++)
    {
        Vec3i c = circles[i];
        Point center = Point(c[0], c[1]);
        // circle center
        circle(src, center, 1, Scalar(0, 100, 100), 3, LINE_AA);
        // circle outline
        int radius = c[2];
        circle(src, center, radius, Scalar(255, 0, 255), 3, LINE_AA);
    }

    moveWindow("Source", 0, 0);
    moveWindow("Adaptive Thresholding", 2 * src.cols, 0);

    imshow("Source", src);

    cout << "Press any key to exit...\n";
    waitKey(0);
    return 0;
}

All time i move the trackbar i need to have in the source window (refreshing detection). If i change the values in the adaptive threshold i could have new detection from the source window, but i have a probleme always i change the values the adaptive threshold take the new source of image and change it.

Sorry for my english.

sincerely. SlyDark

edit retag flag offensive close merge delete