1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
| #include <math.h>
#include <cv.h>
#include <highgui.h>
#include <cxcore.h>
#include <stdio.h>
#include <ctype.h>
#include <cvcam.h>
int main()
{
IplImage *image, *ellipse, *histimg=0, *histog=0;
CvHistogram *hist;
int hdims = 255;
image = cvLoadImage ("1.jpg",1);
histog = cvCreateImage( cvGetSize(image), 8, 1 );
cvNamedWindow("image",1);
histimg = cvCreateImage( cvSize(320,200), 8, 3 );
cvNamedWindow( "histo", 1 );
float hranges_arr[] = {0,255};
float* hranges = hranges_arr;
int vmin = 10, vmax = 256, smin = 30;
float max_value = 0;
int bin_w;
hist = cvCreateHist( 1, &hdims, CV_HIST_ARRAY, &hranges, 1 );
cvCalcHist( &histog, hist, 0, 0 );
cvGetMinMaxHistValue( hist, 0, &max_value, 0, 0 );
cvConvertScale( hist->bins, hist->bins, max_value ? 255. / max_value : 0., 0 );
cvZero( histimg );
bin_w = histimg->width / hdims;
CvScalar colorr = cvScalar(0, 255, 255,0);
for( int i = 0; i < hdims; i++ )
{
int val = cvRound( cvGetReal1D(hist->bins,i)*histimg->height/255 );
cvRectangle( histimg, cvPoint(i*bin_w,histimg->height),
cvPoint((i+1)*bin_w,histimg->height - val),
colorr, -1, 8, 0 );
}
cvShowImage("image",image);
cvShowImage ("histo", histimg);
cvWaitKey(0);
} |
Partager