Current location - Training Enrollment Network - Mathematics courses - How to calculate the straight line fitting curve of color image in opencv?
How to calculate the straight line fitting curve of color image in opencv?
CV _ EXPORTS void calcHist(const Mat * images,

In the image,

Const int* channel,

In the array mask,

Output array history,

int dims,

const int* histSize,

Constant floating * * range,

Boolean unity = true,

bool accumulate = false);

The parameter 1 represents the source image sequence that needs to be used to calculate the histogram, so it can allow multiple images with the same size and data type to be used to count their histogram characteristics.

Parameter 2 indicates how many images in the image sequence are used to calculate the histogram.

The appearance of parameter 3 mainly considers that each input image may be multi-channel. For example, RGB images are three channels, so statistically speaking,

An RGB image is actually three single-channel images, and the essence of histogram calculation is also aimed at a single image. Although there are many pictures in the image sequence we input here, there are not.

Each channel of each picture will be used for calculation. Therefore, the function of parameter 3 is to specify which channels of the image are used for calculation (the following explanation assumes that the images in the image sequence are 3 channels, so some images can

There are multiple channels for calculation, and some images may not even be used for one channel. In this case, the channel number is stored in parameter 3, so the channel number of the first picture in the image sequence.

0, 1, 2 (assuming there are 3 channels in the image); The image sequence of the second image in the image is 3, 4, 5,; And so on.

Parameter 4 is a masking operation, which specifies which pixels of each picture are used to calculate the histogram. This mask matrix cannot set a specific mask for a specific image, so it is treated equally here.

Parameter 5 is a matrix for storing the calculated histogram results, which can be a multidimensional matrix.

Parameter 6 is the dimension of the histogram to be calculated.

Parameter 7 is the size of each dimension of the histogram to be calculated, that is, the number of bins in each dimension.

Parameter 8 is the range of each dimension of the histogram to be calculated. If the unity of parameter 9 is true, the size of parameter 8 is 2 at this time, and the inner

Element values represent the upper and lower bounds of each dimension; If the unity of parameter 9 is false, then the size of parameter 8 is the number of bin, that is, the value of parameter 7 and the elements in parameter 8.

Qualitative values need to be specified manually, that is, the coordinate values of each dimension are not necessarily uniform and need to be specified manually.

If parameter 9 is true, it means that each dimension of the histogram to be calculated is evenly assigned according to its range and size; If it is false, it means that the dimensions of the histogram are not evenly distributed. Please refer to the explanation of parameter 8.

If the parameter 10 is false, it means that the histogram output matrix hist has been cleared when using this function; If it is true, it means that the hist has not been cleared when the calcHist () function is used, and the calculation result will be accumulated into the previously saved value.

When using this function, it should be noted that if the default parameter is uniform = true, the range size must be twice that of histSize and the channel size must be equal to dims size.

It can be understood from the above that the value in the channel has specified which single-channel images are used to calculate the target histogram, so once the size of the channel is determined, the dimension of the corresponding histogram is also determined, so we cannot use multiple images to calculate a one-dimensional histogram.

The above content comes from: opencv( 19) of basic study notes: histogram calculation of image sequence.

[cpp] View ordinary copy printing?

# include " open cv2/high GUI/high GUI . HPP "

# include " open cv2/imgproc/imgproc . HPP "

# include & ltiostream & gt

# include & ltstdio.h & gt

Use namespace std

Use namespace cv;

/* * @ Function main */

int main( int argc,char** argv)

{

Mat src,dst

///Load image

src = imread( argv[ 1], 1);

If (! src.data)

{ return- 1; }

///Divided into three single-channel images (R, G and B)

Vector & ltMat & gtrgb _ planes

split( src,RGB _ planes);

///Set the number of bin.

int histSize = 255

///Set the value range (r, g, b))

Floating range [] = {0,255};

const float * hist range = { range };

bool uniform = truebool accumulate = false

Mat r_hist,g_hist,b _ hist

///Calculate histogram:

Calci (& amprgb_planes[0], 1, 0, Mat (), r_hist, 1, & amphistsize &; histRange,uniform,accumulate);

Calci (& amprgb_planes[ 1], 1, 0, Mat (), g_hist, 1, & amphistsize &; histRange,uniform,accumulate);

Calci (& amprgb_planes[2], 1, 0, Mat (), b_hist, 1, & amphistsize &; histRange,uniform,accumulate);

//Create a histogram canvas

int hist _ w = 400int hist _ h = 400

int bin _ w = cv round((double)hist _ w/histSize);

mat his image(hist _ w,hist_h,CV_8UC3,Scalar( 0,0,0));

///Normalize the histogram to the range [0, his image. rows]

normalize(r_hist,r_hist,0,histImage.rows,NORM_MINMAX,- 1,Mat());

normalize(g_hist,g_hist,0,histImage.rows,NORM_MINMAX,- 1,Mat());

normalize(b_hist,b_hist,0,histImage.rows,NORM_MINMAX,- 1,Mat());

///Draw a histogram on the histogram canvas.

for(int I = 1; I & lthistSizei++)

{

line(his image,Point( bin_w*(i- 1),hist _ h-cv round(r _ hist . at & lt; float & gt(i- 1))),

Point( bin_w*(i),hist _ h-cv round(r _ hist . at & lt; Float & gt (1)),

Scalar (0,0,255), 2,8,0);

line(his image,Point( bin_w*(i- 1),hist _ h-cv round(g _ hist . at & lt; float & gt(i- 1))),

Point( bin_w*(i),hist _ h-cv round(g _ hist . at & lt; Float & gt (1)),

Scalar (0,255,0), 2,8,0);

line(his image,Point( bin_w*(i- 1),hist _ h-cv round(b _ hist . at & lt; float & gt(i- 1))),

Point( bin_w*(i),hist _ h-cv round(b _ hist . at & lt; Float & gt (1)),

Scalar (255,0,0), 2,8,0);

}

///Display histogram

namedWindow("calcHist Demo ",CV _ WINDOW _ AUTOSIZE);

Imshow ("Calci's demonstration", HIS image);

wait key(0);

Returns 0;

}