gpt4 book ai didi

c++ - 使用 openCV 绘制 RGB 强度曲线

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

我是 openCV 的初学者。

我想为下面给出的图像绘制 R、G 和 B 的强度分布图。

我想绘制 R、G 和 B 值 w.r.t 到三个不同图形中的像素位置。

到目前为止,我已经学会了如何读取图像和显示。例如使用 imread();

 Mat img = imread("Apple.bmp");

然后使用 imshow("Window", img); 在屏幕上显示它。

现在我想将所有 R 、 G 和 B 值放在 3 个单独的缓冲区中; buf1、buf2、buf3 并绘制这些值。

请提供一些提示或示例代码片段以帮助我理解这一点。

enter image description here

最佳答案

您可以使用 cv::split() 将 R、G 和 B 分成单独的垫子

std::vector<Mat> planes(3);
cv::split(img, planes);
cv::Mat R = planes[2];
cv::Mat G = planes[1];
cv::Mat B = planes[0];

但是,如果您的代码期望 Mat 具有单一颜色 channel ,则只需要像这样将它们分开。不要使用 at<>()正如所谓的拷贝所暗示的那样 - 如果您按顺序扫描图像,它真的很慢(但它有利于随机访问)。

您可以像这样高效地扫描图像

for(int i = 0; i < img.rows; ++i)
{
// get pointers to each row
cv::Vec3b* row = img.ptr<cv::Vec3b>(i);

// now scan the row
for(int j = 0; j < img.cols; ++j)
{
cv::Vec3b pixel = row[j];
uchar r = pixel[2];
uchar g = pixel[1];
uchar b = pixel[0];
process(r, g, b);
}
}

最后,如果你确实想制作直方图,你可以使用这段代码。它相当旧,所以我想它仍然有效。

void show_histogram_image(cv::Mat src, cv::Mat &hist_image)
{ // based on http://docs.opencv.org/2.4.4/modules/imgproc/doc/histograms.html?highlight=histogram#calchist

int sbins = 256;
int histSize[] = {sbins};

float sranges[] = { 0, 256 };
const float* ranges[] = { sranges };
cv::MatND hist;
int channels[] = {0};

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

double maxVal=0;
minMaxLoc(hist, 0, &maxVal, 0, 0);

int xscale = 10;
int yscale = 10;
//hist_image.create(
hist_image = cv::Mat::zeros(256, sbins*xscale, CV_8UC3);

for( int s = 0; s < sbins; s++ )
{
float binVal = hist.at<float>(s, 0);
int intensity = cvRound(binVal*255/maxVal);
rectangle( hist_image, cv::Point(s*xscale, 0),
cv::Point( (s+1)*xscale - 1, intensity),
cv::Scalar::all(255),
CV_FILLED );
}
}

关于c++ - 使用 openCV 绘制 RGB 强度曲线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24503670/

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