opencv + mathGL displays the histogram of the image

surroundings:

visual studio 2019 + opencv3.3.1 + mathGL

Problem Description:

Drawing histograms in C++ is not particularly convenient, although you can also combine the hist function of opencv and write one yourself. The following is the effect of a function written by yourself:

Insert picture description here
In general, it is still more troublesome. C++ has fewer visualization libraries. Let's try MathGl today.

Configure MathGL:

Installation reference

You can also refer to the official documentation and tutorials of MathGL.
MathGl official document
tutorial download

Code demo:

Code:

#include <opencv2/core.hpp>
#include <opencv2/imgproc.hpp>
#include <opencv2/highgui.hpp>

#include <iostream>
#include <algorithm>
#include <vector>

#include "mgl2/mgl.h"
using namespace std;
using namespace cv;
int my_hist(mglGraph* gr, Mat gray)
{
    
    

	int histSize = 256;	// 直方图bin的个数
	float range[] = {
    
     0, 256 };	// 输入数据的维度 灰度值范围为0-255
	const float* histRanges = {
    
     range };
	Mat hist;
	calcHist(&gray, 1, 0, Mat(), hist, 1, &histSize, &histRanges, true, false);
	
	mglData y;
	mglData* py = &y;
	py->Create(256);

	for (int i = 0; i < hist.rows; i++)
	{
    
    
		py->a[i] = hist.at<float>(i);
	}
	
	//y.Norm(mreal(0), mreal(1));
	gr->SetRanges(0, 255, 0, y.Maximal());
	gr->Axis(); // 坐标轴
	gr->Box(); 
	gr->Bars(y);
	return 0;
}
int main(int argc, char** argv) 
{
    
    
	Mat src, dst;
	src = imread("pictures/test.jpg", IMREAD_COLOR);//src 缩写为源文件 打开的文件名要加上格式 比如 jpg
	if (src.empty())
	{
    
    
		printf("could not load image....\n");
		return -1;//返回值为-1 表示异常
	}
	namedWindow("input", CV_WINDOW_AUTOSIZE);//创建一个窗口并命名
	imshow("input", src);//在指定窗口显示指定图片
	
	cvtColor(src, dst, COLOR_BGR2GRAY);

	Mat hist_img = my_drawHist(dst);
	//创建gr对象,指定图像大小为800x500,kind=0说明不使用OpenGL
	mglGraph gr1(0, 800, 500);
	
	my_hist(&gr1, dst);
	//用OpenCV显示图片
	Mat pic(gr1.GetHeight(), gr1.GetWidth(), CV_8UC3);
	pic.data = const_cast<uchar*>(gr1.GetRGB());
	imshow("test", pic);

	//保存图片
	std::cout << "write image as \"test.png\"." << std::endl;
	gr.WritePNG("test.png");  // Don't forget to save the result!

	waitKey(0);
	return 0;
}

result:
Insert picture description here

postscript

mathGL also provides a Hist function:
Insert picture description here
However, if you just draw the histogram of the image, you don’t need this function. It is mentioned in the official document: In
Insert picture description here
other words, this function can only be used when the user defines random data (for example, x is Random number, y is a function of x). At this time, you can use the Hist function, which can automatically help you count.
Official demo:

void smgl_hist(mglGraph *gr)
{
    
    
	mglData x(10000), y(10000), z(10000);	gr->Fill(x,"2*rnd-1");	gr->Fill(y,"2*rnd-1");	gr->Fill(z,"exp(-6*(v^2+w^2))",x,y);
	mglData xx=gr->Hist(x,z), yy=gr->Hist(y,z);	xx.Norm(0,1);	yy.Norm(0,1);
	gr->MultiPlot(3,3,3,2,2,"");	gr->SetRanges(-1,1,-1,1,0,1);	gr->Box();	gr->Dots(x,y,z,"wyrRk");
	gr->MultiPlot(3,3,0,2,1,"");	gr->SetRanges(-1,1,0,1);	gr->Box();	gr->Bars(xx);
	gr->MultiPlot(3,3,5,1,2,"");	gr->SetRanges(0,1,-1,1);	gr->Box();	gr->Barh(yy);
	gr->SubPlot(3,3,2);		gr->Puts(mglPoint(0.5,0.5),"Hist and\nMultiPlot\nsample","a",-3);
}

result:
Insert picture description here

Guess you like

Origin blog.csdn.net/weixin_44456692/article/details/109707795