First time here? Check out the FAQ!

Ask Your Question
0

Splitting ROI at 9 different fields

asked Apr 24 '15

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;
}
Preview: (hide)

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 (Apr 24 '15)edit

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

Storiy gravatar imageStoriy (Apr 27 '15)edit

1 answer

Sort by » oldest newest most voted
1

answered May 29 '15

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
Preview: (hide)

Question Tools

1 follower

Stats

Asked: Apr 24 '15

Seen: 397 times

Last updated: May 29 '15