Domanda

I have what should be a simple exercise in OpenCV, but can't seem to get it working. I'm trying to determine the density of edges in a section of an image. This is the process I follow: 1. pull subimage from image 2. use Canny to find edges in subImage 3. threshold to create binary image 4. create histogram for binary image 5. get number of pixels in binary image that are "on" (255) 6. calculate "edge density" as numPixelsOn/totalPixels

I've checked the results of 1,2,and 3 above, and results seem ok. Steps 4 and 5 seem to be giving me trouble.

Here's my code for calculating the histogram:

      int histSize = 256; // bin size
      float range[] = { 0, 256} ;
      const float* histRange = { range };

      bool uniform = true;
      bool accumulate = false;

      Mat hist;

      /// Compute the histograms:
      calcHist( &gray, 1, 0, Mat(), hist, 1, &histSize, &histRange, uniform, accumulate );

This doesn't seem to be working. When I check hist after calling calcHist, it has no data (i.e. data == 0)... or maybe I don't understand what I'm looking at.

Now for accessing the "bins" in the histogram, I've tried a number of things. First I tried this:

  uchar* p;
  p = hist.ptr<uchar>(0);
  double edgePixels = p[255];

I also tried to use:

cvQueryHistValue_1D(hist,255); // #include <opencv2/legacy/compat.hpp>

This wouldn't compile. Gave 2 errors: 'cv::Mat' does not have an overloaded member 'operator ->', and 'bins': is not a member of 'cv::Mat'

I guess I need some help on this.

È stato utile?

Soluzione

There is an error in your 3rd param - channels, that should be an array so you should call it like this

 int histSize = 256; // bin size
 float range[] = { 0, 256} ;
 const float* histRange = { range };

 bool uniform = true;
 bool accumulate = false;

 Mat hist;

 int channels[] = {0};

 /// Compute the histograms:
 calcHist( &gray, 1, channels, Mat(), hist, 1, &histSize, &histRange, uniform, accumulate );

You should also call:

hist.at<float>(0);

to get your value, OpenCV stores them as floats, this is the reason you're getting 0 when using uchar as uchar is smaller than float and the numbers stores as small enough to not fill the first bites.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top