Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

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;
}