OpenCV Statistical Application-CvHistogram histogram data ~~~

Source: Internet
Author: User

OpenCV Statistical Application-CvHistogram histogram data

CvHistogram can provide histogram calculation and support multiple levels of histogram design, however, when creating a dataset, you need to use its own response function to compile the dataset. CvHistogram is a structure that is more complex than other databases, because of its ability to use intensive data structures with a lower degree than two, CvMatND can be used to design when its direct direction is greater than two, the sparse moment sequence and CvSparseMat method must be used. The following is an example of using CvHistogram to calculate a histogram.

CvHistogram 1 Program Design
# Include <cv. h>
# Include # Include <stdio. h>

IntHistogramBlock = 256;
FloatHistogramRange1 [2] ={ 0,255 };
Float* HistogramRange [1] = {&HistogramRange1 [0]};

IntMain ()
{
IplImage * Image1;
CvHistogram * Histogram1;
IplImage * HistogramImage1;

Image1 = cvLoadImage ("Riverbank.jpg", 0 );

Histogram1 = cvCreateHist (1,&HistogramBlock, CV_HIST_ARRAY, HistogramRange );
HistogramImage1 = cvCreateImage (cvSize (256,300), 8, 3 );
HistogramImage1-> origin = 1;

CvCalcHist (&Image1, Histogram1 );

Printf ("type is: % d/n", Histogram1-> type );
Printf ("Low Bound is: %. f/n", Histogram1-> thresh [0] [0]);
Printf ("Up Bound is: %. f/n", Histogram1-> thresh [0] [1]);
Printf ("The Block is: % d/n", (CvMatND *) Histogram1-> bins)-> dim [0]. size );

Printf ("/nGray Level Values:/n ");
For(IntI = 0; I <HistogramBlock; I ++)
{
Printf ("%. f/n", (CvMatND *) Histogram1-> bins)-> data. fl [I]);
CvLine (HistogramImage1, cvPoint (I, 0), cvPoint (I ,(Int) (CvQueryHistValue_1D (Histogram1, I)/10), CV_RGB (127,127,127 ));
}

CvNamedWindow ("Histogram", 1 );
CvNamedWindow ("Riverbank", 1 );
CvShowImage ("Riverbank", Image1 );
CvShowImage ("Histogram", HistogramImage1 );
CvWaitKey (0 );
}

Original shard:

The result of the rows:

It is the same as the traditional designer that has been directly designed by using the moment operator. In the end, you still need to write your own statement using the Operator, cvHistogram is a feature of the information structure. It can be set to appear in several regions, which is set to 256 regions, but in reality, it can set the region of the direct region in a fuzzy way, that is, it can be smaller than 256 regional data points, is to cut into n equal points, then it can set Up Bound and Low Bound on its own, and the threshold for the number of rows in the gray environment is 0 ~ 255, while the CvHistogram data structure can be set to the upper limit of 30, the lower limit of 200. In this way, the lower limit is small, and the value is 0 ~ 29,201 ~ 266 the data within this region will not be calculated. The content of the CvHistogram structure is shown below, which is divided into CV_HIST_ARRAY and CV_HIST_SPARSE.

1.

2.

The type of CvHistogram is always the value of CV_HIST_MAGIC_VAL, which is irrelevant to the CV_HIST_ARRAY set by the cv_histehist () program above, this may be a poor design for the CvHistogram architecture in OpenCV, and the definition of the parameter number for the CvHistogram data structure is as follows:

# Define CV_HIST_ARRAY 0
# Define CV_HIST_SPARSE 1
# Define CV_HIST_TREE CV_HIST_SPARSE

Therefore, only the CvMatND and CV_HIST_SPARSE sparse moment of cv_array are supported, and the CvSparseMat of CV_HIST_SPARSE is the information with the lower bound, thresh2 is the upstream and downstream resource of an animation, while the common direct data is stored in bins. the CvMatND structure in CvHistogram is for fast initialization. Both bins and mat use the same memory space, and cvQueryHistValue_1D () is used to extract direct data () this function is used.

Next, we will divide the histogram into 50 partitions and the upper bound is 30 and the lower bound is 200.

CvHistogram structure region and upper and lower boundaries
# Include <cv. h>
# Include # Include <stdio. h>

IntHistogramBlock = 50;
IntHistogramBlockWidth;
FloatHistogramRange1 [2] ={ 30,200 };
Float* HistogramRange [1] = {&HistogramRange1 [0]};

