Ask Your Question

Storiy's profile - activity

2020-07-07 21:43:50 -0600 received badge  Popular Question (source)
2018-12-29 04:35:53 -0600 received badge  Popular Question (source)
2018-10-27 08:57:53 -0600 received badge  Famous Question (source)
2017-07-21 00:45:05 -0600 received badge  Notable Question (source)
2017-02-18 13:43:22 -0600 received badge  Popular Question (source)
2016-01-09 18:19:33 -0600 marked best answer Getting coordinates from template matching
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>

int main()
{
    cv::Mat ref = cv::imread("image.png");
    cv::Mat tpl = cv::imread("template1.png");
    if (ref.empty() || tpl.empty())
        return -1;

    cv::Mat gref, gtpl;
    cv::cvtColor(ref, gref, CV_BGR2GRAY);
    cv::cvtColor(tpl, gtpl, CV_BGR2GRAY);

    cv::Mat res(ref.rows-tpl.rows+1, ref.cols-tpl.cols+1, CV_32FC1);
    cv::matchTemplate(gref, gtpl, res, CV_TM_CCOEFF_NORMED);
    cv::threshold(res, res, 0.8, 1., CV_THRESH_TOZERO);

    while (true)
    {
        double minval, maxval, threshold = 0.8;
        cv::Point minloc, maxloc;
        cv::minMaxLoc(res, &minval, &maxval, &minloc, &maxloc);

        if (maxval >= threshold)
        {
            cv::rectangle(
                ref,
                maxloc,
                cv::Point(maxloc.x + tpl.cols, maxloc.y + tpl.rows),
                CV_RGB(255,0,0), 2
            );
            cv::floodFill(res, maxloc, cv::Scalar(0), 0, cv::Scalar(.1), cv::Scalar(1.));
        }
        else
            break;
    }

    cv::imshow("reference", ref);
    cv::waitKey();
    return 0;
}

I am using this code for multiple matching to find 3 images on the screen. But how can i get coordinates from those images? I need bottom-right from the first one, bottom-left from the second and top-left from third. I understand that i am using them to draw a rectangle, but i dont know in what order it is finding the images. C:\fakepath\image.png

2015-12-27 13:10:00 -0600 received badge  Student (source)
2015-07-19 16:43:40 -0600 marked best answer Splitting ROI at 9 different fields

I am using template matching to detect a table on sample image, than use ROI to get this area in focus. Now i am needed to split this area at 9 squares. I can't use coordinates though, since there will be no const values on different images. I there a mathematic way to perform this on ROI with % or something? The code below.

int main()
{
cv::Point pointArray[4];
int i = 0;
cv::Mat ref = cv::imread("table.png");
cv::GaussianBlur(ref, ref, cv::Size(5, 5), 0, 0);
cv::Mat tpl = cv::imread("temp.png");
if (ref.empty() || tpl.empty())
    return -1;
cv::Mat gref, gtpl;
cv::cvtColor(ref, gref, CV_BGR2GRAY);
cv::cvtColor(tpl, gtpl, CV_BGR2GRAY);
cv::Mat res(ref.rows-tpl.rows+1, ref.cols-tpl.cols+1, CV_32FC1);
cv::matchTemplate(gref, gtpl, res, CV_TM_CCOEFF_NORMED);
cv::threshold(res, res, 0.8, 1., CV_THRESH_TOZERO);
while (true)
{
    double minval, maxval, threshold = 0.8;
    cv::Point minloc, maxloc;
    cv::minMaxLoc(res, &minval, &maxval, &minloc, &maxloc);
    if (maxval >= threshold)
    {
        int x = maxloc.x + tpl.cols/2;
        int y = maxloc.y + tpl.rows/2;
        pointArray[i] = cv::Point(x,y);
        i++;
    }
    else
        break;
}
cv::Rect r(pointArray[1], pointArray[2]);
cv::rectangle(ref, r, CV_RGB(255, 0, 0));
cv::Mat imgROI = ref(r);
cv::imshow("reference", ref);
cv::imshow("basicROI", imgROI);
cv::waitKey();
return 0;
}
2015-07-19 16:43:40 -0600 received badge  Self-Learner (source)
2015-07-19 16:43:40 -0600 received badge  Necromancer (source)
2015-05-30 15:15:24 -0600 asked a question Match Template in predefined areas

