Ask Your Question
2

Is this expected HOG outcome?

asked 2015-09-03 04:53:24 -0600

Michael Martin gravatar image

Hi, HOGDescriptor::compute() somehow does not behave as expected for me. It generates different output for the same window(see code). Is there a explanation for?

Mat imgRaw = imread("...", 1);

//Cut two areas of imgRaw that:
    //sliding window fits once
Mat singleWindow = Mat(imgRaw, Rect(1, 0, 48, 96)).clone();

    //sliding window  fits twice and the secound fit is equal to the singleWindow
Mat doubleWindow = Mat(imgRaw, Rect(0, 0, 49, 96)).clone();

vector<float> singleDescri, doubleDescri;

//standard HOG: block(16,16), bStride(8,8), cell(8,8), 9 bins
HOGDescriptor d(Size(48,96), Size(16,16), Size(8,8), Size(8,8), 9);

//using a winStride of one. No padding.
d.compute(singleWindow, singleDescri, Size(1, 1));
d.compute(doubleWindow, doubleDescri, Size(1, 1));

//SINGLE vector contains 1980 values - ((96/8)-1) * ((48/8-1)) * 4 * 9
//DOUBLE vector contains 3960 values
//secound half of DOUBLE should be equal SINGLE
//appeares that the first 396 vales are different, little but different
//as the HOG vector is calculated col by col this is exactly the first col of the window - ((96/8)-1) * 4 * 9

edit retag flag offensive close merge delete

2 answers

Sort by ยป oldest newest most voted
1

answered 2015-09-05 08:44:49 -0600

Michael Martin gravatar image

That problem seems to be known for quite a while. http://code.opencv.org/issues/4149

As mentioned in the code there is an assertion for accessing rows out of a submatrix.(hog.cpp, 311+). The code might still access values out of the submatrix range in x direction.

325:   dbuf[x] = (float)(lut[imgPtr[xmap[x+1]]] - lut[imgPtr[xmap[x-1]]]);

Anyway the result is different gradient image output in x direction.

Mat img = imread("image.png", 0);
Rect roiRect(0, 0, 48, 96);

Mat ROI = Mat(img, roiRect);
Mat ROI_clone = Mat(img, roiRect).clone();

computeGradient of ROI(isContinious = false; isSubmatrix = true) outputs following gradImg:

image description

ROI_clone(isContinious = true; isSubmatrix = false):

image description

edit flag offensive delete link more
1

answered 2015-09-03 10:33:33 -0600

LorenaGdL gravatar image

updated 2015-09-04 06:35:28 -0600

Very interesting question! I've been playing around and I've found a very surprising situation: the problems are caused by the clone() method. If you remove it, then everything is as expected.

But now the question is why? I have no idea, I'm really puzzled. In fact, I've updated the provided code and now shows the strange behavior - I hope some of the devs/gurus here can figure out what's going on:

void main(){
    Mat img = imread("image.png", 0);
    Mat comparison;
    Rect roiRect(0, 0, 64, 128);

    //Taking ROIs (with and without cloning)
    Mat ROI = Mat(img, roiRect);
    Mat ROI_clone = Mat(img, roiRect).clone();

    //Check both ROIs are equal
    compare(ROI, ROI_clone, comparison, CMP_EQ);
    if (countNonZero(comparison) == ROI.rows*ROI.cols){
        cout << "ROI and ROI_clone are the same matrices" << endl;
    }

    //Standard HOG computation, one single window
    vector<float> descriptor, descriptor_clone;
    HOGDescriptor d(Size(roiRect.width, roiRect.height), Size(16, 16), Size(8, 8), Size(8, 8), 9);

    d.compute(ROI, descriptor);
    d.compute(ROI_clone, descriptor_clone);

    compare((Mat)descriptor, (Mat)descriptor_clone, comparison, CMP_EQ);
    cout << "There are " << descriptor.size() - countNonZero(comparison)
         << " different components between both descriptors out of " 
         << descriptor.size() << " total components" << endl;
}

So as expected, using the ROI as it is or cloning it generate the same matrix content, but HOG descriptor over the matrices are different. So...maybe something related to matrix header when cloning?

EDIT: simpler code added

edit flag offensive delete link more

Comments

1

Found the answer to "why" has to do with submatrices and the resulting gradient image. I will post an extended version. 48 hour answer block ;)

Michael Martin gravatar imageMichael Martin ( 2015-09-04 07:16:32 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2015-09-03 04:53:24 -0600

Seen: 535 times

Last updated: Sep 05 '15