IntMain ()
{
IplImage * Image1;
CvHistogram * Histogram1;
IplImage * HistogramImage1;
CvPoint Point1;
CvPoint Point2;

Image1 = cvLoadImage ("Riverbank.jpg", 0 );

Histogram1 = cvCreateHist (1,&HistogramBlock, CV_HIST_ARRAY, HistogramRange );
HistogramImage1 = cvCreateImage (cvSize (256,300), 8, 3 );
CvSetZero (HistogramImage1 );
HistogramImage1-> origin = 1;
HistogramBlockWidth = 256/HistogramBlock;
Printf ("The Bolck Width is: % d/n", HistogramBlockWidth );
CvCalcHist (&Image1, Histogram1 );

Printf ("Gray Level Values:/n ");
For(IntI = 0; I {
Printf ("%. f/n", (CvMatND *) Histogram1-> bins)-> data. fl [I]);
Point1 = cvPoint (I * HistogramBlockWidth, 0 );
Point2 = cvPoint (I + 1) * HistogramBlockWidth ,(Int) CvQueryHistValue_1D (Histogram1, I)/50 );

CvRectangle (HistogramImage1, Point1, Point2, CV_RGB (127,127,127 ));
}

CvNamedWindow ("Histogram", 1 );
CvNamedWindow ("Riverbank", 1 );
CvShowImage ("Riverbank", Image1 );
CvShowImage ("Histogram", HistogramImage1 );
CvWaitKey (0 );
}

The result of the rows:

Therefore, this statement uses the cvRectangle () method for Table Partitioning. For the CvHistogram data structure, why can we define the numbers of upper and lower boundaries and zones at will? Because CvHistogram uses the Look-up table (LUT) method, that is, the Look-up table method, to open a 256-size empty limit column, using the ratio of the limit, store the data records in the Look-up table and apply the data in the index method. The biggest deficiency of the Look-up table is, there is no way to obtain a very accurate number because it is scaled over. Therefore, except for the complete number that can be calculated as 256, LUT usage will be used later.

For other vertices, the procedure is as follows:

Trigger histogram fetch
/****************************
* Change image histogram example
* Date: 08-11-1
* Author: wml
* Copyright: 1.0
*****************************/
# Include <cv. h>
# Include # Include <cvaux. h>
# Include <cxcore. h>
# Include <iostream. h>
# Include <string. h>
# Include <stdio. h>

# Pragma comment (lib, "cv ")
# Pragma comment (lib, "cvaux ")
# Pragma comment (lib, "cxcore ")
# Pragma comment (lib, "highgui ")

Int Histogram3DBlock [3] ={ 256,256,256 };
Float HistogramRange1 [6] = {0,255, 0,255, 0,255 };
Float * HistogramRange [3] = {& HistogramRange1 [0], & HistogramRange1 [2], & HistogramRange1 [4]};

Void Print3DHistogram (CvHistogram * Histogram, int BlockSize );

Int main ()
{
CvHistogram * Histogram1;
IplImage * Image1 = cvLoadImage ("D: // image test c ++ // histrogm3/1.bmp", 1 );
IplImage * RedImage = cvCreateImage (cvGetSize (Image1), 8, 1 );
IplImage * GreenImage = cvCreateImage (cvGetSize (Image1), 8, 1 );
IplImage * BlueImage = cvCreateImage (cvGetSize (Image1), 8, 1 );
IplImage * ImageArray [3] = {RedImage, GreenImage, BlueImage };

CvSplit (Image1, BlueImage, GreenImage, RedImage, 0 );
Histogram1 = cvCreateHist (3, Histogram3DBlock, CV_HIST_SPARSE, HistogramRange );

CvCalcHist (ImageArray, Histogram1 );

Printf ("3D Historgram Data/n ");
Print3DHistogram (Histogram1, 256 );

CvNamedWindow ("Riverbank", 1 );
CvShowImage ("Riverbank", Image1 );

CvWaitKey (0 );
}

Void Print3DHistogram (CvHistogram * Histogram, int BlockSize)
{
For (int I = 0; I <BlockSize; I ++)
{
For (int j = 0; j <BlockSize; j ++)
{
For (int k = 0; k <BlockSize; k ++)
{
If (cvQueryHistValue_3D (Histogram, I, j, k)> 10)
{
Printf ("% 5.f", cvQueryHistValue_3D (Histogram, I, j, k ));
}
}
}
}
}

The result of the rows:

The preceding figure shows the three statistical regions with different degrees of parallelism, namely R, G, and B, and the score is quantified as 256, in the same way, the LUT method is also used. Because this statistic is very large, the region of the LUT method is 256*256*256, in fact, there are only a few points of attention in the allocation. Therefore, sparse moment sequence is required for high-reliability traditional architectures, otherwise, the memory space will be magnified, but for the multi-degree batch table, OpenCV supports a maximum of one second, the binary classification method can be presented with OpenGL (glut). However, the high degree of attention is usually presented in the inductive mode.

CvCreateHist ()
Initialize the CvHistogram information structure. You can select the dense moment sequence (CvMatND) CV_HIST_ARRAY and sparse moment sequence (CvSparseMat) CV_HIST_SPARSE. The first parameter is the regression parameter, the second parameter is the number of regions where the direct operator is to be cut, and the third parameter is the parameter number or generation that selects the cvCreateHist () function, the fourth parameter refers to the upper and lower bounds of each degree.
CvCreateHist, number of the upper and lower bounds of each degree)

CvCalcHist ()
Calculate the histogram's tired data records. The first index is the index structure of the IplImage dataset column, and the second index is the CvHistogram data structure.
CvCalcHist)

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.