Ask Your Question

Revision history [back]

Ok there is a problem but I don't think it is in your code. I have already post a solution. Now CV_TM_CCOEFF_NORMED should give you, good results but I have some doubt. I normalise signal before matchTemplate : that's only difference with your code. It shouldn't be necessary with CV_TM_CCOEFF_NORMED but may be a bug ? If you read doc w and h for image T and I shouldn't be equal.

int main(void) {
    Mat img = imread("board.jpg", IMREAD_GRAYSCALE);
    Mat pen = imread("pen.jpg", IMREAD_GRAYSCALE);
    Mat black(9, 9, CV_8UC1, Scalar(0));
    Mat result;
    Mat penF, imgF;
    img.convertTo(imgF, CV_32F, 1,-mean(img)[0]); // Image with null mean
    pen.convertTo(penF, CV_32F,1, -mean(pen)[0]); // Image with null mean
    matchTemplate(imgF, penF, result, TM_CCOEFF_NORMED);
    imshow("result", result);
    Mat plus = result > 0.9;// why 0.92? try and test
    Mat mask;
    dilate(plus, mask, pen);
    img = imread("board.jpg", IMREAD_COLOR);
    img(Rect(pen.cols / 2, pen.rows / 2, plus.cols, plus.rows)).setTo(Vec3b(255, 0, 255), mask);
    imshow("plus", img);
    waitKey(0);
    return 0;
}

Ok there is a problem but I don't think it is in your code. I have already post a solution. Now CV_TM_CCOEFF_NORMED should give you, good results but I have some doubt. I normalise signal before matchTemplate : that's only difference with your code. It shouldn't be necessary with CV_TM_CCOEFF_NORMED but may be a bug ? If you read doc w and h for image T and I shouldn't be equal.

int main(void) {
    Mat img = imread("board.jpg", IMREAD_GRAYSCALE);
    Mat pen = imread("pen.jpg", IMREAD_GRAYSCALE);
    Mat black(9, 9, CV_8UC1, Scalar(0));
    Mat result;
    Mat penF, imgF;
    img.convertTo(imgF, CV_32F, 1,-mean(img)[0]); // Image with null mean
    pen.convertTo(penF, CV_32F,1, -mean(pen)[0]); // Image with null mean
    matchTemplate(imgF, penF, result, TM_CCOEFF_NORMED);
    imshow("result", result);
    Mat plus = result > 0.9;// 0.88;// why 0.92? 0.88? try and test
    Mat mask;
    dilate(plus, mask, pen);
    img = imread("board.jpg", IMREAD_COLOR);
    img(Rect(pen.cols / 2, pen.rows / 2, plus.cols, plus.rows)).setTo(Vec3b(255, 0, 255), mask);
    imshow("plus", img);
    waitKey(0);
    return 0;
}

Ok there is a problem but I don't think it is in your code. I have already post a solution. Now CV_TM_CCOEFF_NORMED should give you, good results but I have some doubt. I normalise signal before matchTemplate : that's only difference with your code. It shouldn't be necessary with CV_TM_CCOEFF_NORMED but may be a bug ? If you read doc w and h for image T and I shouldn't be equal.

int main(void) {
    Mat img = imread("board.jpg", IMREAD_GRAYSCALE);
    Mat pen = imread("pen.jpg", IMREAD_GRAYSCALE);
    Mat black(9, 9, CV_8UC1, Scalar(0));
    Mat result;
    Mat penF, imgF;
    img.convertTo(imgF, CV_32F, 1,-mean(img)[0]); // Image with null mean
    pen.convertTo(penF, CV_32F,1, -mean(pen)[0]); // Image with null mean
    matchTemplate(imgF, penF, result, TM_CCOEFF_NORMED);
    imshow("result", result);
    Mat plus = result > 0.88;// why 0.88? try and test
    Mat mask;
    dilate(plus, mask, pen);
    img = imread("board.jpg", IMREAD_COLOR);
    img(Rect(pen.cols / 2, pen.rows / 2, plus.cols, plus.rows)).setTo(Vec3b(255, 0, 255), mask);
    imshow("plus", img);
    waitKey(0);
    return 0;
}

Why thereshold is not equal for all pens : as your image is in jpeg may be compression change pen shape and you have three pens in board image

Ok there is a problem but I don't think it is in your code. I have already post a solution. Now CV_TM_CCOEFF_NORMED should give you, good results but I have some doubt. I normalise signal before matchTemplate : that's only difference with your code. It shouldn't be necessary with CV_TM_CCOEFF_NORMED but may be a bug ? If you read doc w and h for image T and I shouldn't be equal.equal. Normalisation is done before match template then TM_CCORR_NORMED is the good flag

