简体   繁体   中英

OpenCV C++ analog of Python method

What will be full analog of that function in C++?

# img, channels, mask, bins for each channel, ranges for each channel
hist2 = cv2.calcHist([img2], [0, 1, 2], None, [32, 32, 32],
        [50, 256, 50, 256, 50, 256])

Because of C interfaces I can't understand it. Ranges in C++ version sets in some another way..

I will reply to one of your comments first. Here is a short example that shows you how to compute a non-uniform histogram:

using namespace std;
using namespace cv;

Mat img = imread("Lenna.png", IMREAD_COLOR);

int channels[] = { 0, 1 }; 

Mat histogram;
int hist_size[] = { 4, 5 };

float range_0[] = { 0.0f, 60.0f, 120.0f, 180.0f, 255.0f }; 
float range_1[] = { 0.0f, 50.0f, 100.0f, 150.0f, 200.0f, 255.0f};
const float* all_ranges[] = { range_0, range_1 };

calcHist(&img, 1, channels, Mat(), histogram, 2, hist_size, all_ranges, false);

The histogram will have 4 x 5 bins, so I had to define 5 x 6 bin boundaries. The first bin for channel 0 will store the values from interval [0, 60), the second bin will store values from the interval [60, 120), and so on.

If you choose the uniform argument to be true, you only need to specify the lower boundary of the first bin and the upper boundary of the last bin for every channel. The algorithm then computes the bin boundaries by dividing this range with number of bins for every channel.

I think that with all information I've given you so far, you should be able to figure out how to rewrite your Python code snippet into C++ code. :)

If you're looking for the C++ version of the opencv calcHist function, there is a good tutorial at http://docs.opencv.org/2.4/modules/imgproc/doc/histograms.html#calchist

It shows how to declare the ranges parameter, which I assume is your confusion

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM