画出C ++ / OpenCV的频率直方图 [英] Draw a frequency histogram in C++/OpenCV

查看:179
本文介绍了画出C ++ / OpenCV的频率直方图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

频率的数组创建。

int arr[10];
arr[0] = 24;
arr[1] = 28;
arr[2] = 23;
arr[3] = 29;
//and so on

如何绘制下使用OpenCV的++的阵列上的直方图?

How do i draw a histogram using OpenCV in C++ based on the array?

推荐答案

OpenCV的是不是真的适合绘图。然而,对于简单的直方图,可以画出一个矩形为每列(最终以交替的颜色)

OpenCV is not really suited for plotting. However, for simple histograms, you can draw a rectangle for each column (eventually with alternating colors)

这是code:

#include <opencv2\opencv.hpp>
#include <algorithm>
using namespace std;
using namespace cv;


void drawHist(const vector<int>& data, Mat3b& dst, int binSize = 3, int height = 0)
{
    int max_value = *max_element(data.begin(), data.end());
    int rows = 0;
    int cols = 0;
    if (height == 0) {
        rows = max_value + 10;
    } else { 
        rows = max(max_value + 10, height);
    }

    cols = data.size() * binSize;

    dst = Mat3b(rows, cols, Vec3b(0,0,0));

    for (int i = 0; i < data.size(); ++i)
    {
        int h = rows - data[i];
        rectangle(dst, Point(i*binSize, h), Point((i + 1)*binSize-1, rows), (i%2) ? Scalar(0, 100, 255) : Scalar(0, 0, 255), CV_FILLED);
    }

}

int main()
{
    vector<int> hist = { 10, 20, 12, 23, 25, 45, 6 };

    Mat3b image;
    drawHist(hist, image);

    imshow("Histogram", image);
    waitKey();

    return 0;
}

由此产生的直方图是这样的:

The resulting histogram would be like:

在这里输入的形象描述

如果您需要从静态数组转换为矢量

If you need to convert from static array to vector:

#define N 3
int arr[N] = {4, 3, 9};
vector<int> hist(arr, arr + N);


更新

有关此绘图功能的更新版本,看看我的其他帖子

For an updated version of this drawing function, have a look at my other post

这篇关于画出C ++ / OpenCV的频率直方图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