int main(void) {
    Mat img = imread("board.jpg", IMREAD_GRAYSCALE);
    Mat pen = imread("pen.jpg", IMREAD_GRAYSCALE);
    Mat black(9, 9, CV_8UC1, Scalar(0));
    Mat result;
    Mat penF, imgF;
    img.convertTo(imgF, CV_32F, 1,-mean(img)[0]); // Image with null mean
    pen.convertTo(penF, CV_32F,1, -mean(pen)[0]); // Image with null mean
    matchTemplate(imgF, penF, result, TM_CCOEFF_NORMED);
TM_CCORR_NORMED);
    imshow("result", result);
    Mat plus = result > 0.88;// why 0.88? try and test
    Mat mask;
    dilate(plus, mask, pen);
    img = imread("board.jpg", IMREAD_COLOR);
    img(Rect(pen.cols / 2, pen.rows / 2, plus.cols, plus.rows)).setTo(Vec3b(255, 0, 255), mask);
    imshow("plus", img);
    waitKey(0);
    return 0;
}

Why thereshold is not equal for all pens : as your image is in jpeg may be compression change pen shape and you have three pens in board image

Ok there is a problem but I don't think it is in your code. I have already post a solution. Now CV_TM_CCOEFF_NORMED should give you, good results but I have some doubt. I normalise signal before matchTemplate : that's only difference with your code. It shouldn't be necessary with CV_TM_CCOEFF_NORMED but may be a bug ? If you read doc w and h for image T and I shouldn't be equal. Normalisation is done before match template then TM_CCORR_NORMED is the good flag

int main(void) {
    Mat img = imread("board.jpg", IMREAD_GRAYSCALE);
    Mat pen = imread("pen.jpg", IMREAD_GRAYSCALE);
    Mat black(9, 9, CV_8UC1, Scalar(0));
    Mat result;
    Mat penF, imgF;
    img.convertTo(imgF, CV_32F, 1,-mean(img)[0]); // Image with null mean
    pen.convertTo(penF, CV_32F,1, -mean(pen)[0]); // Image with null mean
    matchTemplate(imgF, penF, result, TM_CCORR_NORMED);
    imshow("result", result);
    Mat plus = result > 0.88;// 0.80;// why 0.88? try and test
    Mat mask;
    dilate(plus, mask, pen);
    img = imread("board.jpg", IMREAD_COLOR);
    img(Rect(pen.cols / 2, pen.rows / 2, plus.cols, plus.rows)).setTo(Vec3b(255, 0, 255), mask);
    imshow("plus", img);
    waitKey(0);
    return 0;
}

Why thereshold is not equal for all pens : as your image is in jpeg may be compression change pen shape and you have three pens in board image

Ok there is a problem but I don't think it is in your code. I have already post a solution. Now CV_TM_CCOEFF_NORMED should give you, good results but I have some doubt. I normalise signal before matchTemplate : that's only difference with your code. It shouldn't be necessary with CV_TM_CCOEFF_NORMED but may be a bug ? If you read doc w and h for image T and I shouldn't be equal. Normalisation is done before match template then TM_CCORR_NORMED is the good flagflag You can work in color to improve results

int main(void) {
    Mat img = imread("board.jpg", IMREAD_GRAYSCALE);
imread("capture.png", IMREAD_COLOR);
    Mat pen = imread("pen.jpg", IMREAD_GRAYSCALE);
    Mat black(9, 9, imread("folder.png", IMREAD_COLOR);
    Mat black(3, 3, CV_8UC1, Scalar(0));
Scalar(255));
    Mat result;
    Mat penF, imgF;
    img.convertTo(imgF, vector<Mat>imgPlane;
    vector<Mat>tplPlane;
    split(pen, tplPlane);
    split(img, imgPlane);
    for (int i = 0; i < max(tplPlane.size(), imgPlane.size()); i++)
    {
        Mat r;
        imgPlane[i].convertTo(imgF, CV_32F, 1,-mean(img)[0]); // Image with null mean
    pen.convertTo(penF, 1,-mean(imgPlane[i])[0]);
        tplPlane[i].convertTo(penF, CV_32F,1, -mean(pen)[0]); // Image with null mean
-mean(tplPlane[i])[0]);
        matchTemplate(imgF, penF, result, r, TM_CCORR_NORMED);
        if (result.empty())
            result = r.clone();
        else
            result = result + r;
    }
    imshow("result", result);
    Mat plus = result > 0.80;// 2.3 & result < 3;// why 0.88? 0.92? try and test
    Mat mask;
    dilate(plus, mask, pen);
black);
    img = imread("board.jpg", imread("capture.png", IMREAD_COLOR);
    img(Rect(pen.cols / 2, pen.rows / 2, plus.cols, plus.rows)).setTo(Vec3b(255, 0, 255), mask);
    imshow("plus", img);
    waitKey(0);
    return 0;
}