I am trying to optimize my code, since now it works very clunky. For now i use this:

        while (true)
{
    double minval, maxval, thresh = 0.8;
    Point minloc, maxloc;
    matchTemplate(gref, gtpl, res, CV_TM_CCOEFF_NORMED);
    threshold(res, res, 0.85, 1., CV_THRESH_TOZERO);
    minMaxLoc(res, &minval, &maxval, &minloc, &maxloc);
    if (maxval <= thresh)
    {
        resize(gtpl, gtpl, Size(gtpl.cols-1, gtpl.rows-1));
    }
    else
    {
        break;
    }

}
while (true)
{
    double minval, maxval, thresh = 0.8;
    Point minloc, maxloc;
    minMaxLoc(res, &minval, &maxval, &minloc, &maxloc);
    if (maxval >= thresh)
    {
        floodFill(res, maxloc, Scalar(0), 0, Scalar(.1), Scalar(1.));
        if(maxloc.x < 600 && maxloc.y > 800)
        {
            x = maxloc.x + gtpl.rows;
            y = maxloc.y;
        }
        else if( maxloc.x < 600 && maxloc.y < 600)
        {
            x = maxloc.x + gtpl.cols ;
            y = maxloc.y + gtpl.cols;
        }
        else
        {
            x = maxloc.x ;
            y = maxloc.y + gtpl.rows;
        }
        circle(ref, Point(x, y), 5, CV_RGB(0, 0, 255));
    }
    else
    {
        break;
    }
}

For now i define areas of searching in pixels, but if image will be diffrenet, resolution may be too and everything will mess up. Maybe someone can advice me in using % or something similar?
C:\fakepath\table.png

2015-05-29 06:02:47 -0600 answered a question Splitting ROI at 9 different fields

for those in need

//----------------------------------------------------------------------ImgDIVISON
if(!imgROI.data || imgROI.empty())
{
    cerr << "Problem Loading Image" << endl;
    return 1;
}
while (imgROI.cols % colDivisor != 0)
{
  imgROI.cols++;
}
while (imgROI.rows % rowDivisor != 0)
{
  imgROI.rows++;
}
if(imgROI.cols % colDivisor == 0 && imgROI.rows % rowDivisor == 0)
{
    for(int y = 0, i = 0; y < imgROI.cols; y += imgROI.cols / colDivisor, i++)
    {
        for(int x = 0, j = 0; x < imgROI.rows; x += imgROI.rows / rowDivisor, j++)
        {
            blocks.push_back(imgROI(Rect(y, x, (imgROI.cols / colDivisor), (imgROI.rows / rowDivisor))).clone());
            rectangle (imgROI, Point(y, x), Point(y + (imgROI.cols / colDivisor) - 1, x + (imgROI.rows / rowDivisor) - 1), CV_RGB(255, 0, 0), 1);
        }
    }
}else if(imgROI.cols % colDivisor != 0)
{
    cout << "Error: Please use another divisor for the column split." << endl;
    exit(1);
}else if(imgROI.rows % rowDivisor != 0)
{
    cout << "Error: Please use another divisor for the row split." << endl;
    exit(1);
}
//----------------------------------------------------------------------ImgDIVISON
2015-05-26 12:23:36 -0600 commented answer Resize & Template Matching

yeah, it just a test code, so i missed a lot of things while trying to make it work. i knew that top right was missing, but couldnt understand why. thanks a lot for those advices!

2015-05-21 01:35:55 -0600 asked a question Resize & Template Matching

Hi folks. I tried to use this resize function to help me in template matching, since i dont know the size of a template image on my main image, but somewhy it goes wrong and find only 2/3 same images. Still works fine with 1:1 template image. Maybe you guys can help me here? Here's the code and images C:\fakepath\table.png C:\fakepath\temp3.png

int main()
{
    const int colDivisor = 21;
    const int rowDivisor = 13;
    int blacks[rowDivisor][colDivisor];
    int blockCount = 0;
    Mat imgROI;
    Point pointArray[3];
    int i = 0;
    Mat ref = imread("C:/Dev/QtBuild/try/debug/table.png");
    GaussianBlur(ref, ref, Size(5, 5), 0, 0);
    Mat tpl = imread("C:/Dev/QtBuild/try/debug/temp5.png");
    GaussianBlur(tpl, tpl, Size(5, 5), 0, 0);
    if (ref.empty() || tpl.empty())
        return -1;
    Mat gref, gtpl;
    cvtColor(ref, gref, CV_BGR2GRAY);
    cvtColor(tpl, gtpl, CV_BGR2GRAY);
    Mat res(ref.rows-tpl.rows+1, ref.cols-tpl.cols+1, CV_32FC1);
    int x = 0;
    int y = 0;
    resize(gtpl, gtpl, Size2i(gtpl.cols-1, gtpl.rows-1));
    while (true)
    {
        double minval, maxval, thresh = 0.8;
        Point minloc, maxloc;
        matchTemplate(gref, gtpl, res, CV_TM_CCOEFF_NORMED);
        threshold(res, res, 0.8, 1., CV_THRESH_TOZERO);
        minMaxLoc(res, &minval, &maxval, &minloc, &maxloc);
        if (maxval < thresh)
        {
            resize(gtpl, gtpl, Size(gtpl.cols-1, gtpl.rows-1));
        }
        else
        {
            break;
        }

    }
    matchTemplate(gref, gtpl, res, CV_TM_CCOEFF_NORMED);
    threshold(res, res, 0.8, 1., CV_THRESH_TOZERO);
    while (true)
    {
        double minval, maxval, thresh = 0.8;
        Point minloc, maxloc;

        minMaxLoc(res, &minval, &maxval, &minloc, &maxloc);
        if (maxval >= thresh)
        {
            floodFill(res, maxloc, Scalar(0), 0, Scalar(.1), Scalar(1.));
            if(maxloc.x < 100 && maxloc.y > 500)
            {
                x = maxloc.x+ gtpl.rows;
                y = maxloc.y;
            }
            else if( maxloc.x < 100 && maxloc.y < 100)
            {
                x = maxloc.x ;
                y = maxloc.y+ gtpl.cols;
            }
            else
            {
                x = maxloc.x ;
                y = maxloc.y + gtpl.rows;
            }
            pointArray[i] = Point(x,y);
            i++;
            circle(ref, Point(x, y), 5, CV_RGB(0, 0, 255));
        }
        else
        {
            break;
        }
    }
}
2015-05-13 08:34:58 -0600 commented question No SURF Feature Detector

