Ask Your Question
0

LBP calcHist() Wrong

asked 2017-03-14 16:16:28 -0600

ginister gravatar image

updated 2018-01-07 17:07:18 -0600

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. Hist LBP

LBP contains values between 0 and 255 as expected, and hist contains just zeroes.

edit retag flag offensive close merge delete

Comments

Try replacing the Mat() in calcHist with noArray().

Tetragramm gravatar imageTetragramm ( 2017-03-14 19:13:32 -0600 )edit

@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.

ginister gravatar imageginister ( 2017-03-14 19:39:40 -0600 )edit

Well, there's your problem. You access hist by doing

for(int i = 0; i<256; ++i)
    hist.at<float>(i);

The data type is float, and you don't use i and j to access the values.

Tetragramm gravatar imageTetragramm ( 2017-03-14 19:58:58 -0600 )edit

@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.

ginister gravatar imageginister ( 2017-03-14 20:15:16 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
1

answered 2017-03-14 20:56:45 -0600

Tetragramm gravatar image

Oh, duh.

Look at your pixels. You're repeating some of them. Here's a corrected list.

        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-1, j))
            thisCode |= (1 << 4);
        if (thisPixel >= gray.at<uchar>(i + 1, j))
            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;
edit flag offensive delete link more

Comments

Argh! That's annoyingly silly. Sometimes you just need a fresh pair of eyes. Thanks for spotting it!

ginister gravatar imageginister ( 2017-03-15 06:03:11 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2017-03-14 16:16:28 -0600

Seen: 579 times

Last updated: Mar 14 '17