Why thereshold is not equal for all pens : as your image is in jpeg may be compression change pen shape and you have three pens in board image

Ok there is a problem but I don't think it is in your code. I have already post a solution. Now CV_TM_CCOEFF_NORMED should give you, good results but I have some doubt. I normalise signal before matchTemplate : that's only difference with your code. It shouldn't be necessary with CV_TM_CCOEFF_NORMED but may be a bug ? If you read doc w and h for image T and I shouldn't be equal. Normalisation is done before match template then TM_CCORR_NORMED is the good flag You can work in color to improve results

int main(void) {
    Mat img = imread("capture.png", IMREAD_COLOR);
    Mat pen = imread("folder.png", IMREAD_COLOR);
    Mat black(3, 3, CV_8UC1, Scalar(255));
    Mat result;
    Mat penF, imgF;
    vector<Mat>imgPlane;
    vector<Mat>tplPlane;
    split(pen, tplPlane);
    split(img, imgPlane);
    for (int i = 0; i < max(tplPlane.size(), min(tplPlane.size(), imgPlane.size()); i++)
    {
        Mat r;
        imgPlane[i].convertTo(imgF, CV_32F, 1,-mean(imgPlane[i])[0]);
        tplPlane[i].convertTo(penF, CV_32F,1, -mean(tplPlane[i])[0]);
-mean(imgPlane[i])[0]);
        matchTemplate(imgF, penF, r, TM_CCORR_NORMED);
        if (result.empty())
            result = r.clone();
(r.clone());
        else
            result = result + r;
 (r);
   }
     imshow("result", result);
    Mat plus = result > 2.3 & result < 3;// why 0.92? try and test
    Mat mask;
    dilate(plus, mask, black);
    img = imread("capture.png", IMREAD_COLOR);
    img(Rect(pen.cols / 2, pen.rows / 2, plus.cols, plus.rows)).setTo(Vec3b(255, 0, 255), mask);
    imshow("plus", img);
    waitKey(0);
    return 0;
}

Why thereshold is not equal for all pens : as your image is in jpeg may be compression change pen shape and you have three pens in board image

Ok there is a problem but I don't think it is in your code. I have already post a solution. Now CV_TM_CCOEFF_NORMED should give you, good results but I have some doubt. I normalise signal before matchTemplate : that's only difference with your code. It shouldn't be necessary with CV_TM_CCOEFF_NORMED but may be a bug ? If you read doc w and h for image T and I shouldn't be equal. Normalisation is done before match template then TM_CCORR_NORMED is the good flag You can work in color to improve results

int main(void) {
    Mat img = imread("capture.png", IMREAD_COLOR);
    Mat pen = imread("folder.png", IMREAD_COLOR);
    Mat black(3, 3, CV_8UC1, Scalar(255));
    Mat result;
    Mat penF, imgF;
    vector<Mat>imgPlane;
    vector<Mat>tplPlane;
    split(pen, tplPlane);
    split(img, imgPlane);
    for (int i = 0; i < min(tplPlane.size(), imgPlane.size()); i++)
    {
        Mat r;
        imgPlane[i].convertTo(imgF, CV_32F, 1,-mean(imgPlane[i])[0]);
        tplPlane[i].convertTo(penF, CV_32F,1, -mean(imgPlane[i])[0]);
        matchTemplate(imgF, penF, r, TM_CCORR_NORMED);
        if (result.empty())
            result = (r.clone());
        else
            result = result + (r);
   }
    imshow("result", result);
   double minf, maxf;
   minMaxLoc(result, &minf, &maxf);
    Mat plus = result > 2.3 & result < 3;// 0.99*maxf ;// why 0.92? try and test
    Mat mask;
    dilate(plus, mask, black);
    img = imread("capture.png", IMREAD_COLOR);
    img(Rect(pen.cols / 2, pen.rows / 2, plus.cols, plus.rows)).setTo(Vec3b(255, 0, 255), mask);
    imshow("plus", img);
    waitKey(0);
    return 0;
}

Why thereshold is not equal for all pens : as your image is in jpeg may be compression change pen shape and you have three pens in board image

