gpt4 book ai didi

c++ - opencv calcHist 在使用 cvtColor 后返回黑色直方图

转载 作者:太空宇宙 更新时间:2023-11-03 22:21:36 25 4
gpt4 key购买 nike

我在“OpenCV 2 计算机视觉应用程序编程手册”一书第 4 章中运行一个示例。calcHist 返回的只有 2 个直方图是黑色的,并且遵循源代码。运行结果如下图。

ColorHistogram() {

// Prepare arguments for a color histogram
histSize[0]= histSize[1]= histSize[2]= 256;
hranges[0]= 0.0; // BRG range
hranges[1]= 255.0;
ranges[0]= hranges; // all channels have the same range
ranges[1]= hranges;
ranges[2]= hranges;
channels[0]= 0; // the three channels
channels[1]= 1;
channels[2]= 2;
}

...

// Computes the 2D ab histogram.
// BGR source image is converted to Lab
cv::MatND getabHistogram(const cv::Mat &image) {

cv::MatND hist;

// Convert to Lab color space
cv::Mat lab;
cv::cvtColor(image,lab,CV_BGR2Lab);

// Prepare arguments for a 2D color histogram
hranges[0]= -128.0;
hranges[1]= 127.0;
channels[0]= 1; // the two channels used are ab
channels[1]= 2;

// Compute histogram
cv::calcHist(&lab,
1, // histogram of 1 image only
channels, // the channel used
cv::Mat(), // no mask is used
hist, // the resulting histogram
2, // it is a 2D histogram
histSize, // number of bins
ranges // pixel value range
);

return hist;
}

错误在哪里?我想不明白。将图像转换为 Lab 格式时 cvtColor(...) 函数是否出错?

enter image description here

最佳答案

代码有错误。您正在为 channel 分配随机值。在 calchist() 中将范围更改为 hranges。试试这个:

cv::MatND getabHistogram(const cv::Mat &image) {

cv::MatND hist;

// Convert to Lab color space
cv::Mat lab;
cv::cvtColor(image,lab,CV_BGR2Lab);

// Prepare arguments for a 2D color histogram
hranges[0]= -128.0;
hranges[1]= 127.0;
channels[0]= 1; // the two channels used are ab
channels[1]= 2;

// Compute histogram
cv::calcHist(&lab,
1, // histogram of 1 image only
channels, // the channel used
cv::Mat(), // no mask is used
hist, // the resulting histogram
2, // it is a 2D histogram
histSize, // number of bins
hranges // pixel value range
);

return hist;

关于c++ - opencv calcHist 在使用 cvtColor 后返回黑色直方图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48576829/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com