gpt4 book ai didi

c++ - 使用 OpenCV 在 C++ 中绘制图像绿色分量的直方图

转载 作者:搜寻专家 更新时间:2023-10-31 01:14:15 27 4
gpt4 key购买 nike

我想使用 OpenCV 在 C++ 中为图像的绿色分量创建直方图。以下代码适用于彩色图像,但一旦我将图像拆分为其 RGB 组件并使用绿色组件调用 calcHist 函数,我就会收到以下错误。

OpenCV 错误:histPrepareImages 中的断言失败 (j < nimages),文件/root/src/OpenCV-2.4.1/modules/imgproc/src/histogram.cpp,第 148 行在抛出“cv::Exception”实例后调用终止 what():/root/src/OpenCV-2.4.1/modules/imgproc/src/histogram.cpp:148: error: (-215) j < nimages 函数 histPrepareImages中止(核心转储)

这是我的代码。我拍了两张图片来创建直方图。任何人都请帮助解决这个问题。

#include <cv.h>
#include <highgui.h>

using namespace cv;

int main( int argc, char** argv )
{
Mat src,src1, hsv, hsv1;
if( argc != 3 || !(src=imread(argv[1], 1)).data || !(src=imread(argv[2], 1)).data)
return -1;

std::vector<cv::Mat> three_channels;
cv::split(src,three_channels);

std::vector<cv::Mat> three_channels1;
cv::split(src1,three_channels1);


//cvtColor(src, hsv, CV_BGR2HSV);
//cvtColor(src1, hsv1, CV_BGR2HSV);

// Quantize the hue to 30 levels
// and the saturation to 32 levels
int hbins = 30, sbins = 32;
int histSize[] = {hbins, sbins};
// hue varies from 0 to 179, see cvtColor
float hranges[] = { 0, 180 };
// saturation varies from 0 (black-gray-white) to
// 255 (pure spectrum color)
float sranges[] = { 0, 256 };
const float* ranges[] = { hranges, sranges };
MatND hist, hist1, difference;
// we compute the histogram from the 0-th and 1-st channels
int channels[] = {0, 1};

calcHist( &three_channels[1], 1, channels, Mat(), // do not use mask
hist, 2, histSize, ranges,
true, // the histogram is uniform
false );
calcHist( &three_channels1[1], 1, channels, Mat(), // do not use mask
hist1, 2, histSize, ranges,
true, // the histogram is uniform
false );
double maxVal=0;
minMaxLoc(hist, 0, &maxVal, 0, 0);
minMaxLoc(hist1, 0, &maxVal, 0, 0);

int scale = 10;
Mat histImg = Mat::zeros(sbins*scale, hbins*10, CV_8UC3);
Mat hist1Img = Mat::zeros(sbins*scale, hbins*10, CV_8UC3);
Mat hist2Img = Mat::zeros(sbins*scale, hbins*10, CV_8UC3);

double hist_diff =0;
hist_diff = compareHist(hist, hist1, CV_COMP_CORREL);

absdiff(hist, hist1, difference);

printf("\nHist Diff: %f\n", hist_diff);

for( int h = 0; h < hbins; h++ )
for( int s = 0; s < sbins; s++ )
{
float binVal = hist.at<float>(h, s);
int intensity = cvRound(binVal*255/maxVal);
rectangle( histImg, Point(h*scale, s*scale),
Point( (h+1)*scale - 1, (s+1)*scale - 1),
Scalar::all(intensity),
CV_FILLED );
}
for( int h = 0; h < hbins; h++ )
for( int s = 0; s < sbins; s++ )
{
float binVal = hist1.at<float>(h, s);
int intensity = cvRound(binVal*255/maxVal);
rectangle( hist1Img, Point(h*scale, s*scale),
Point( (h+1)*scale - 1, (s+1)*scale - 1),
Scalar::all(intensity),
CV_FILLED );
}

for( int h = 0; h < hbins; h++ )
for( int s = 0; s < sbins; s++ )
{
float binVal = difference.at<float>(h, s);
int intensity = cvRound(binVal*255/maxVal);
rectangle( hist2Img, Point(h*scale, s*scale),
Point( (h+1)*scale - 1, (s+1)*scale - 1),
Scalar::all(intensity),
CV_FILLED );
}


namedWindow( "Source", 1 );
imshow( "Source", src );

namedWindow( "H-S Histogram", 1 );
imshow( "H-S Histogram", histImg );

namedWindow( "H-S Histogram1", 1 );
imshow( "H-S Histogram1", hist1Img );

namedWindow( "H-S Histogram2", 1 );
imshow( "H-S Histogram2", hist2Img );
waitKey();
}

