gpt4 book ai didi

c++ - OpenCV - 使用 calcHist 混淆

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:35:03 36 4
gpt4 key购买 nike

我已经多次阅读 calcHist() 的文档,但我认为我对 OpenCV 的缺乏经验和生疏的编程技能完全使我无法理解它。

我正在计算 HSV 图像(Hue 或 channel [0])的一个 channel 中的像素以用于分割目的,使用 10 个接近颜色的 bin 根据类似的东西(让我们以此为例,我偷了网络之外的范围 - fwiw,省略紫红色似乎是错误的):

红色:0-19 和 330-360红黄 (RY):20-49黄色:50-69YG:70-84绿色:85-170国标:171-191蓝色:192-264英国石油公司:265-289紫色:290-329

等等……

那么我该如何使用 calcHist 做到这一点呢?

我目前为止:

#include <opencv2/opencv.hpp>
#include <vector>
#include <opencv2/highgui/highgui.hpp>
#include <iostream>

using namespace std;
using namespace cv;

int main(int argc, char *argv[])
{
Mat scene, sceneHSV, dest, histo;
int numImages = 1, histChannel[] = {0}, dims = 1, histSize[] = {10};

float redRange[] = {0, 10};
float roRange[] = {10, 25};
float orangeRange[] = {25, 35};
float oyRange[] = {35, 42};
float yellowRange[] = {42, 85};
float ygRange[] = {85, 96};
float greenRange[] = {96, 132};
float gbRange[] = {132, 145};
float blueRange[] = {145, 160};
float bpRange[] = {160, 165};
float purpleRange[] = {165, 180};

const float* ranges[] = {redRange, roRange, orangeRange, oyRange, yellowRange, ygRange, greenRange, gbRange, blueRange, bpRange, purpleRange};

vector<Mat> channels;

scene = imread("Apple.jpg", 1);
if (scene.data == NULL)
{
cout<<"FAIL"<<endl;
cin.get();
}

cvtColor(scene, sceneHSV, CV_BGR2HSV);
dilate(sceneHSV, sceneHSV, Mat(), Point(-1, -1), 1, BORDER_CONSTANT, 1);
pyrMeanShiftFiltering(sceneHSV, dest, 2, 50, 3);
split(sceneHSV, channels);

calcHist(&scene, 1, histChannel, Mat(), histo, dims, histSize, ranges, false, false);

cout<<histo<<endl;

waitKey(0);

return 0;
}

现在呢?在这种情况下,calcHist 的参数是什么样的,输出直方图是什么样的?只是一个充满整数的 1x9 数组?

非常感谢。

最佳答案

我修改了 here 中的代码

您可能还想查看 cvtColor here 的文档

请注意,我没有尝试编译或运行这段代码,所以我不保证它会工作。但尽管如此,它作为引用还是很有用的。

Mat hist;
int nimages = 1; // Only 1 image, that is the Mat scene.
int channels[] = {0} // Index for hue channel
int dims = 1 // Only 1 channel, the hue channel
int histSize[] = {9} // 9 bins, 1 each for Red, RY, Yellow, YG etc.
float hranges[] = { 0, 180 }; // hue varies from 0 to 179, see cvtColor
const float *ranges[] = {hranges};

// Compute the histogram.
calcHist(&scene,
nimages,
channels,
Mat(), // No mask
hist, dims, histSize, ranges, uniform=true)

// Now hist will contain the counts in each bin.
// Lets just print out the values. Note that you can output Mat using std::cout
cout << "Histogram: " << endl << hist << endl;

// To access the individual bins, you can either iterate over them
// or use hist.at<uchar>(i, j); Note that one of the index should be 0
// because hist is 1D histogram. Print out hist.rows and hist.cols to see if hist is a N x 1 or 1 x N matrix.
/*
MatIterator_<uchar> it, end;
int binIndex = 0;
for( it = hist.begin<uchar>(), end = hist.end<uchar>(); it != end; ++it)
{
printf("Count in %d bin: %d\n", binIndex, *it);
++binIndex;
}
*/

关于c++ - OpenCV - 使用 calcHist 混淆,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15211387/

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