gpt4 book ai didi

c++ - OpenCV绘制带有轴标签和坐标的直方图

转载 作者:行者123 更新时间:2023-12-01 15:03:05 34 4
gpt4 key购买 nike

我阅读了有关如何绘制直方图的OpenCV documentation,并且已经根据需要调整了本教程中的代码,因此输出为条形直方图,如下所示:

enter image description here

我正在寻找一种添加带有标签和索引的轴的方法。是否有任何轻松的解决方案来实现这一目标?

最佳答案

如果您仍然需要解决方案,并且不介意使用gnuplot。

这是一个示例,我读取一个彩色图像,将其变为灰色,计算直方图,然后绘制直方图。

main.cpp

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

#include "gnuplot-iostream.h"

int main(int argc, char* argv[])
{
cv::Mat image = cv::imread(argv[1], CV_LOAD_IMAGE_COLOR);
if (!image.data)
{
LOGMSG_ERR_S_C() << "No image data\n";
return -1;
}
cv::cvtColor(image, image, cv::COLOR_BGR2GRAY);

// construct a grayscale histogram
int binNumber = 256;
int channels[] = {0};
int numImages = 1;
int histDim = 1;
int histSize[] = {binNumber};
float grayRange[] = {0, 256};
const float* ranges[] = {grayRange};
cv::MatND hist;
cv::calcHist(&image, numImages, channels, cv::Mat(), hist, histDim, histSize, ranges);

// plot the historgram
std::vector<std::pair<double, double> > data;
for (int bin = 0; bin < binNumber; bin++)
{
data.emplace_back(bin, hist.at<float>(bin));
}
Gnuplot gp;
gp << "plot" << gp.file1d(data) << "with lines title 'hist'" << std::endl;
cv::imshow("gray", image);

// show the image and wait for a keypress
cv::waitKey(0);
cv::destroyAllWindows();

return 0;
}

您可能需要对 #include "gnuplot-iostream.h"使用boost

CMakeLists.txt
# Handle Boost
find_package(Boost COMPONENTS system iostreams filesystem REQUIRED) # gnuplot needed: system iostreams filesystem

include_directories(
${CMAKE_CURRENT_SOURCE_DIR}
${Boost_INCLUDE_DIR})

target_link_libraries(${targetName}
${Boost_LIBRARIES}
${OpenCV_LIBS})

关于c++ - OpenCV绘制带有轴标签和坐标的直方图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24079822/

34 4 0