Ask Your Question
0

Splitting ROI at 9 different fields

asked 2015-04-24 07:37:39 -0600

Storiy gravatar image

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;
}
edit retag flag offensive close merge delete

Comments

1

check the function here. Just have in mind that it considers that you want equal sized blocks and the number of blocks is directly relative to the dimensions of the image/roi you want to divide.

theodore gravatar imagetheodore ( 2015-04-24 08:52:40 -0600 )edit

thanks for the link and advices. will check this one now.

Storiy gravatar imageStoriy ( 2015-04-27 04:39:37 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
1

answered 2015-05-29 06:02:47 -0600

Storiy gravatar image

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
edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2015-04-24 07:37:39 -0600

Seen: 361 times

Last updated: May 29 '15