LBP calcHist() Wrong
Hi everyone, I've written my own LBP method however it does not work. The outputted histogram of codes is 1 column that is identical for every inputted image. However the LBP map looks correct, so I think there is an error with one of the inputs to calcHist(). Are there any obvious errors? Thanks.
Mat gray, lbpMap, hist;
lbpMap.create(in.rows-2, in.cols-2, CV_8UC1);
cvtColor(in, gray, CV_BGRA2GRAY);
for (int i = 1; i < in.rows-1; i++)
for (int j = 1; j < in.cols-1; j++){
uchar thisPixel = gray.at<uchar>(i,j);
uchar thisCode = 0;
if (thisPixel > gray.at<uchar>(i-1,j-1))
thisCode |= 1 << 7;
if (thisPixel > gray.at<uchar>(i,j-1))
thisCode |= 1 << 6;
if (thisPixel > gray.at<uchar>(i+1,j-1))
thisCode |= 1 << 5;
if (thisPixel > gray.at<uchar>(i,j-1))
thisCode |= 1 << 4;
if (thisPixel > gray.at<uchar>(i+1,j-1))
thisCode |= 1 << 3;
if (thisPixel > gray.at<uchar>(i-1,j+1))
thisCode |= 1 << 2;
if (thisPixel > gray.at<uchar>(i,j+1))
thisCode |= 1 << 1;
if (thisPixel > gray.at<uchar>(i+1,j+1))
thisCode += 1;
//Add this string to histogram
lbpMap.at<uchar>(i-1,j-1) = thisCode;
}
//After map built, get hist
const int channelNo[] = { 0 };
float range[] = { 0, 255 };
const float *ranges[] = { range };
int bins = 256;
calcHist(&lbpMap, 1, channelNo, noArray(), hist, 1, &bins, ranges);
Here is the LBP map and hist after this code is run.
LBP contains values between 0 and 255 as expected, and hist contains just zeroes.
Try replacing the Mat() in calcHist with noArray().
@Tetragramm Thanks for the hint, I've tried your tip but sadly the same outcome. When I iterate over all entries in hist.at<uchar>(i,j) it only shows entries of 0.
Well, there's your problem. You access hist by doing
The data type is float, and you don't use i and j to access the values.
@Tetragramm Oh good eye, that's a definite problem. That being said, all the histograms seem to come out identical to the one above through imshow(), is there nothing else above that might be causing this? The problem you identified is one of accessing, not creating the hist.