最佳答案

您正在尝试根据拆分时只有一个 channel 的图像计算两个 channel (0 和 1)的直方图。

我没有详细查看您的代码,但我想您可以省略拆分并将 src/src1 传递给 calcHist 而不是 three_channels[1]/three_channels1[1],设置 channels = {1}

编辑

在您的代码中,将 channels = {0,1} 更改为 channels{0},您应该不会收到任何错误。您将单 channel 图像传递给 calcHist(),这就是为什么您应该只使用 channel 0(唯一的 channel )。通过将 three_channels[1] 作为输入图像传递,您可以确保您实际上是在分析输入图像的第二个 channel 。

或执行以下操作:

    int channels[] = {1};

calcHist( &src, 1, channels, Mat(), // do not use mask
hist, 2, histSize, ranges,
true, // the histogram is uniform
false );

您不再需要 cv::split(src,three_channels)

这是编译的两个版本,但您实际上想要计算绿色 (1D) 直方图而不是 2D 直方图。所以这是您编辑的代码,(希望)可以满足您的需求:


int main( int argc, char** argv )
{
Mat src,src1;
if( argc != 3 || !(src=imread(argv[1], 1)).data || !(src=imread(argv[2], 1)).data)
return -1;



// Quantize the green to 30 levels
int greenbins = 30;
int histSize[] = {greenbins};
// green varies from 0 to 255 (pure spectrum color)
float greenranges[] = { 0, 256 };
const float* ranges[] = { greenranges };
MatND hist, hist1, difference;
// we compute the histogram from the 2nd channel (green, index is 1)
int channels[] = {1};

calcHist( &src, 1, channels, Mat(), // do not use mask
hist, 1, histSize, ranges,
true, // the histogram is uniform
false );
calcHist( &src1, 1, channels, Mat(), // do not use mask
hist1, 1, histSize, ranges,
true, // the histogram is uniform
false );
double maxVal1=0;
double maxVal2 =0;
minMaxLoc(hist, 0, &maxVal1, 0, 0);
minMaxLoc(hist1, 0, &maxVal2, 0, 0);
double maxVal = max(maxVal1, maxVal2);

int scale = 10;
int width = 50;
Mat histImg = Mat::zeros(greenbins*scale, width, CV_8UC3);
Mat hist1Img = Mat::zeros(greenbins*scale, width, CV_8UC3);
Mat hist2Img = Mat::zeros(greenbins*scale, width, CV_8UC3);

double hist_diff =0;
hist_diff = compareHist(hist, hist1, CV_COMP_CORREL);

absdiff(hist, hist1, difference);

printf("\nHist Diff: %f\n", hist_diff);

for( int h = 0; h<greenbins; ++h)
{
float binVal = hist.at<float>(h);
int intensity = cvRound(binVal*255/maxVal);
rectangle( histImg, Point(0, h*scale),
Point(width, (h+1)*scale),
Scalar::all(intensity),
CV_FILLED );
}
for( int h = 0; h<greenbins; ++h)
{
float binVal = hist1.at<float>(h);
int intensity = cvRound(binVal*255/maxVal);
rectangle( hist1Img, Point(0, h*scale),
Point(width, (h+1)*scale),
Scalar::all(intensity),
CV_FILLED );
}

for(int h = 0; h < greenbins; ++h)
{
float binVal = difference.at<float>(h);
int intensity = cvRound(binVal*255/maxVal);
rectangle( hist2Img, Point(0, h*scale),
Point(width, (h+1)*scale),
Scalar::all(intensity),
CV_FILLED );
}

imshow( "Source", src );
imshow( "Source1", src1 );

imshow( "src1 green Histogram", histImg );

imshow( "src2 green Histogram", hist1Img );

imshow( "diff green Histogram", hist2Img );
waitKey();
}

关于c++ - 使用 OpenCV 在 C++ 中绘制图像绿色分量的直方图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11644976/

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