Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

maybe simple template matching already does the trick, we can also check "per battery" in the pack:

image description

float sim(const Mat &q, const Mat &t) {
    Mat res;
    matchTemplate(q,t,res,TM_CCOEFF);
    double m; Point p;
    minMaxLoc(res,0,&m,0,&p);
    // p should be in top-left half of our quadrant
    cout << p << " " << (Rect(0,0,q.cols/2,q.rows/2).contains(p)) << " " << m << endl;
    return m;
}
int main()
{
    Mat input = imread("battery2.jpg", 0);
    Mat templ = imread("battery_t2.jpg", 0);

    // slice image into 4 overlapping regions:
    Rect tl(0,0,input.cols/1.9,input.rows/1.9);
    Rect tr(input.cols/2.3,0,input.cols/1.9,input.rows/1.9);
    Rect bl(0,input.rows/2.3,input.cols/1.9,input.rows/1.9);
    Rect br(input.cols/2.3,input.rows/2.3,input.cols/1.9,input.rows/1.9);
    // check similarity per region:
    float r1 = sim(input(tl),templ);
    float r2 = sim(input(tr),templ);
    float r3 = sim(input(bl),templ);
    float r4 = sim(input(br),templ);
    return 0;
}

for the 2nd (wrong) image:

[33, 360] 0 5.76699e+07
[169, 220] 1 7.66588e+07
[171, 186] 1 1.70977e+08
[25, 324] 0 3.40818e+07

maybe simple template matching already does the trick, trick.

we can also check "per battery" in the pack:

image description

float sim(const Mat &q, const Mat &t) {
    Mat res;
    matchTemplate(q,t,res,TM_CCOEFF);
    double m; Point p;
    minMaxLoc(res,0,&m,0,&p);
    // p should be in top-left half of our quadrant
    cout << p << " " << (Rect(0,0,q.cols/2,q.rows/2).contains(p)) << " " << m << endl;
    return m;
}
int main()
{
    Mat input = imread("battery2.jpg", 0);
    Mat templ = imread("battery_t2.jpg", 0);

    // slice image into 4 overlapping regions:
    Rect tl(0,0,input.cols/1.9,input.rows/1.9);
    Rect tr(input.cols/2.3,0,input.cols/1.9,input.rows/1.9);
    Rect bl(0,input.rows/2.3,input.cols/1.9,input.rows/1.9);
    Rect br(input.cols/2.3,input.rows/2.3,input.cols/1.9,input.rows/1.9);
    // check similarity per region:
    float r1 = sim(input(tl),templ);
    float r2 = sim(input(tr),templ);
    float r3 = sim(input(bl),templ);
    float r4 = sim(input(br),templ);
    return 0;
}

for the 2nd (wrong) image:

[33, 360] 0 5.76699e+07
[169, 220] 1 7.66588e+07
[171, 186] 1 1.70977e+08
[25, 324] 0 3.40818e+07