Ok there is a problem but I don't think it is in your code. I have already post a solution. Now CV_TM_CCOEFF_NORMED should give you, good results but I have some doubt. I normalise signal before matchTemplate : that's only difference with your code. It shouldn't be necessary with CV_TM_CCOEFF_NORMED but may be a bug ? If you read doc w and h for image T and I shouldn't be equal. Normalisation is done before match template then TM_CCORR_NORMED is the good flag You can work in color to improve results

int main(void) {
    Mat img = imread("capture.png", IMREAD_COLOR);
    Mat pen = imread("folder.png", IMREAD_COLOR);
    Mat black(3, 3, CV_8UC1, Scalar(255));
    Mat result;
    Mat penF, imgF;
    vector<Mat>imgPlane;
    vector<Mat>tplPlane;
    split(pen, tplPlane);
    split(img, imgPlane);
    for (int i = 0; i < min(tplPlane.size(), imgPlane.size()); i++)
    {
        Mat r;
        imgPlane[i].convertTo(imgF, CV_32F, 1,-mean(imgPlane[i])[0]);
        tplPlane[i].convertTo(penF, CV_32F,1, -mean(imgPlane[i])[0]);
        matchTemplate(imgF, penF, r, TM_CCORR_NORMED);
        if (result.empty())
            result = (r.clone());
        else
            result = result + (r);
   }
   imshow("result", result);
   double minf, maxf;
   minMaxLoc(result, &minf, &maxf);
    Mat plus = result > 0.99*maxf ;// why 0.92? try and test
    Mat mask;
    dilate(plus, mask, black);
    img = imread("capture.png", IMREAD_COLOR);
    img(Rect(pen.cols / 2, pen.rows / 2, plus.cols, plus.rows)).setTo(Vec3b(255, 0, 255), mask);
    imshow("plus", img);
    waitKey(0);
    return 0;
}

Why thereshold is not equal for all pens : as your image is in jpeg may be compression change pen shape and you have three pens in board image

Now there is some limitation in this method and I think it can be improved color image is a 2D quaternion image and to solve this problem using matchtemplate method : Sangwine, S. J. and Ell, T. A.,“Hypercomplex Fourier Transforms of Color Images” and example in COLOR IMAGE REGISTRATION AND TEMPLATE MATCHING USING QUATERNION PHASE CORRELATION

Ok there is a problem but I don't think it is in your code. I have already post a solution. Now CV_TM_CCOEFF_NORMED should give you, good results but I have some doubt. I normalise signal before matchTemplate : that's only difference with your code. It shouldn't be necessary with CV_TM_CCOEFF_NORMED but may be a bug ? If you read doc w and h for image T and I shouldn't be equal. Normalisation is done before match template then TM_CCORR_NORMED is the good flag You can work in color to improve results

int main(void) {
    Mat img = imread("capture.png", IMREAD_COLOR);
    Mat pen = imread("folder.png", IMREAD_COLOR);
    Mat black(3, 3, CV_8UC1, Scalar(255));
    Mat result;
    Mat penF, imgF;
    vector<Mat>imgPlane;
    vector<Mat>tplPlane;
    split(pen, tplPlane);
    split(img, imgPlane);
    for (int i = 0; i < min(tplPlane.size(), imgPlane.size()); i++)
    {
        Mat r;
        imgPlane[i].convertTo(imgF, CV_32F, 1,-mean(imgPlane[i])[0]);
        tplPlane[i].convertTo(penF, CV_32F,1, -mean(imgPlane[i])[0]);
        matchTemplate(imgF, penF, r, TM_CCORR_NORMED);
        if (result.empty())
            result = (r.clone());
        else
            result = result + (r);
   }
   imshow("result", result);
   double minf, maxf;
   minMaxLoc(result, &minf, &maxf);
    Mat plus = result > 0.99*maxf ;// why 0.92? try and test
    Mat mask;
    dilate(plus, mask, black);
    img = imread("capture.png", IMREAD_COLOR);
    img(Rect(pen.cols / 2, pen.rows / 2, plus.cols, plus.rows)).setTo(Vec3b(255, 0, 255), mask);
    imshow("plus", img);
    waitKey(0);
    return 0;
}

Why thereshold is not equal for all pens : as your image is in jpeg may be compression change pen shape and you have three pens in board image

try with https://github.com/LaurentBerger/ColorMatchTemplate

Now there is some limitation in this method and I think it can be improved color image is a 2D quaternion image and to solve this problem using matchtemplate method : Sangwine, S. J. and Ell, T. A.,“Hypercomplex Fourier Transforms of Color Images” and example in COLOR IMAGE REGISTRATION AND TEMPLATE MATCHING USING QUATERNION PHASE CORRELATION