@NitinPrasad, how exactly do you fixed it?

2015-05-13 08:21:42 -0600 commented question Image Pyramids & Template Matching

btw, can i use this resize function? and somewhy my opencv dont see the libraries, although they are connected, so i keep getting the "undefined reference" to any SURF function

2015-05-13 07:22:44 -0600 commented question Image Pyramids & Template Matching

than my approach is completely wrong. i need to find the template on image, and i dont know the size of the one on the image, since every time i will match them in my program, size of it will be different.

2015-05-13 05:20:06 -0600 answered a question Counting black & white pixels with a threshold

Just in case someone need's that, i add the code

            Mat  partROI;
            cvtColor(partROI, partROI, CV_BGR2GRAY);
            int count_white = 0;
            int count_black = 0;
            threshold( partROI, partROI, 200, 255, THRESH_BINARY );
            count_white = countNonZero(partyROI);
            count_black = partyROI.cols * partyROI.rows - count_white;
            cout << "white pixels = " << count_white << endl;
            cout << "black pixels = " << count_black << endl;
            cout << endl;
            imshow("Image", partROI);
2015-05-13 04:08:02 -0600 commented question Image Pyramids & Template Matching

thanks, i am looking up to this one right now. but i have a problem with a right number of layers that i send to the function. if i add too much, i just get my PC lagging, and any low value just dont found the template, if that is the case of a problem, ofc. edit: increasing level value dont seem to help at all.

2015-05-13 04:03:17 -0600 received badge  Editor (source)
2015-05-12 08:35:35 -0600 asked a question Image Pyramids & Template Matching

I am trying to get into Image Pyramids, and while googling found this turorial. It compiles, and founds the 1:1 template, but when i am trying to increase template image, at least for 10 px, function stops finding it on image. What is wrong?

2015-05-12 05:56:23 -0600 commented question Chamfer Matching match score

first of all, you can use cvtColor(input_image, output_image, CV_BGR2GRAY); to transfer image to binary, also, to count only black and white pixels, you will need to threshold already binary image with threshold( input_image, output_image, 200, 255, THRESH_BINARY );, after that with a help of countNonZero you can count them. That will only work for black-n-white images so keep it on mind.

        count_white = countNonZero(partyROI);
        count_black = input_image.cols * input_image.rows - count_white;

but to work only with a selected region you need something like this, i think.

2015-05-05 05:44:42 -0600 commented question Chamfer Matching match score

I think, if you transfer both images to binary, you can count pixels in matching points using mask from original image. Btw if you are trying to compare images, you probably should use contour images, not colored one's.

2015-04-30 08:33:38 -0600 commented question Counting black & white pixels with a threshold

i used cvtColor(ref, gref, CV_BGR2GRAY); on my main RGB image and didn't thought i will have to use that on ROI of the same image. So, yeah. Thanks for all the help btw.

2015-04-30 08:24:08 -0600 received badge  Autobiographer
2015-04-30 08:16:07 -0600 asked a question Decreasing ROI

Is there a way to make already selected ROI a bit (5%-10%) smaller. I can't just set original ROI smaller, since i get it as a result of function. Basicly, i need to do something like this:image description

2015-04-30 07:49:34 -0600 commented question Counting black & white pixels with a threshold

no, error doesn't show up anymore cvtColor(partROI, partROI, CV_BGR2GRAY); helped me to fix that. And yes, i were first counting the white pixels with THRESH_BINARY and then inverting them with THRESH_BINARY_INV and counting again the black ones. But you are right and this way will be more efficient

            threshold( partROI, partROI, 240, 255, THRESH_BINARY );
            count_white = countNonZero(partROI);
            count_black = partROI.cols * partROI.rows - count_white2;
2015-04-30 05:14:35 -0600 commented question Counting black & white pixels with a threshold

did it for now with this code.

threshold( partROI1, partROI1, 20, 255, THRESH_BINARY );
count_black = countNonZero(partROI1);
threshold( partROI1, partROI1, 240, 255, THRESH_BINARY_INV );
count_white= countNonZero(partROI1);

since i were using a ROI of an image

cvtColor(partROI1, partROI1, CV_BGR2GRAY);

helped