Learning opencv--Histogram & Maximum entropy segmentation

Source: Internet
Author: User
Tags float range ranges scalar

The original always feel that the histogram function in OpenCV is very simple, today temporarily need to find the original OpenCV calchist function so powerful, not only can calculate the common 1D Hist, calchist theoretically support 32-dimensional Hist. (32-dimensional Ah, there are wood.) )

void Calchist (const mat* images, int nimages, const int* channels, Inputarray mask, Outputarray hist, int dims, const int* Histsize, const float** ranges, BOOL uniform=true, BOOL Accumulate=false)

Image: Enter a sequence of images

Nimages: Number of source images

Channels: The channel list for calculating hist

Mask: Not explained

hist: Generated hist

Dims:hist dimension (must be greater than 0, current version is less than cv_max_dims=32)

Histszie: Size of each dimension (i.e. bins)

Ranges: Sequence of bins boundaries for each dimension of the histogram

Uniform: whether to align

Accumlate: If set is true, the histogram is not clear before use, to save histograms of multiple image sets within a hist, or to update hist in a timely manner.



Implementation code:

Two histogram drawing functions are implemented here, and the 1d,2d histogram is plotted separately.


BGR Three channel 1D histogram:


Included in the program to implement a window to display multiple images, you can also use Mat1.put_back (MAT2), but Push_back is the y-axis push mat, Jiangzi display is a slender slender window, so it is better to use the ROI user experience.

Mat Drawhist (Mat hist,int bins,int height,scalar RGB) {double maxval=0;
	Minmaxloc (hist,0,&maxval,0,0);
	int scale=1;
	Mat histimg = Mat::zeros (height, bins, cv_8uc3);
	float *binval = hist.ptr<float> (0);
		for (int i=0; i<bins; i++) {int intensity = Cvround (binval[i]*height/maxval);
	Rectangle (Histimg,point (i*scale,0), point ((i+1) *scale, (intensity)), rgb,cv_filled);
	} flip (histimg,histimg,0);
return histimg;

	} void Darwhistrgb (const mat& src) {Mat histb,histg,histr;
	int bins=256;
	int histsize[] = {bins};
	Float range[] = {0,256};
	Const float* Ranges[] = {range};
        int channelsb[] = {0};
        int channelsg[] = {1};
	int channelsr[] = {2};
	Calchist (&src,1,channelsb,mat (), histb,1,histsize,ranges,true,false);
	Calchist (&src,1,channelsg,mat (), histg,1,histsize,ranges,true,false);

	Calchist (&src,1,channelsr,mat (), histr,1,histsize,ranges,true,false);
	Mat histbimg = drawhist (Histb,bins,200,scalar (255,0,0)); Mat histgimg = Drawhist (hisTg,bins,200,scalar (0,255,0));
	
	Mat histrimg = drawhist (Histr,bins,200,scalar (0,0,255));
	Display multiple images in one window Mat display (200,BINS*3,CV_8UC3);
	Mat Displayroi = display (Rect (0,0,bins,200));
	Resize (histbimg,displayroi,displayroi.size ());
	Displayroi = Display (Rect (bins,0,bins,200));
	Resize (histgimg,displayroi,displayroi.size ());
	Displayroi = Display (Rect (bins*2,0,bins,200));

	Resize (histrimg,displayroi,displayroi.size ());
	Imshow ("Histrgb", display);
Waitkey ();
	} int main () {Mat src = imread ("d:/demo.jpg", 1);
	Darwhistrgb (SRC);
return 1; }



HSV H-s Channel 2D histogram:


The size of the grayscale value represents the height of the histogram. Can be seen as a three-dimensional histogram from the top down (z-axis direction).

int main () {Mat src, HSV;


	src = imread ("d:/demo.jpg", 1);

	Cvtcolor (SRC, HSV, CV_BGR2HSV);
	Quantize the hue to + levels//and the saturation to + levels int hbins = +, 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;

	We compute the histogram from the 0-th and 1-st channels int channels[] = {0, 1}; Calchist (&AMP;HSV, 1, channels, Mat (),//Don't use Mask hist, 2, histsize, ranges, true,//-the histogram is Unifor
	M false);
	Double maxval=0;

	Minmaxloc (hist, 0, &maxval, 0, 0);
	int scale = 10;

	Mat histimg = Mat::zeros (Sbins*scale, hbins*10, CV_8UC3); 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);
		} namedwindow ("Source", 1);

		Imshow ("Source", SRC);
		Namedwindow ("H-s histogram", 1);
		Imshow ("H-s histogram", histimg);
Waitkey (); }


Maximum Entropy Division

Using hist to realize maximum entropy model

Information entropy:

Maximum Entropy segmentation:



#include <iostream> #include <opencv/cv.h> #include <opencv/highgui.h> using namespace std;

using namespace CV;
typedef enum {Back,object} entropy_state;

float Total;
Draw hist;
	Mat Drawhist (Mat hist,int bins,int height,scalar RGB) {double maxval=0;
	Minmaxloc (hist,0,&maxval,0,0);
	int scale=1;
	Mat histimg = Mat::zeros (height, bins, cv_8uc3);
	float *binval = hist.ptr<float> (0);
		for (int i=0; i<bins; i++) {int intensity = Cvround (binval[i]*height/maxval);
	Rectangle (Histimg,point (i*scale,0), point ((i+1) *scale, (intensity)), rgb,cv_filled);
	} flip (histimg,histimg,0);
return histimg;
}//Calculate histogram;
	Mat Hist (const mat& src) {mat Hist;
	int bins=256;
	int histsize[] = {bins};
	Float range[] = {0,256};
	Const float* Ranges[] = {range};
	int channels[] = {0};
	Calchist (&src,1,channels,mat (), hist,1,histsize,ranges,true,false);
	Mat histimg = drawhist (Hist,bins,200,scalar (255,0,0));
	Imshow ("Histrgb", histimg);
Return hist;
}//calculate current entropy; FloatCalentropy (const mat& hist,int threshold) {float total_back=0,total_object=0;
	float entropy_back=0,entropy_object=0;
	float entropy = 0;

	int i=0;
	Const float* hist_p = (float*) hist.ptr<float> (0);
	for (i=0; i<threshold; i++) {total_back + = Hist_p[i];

	} Total_object=total-total_back;
	Background entropy;
		for (i=0; i<threshold; i++) {//if (hist_p[i]==0)//continue;
		float percentage = Hist_p[i]/total_back; Entropy_back + =-percentage * LOGF (percentage);
	The definition formula of energy}//foreground entropy; for (i=threshold; i 





Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.