double pghMatchShapes(CvSeq *shape1, CvSeq *shape2) { int dims[] = {8, 8}; float range[] = {-180, 180, -100, 100}; float *ranges[] = {&range[0], &range[2]}; CvHistogram* hist1 = cvCreateHist(2, dims, CV_HIST_ARRAY, ranges, 1); CvHistogram* hist2 = cvCreateHist(2, dims, CV_HIST_ARRAY, ranges, 1); cvCalcPGH(shape1, hist1); cvCalcPGH(shape2, hist2); cvNormalizeHist(hist1, 100.0f); cvNormalizeHist(hist2, 100.0f); double corr = cvCompareHist(hist1, hist2, CV_COMP_BHATTACHARYYA); cvReleaseHist(&hist1); cvReleaseHist(&hist2); return corr; }
static void cvTsCalcBackProjectPatch( IplImage** images, IplImage* dst, CvSize patch_size, CvHistogram* hist, int method, double factor, int* channels ) { CvHistogram* model = 0; IplImage imgstub[CV_MAX_DIM], *img[CV_MAX_DIM]; IplROI roi; int i, dims; int x, y; CvSize size = cvGetSize(dst); dims = cvGetDims( hist->bins ); cvCopyHist( hist, &model ); cvNormalizeHist( hist, factor ); cvZero( dst ); for( i = 0; i < dims; i++ ) { CvMat stub, *mat; mat = cvGetMat( images[i], &stub, 0, 0 ); img[i] = cvGetImage( mat, &imgstub[i] ); img[i]->roi = &roi; } roi.coi = 0; for( y = 0; y < size.height; y++ ) { for( x = 0; x < size.width; x++ ) { double result; roi.xOffset = x; roi.yOffset = y; roi.width = patch_size.width; roi.height = patch_size.height; cvTsCalcHist( img, model, 0, channels ); cvNormalizeHist( model, factor ); result = cvCompareHist( model, hist, method ); CV_IMAGE_ELEM( dst, float, y, x ) = (float)result; } } cvReleaseHist( &model ); }
//default values h_bins=30,s_bins=32,scale=10 CvHistogram * Histogram::getHShistogramFromRGB(IplImage* src){ IplImage* h_plane = cvCreateImage( cvGetSize(src), 8, 1 ); IplImage* s_plane = cvCreateImage( cvGetSize(src), 8, 1 ); IplImage* v_plane = cvCreateImage( cvGetSize(src), 8, 1 ); IplImage* planes[] = { h_plane, s_plane }; IplImage* hsv = cvCreateImage( cvGetSize(src), 8, 3 ); int hist_size[] = {this->h_bins, this->s_bins}; float h_ranges[] = { 0, 180 }; /* hue varies from 0 (~0°red) to 180 (~360°red again) */ float s_ranges[] = { 0, 255 }; /* saturation varies from 0 (black-gray-white) to 255 (pure spectrum color) */ float* ranges[] = { h_ranges, s_ranges }; CvHistogram* hist; cvCvtColor( src, hsv, CV_BGR2HSV ); cvCvtPixToPlane( hsv, h_plane, s_plane, v_plane, 0 ); hist = cvCreateHist( 2, hist_size, CV_HIST_ARRAY, ranges, 1 ); cvCalcHist( planes, hist, 0, 0 ); cvNormalizeHist(hist,1.0); cvReleaseImage(&hsv); cvReleaseImage(&h_plane); cvReleaseImage(&s_plane); cvReleaseImage(&v_plane); return hist; }
void CV_NormHistTest::run_func(void) { if( hist_type != CV_HIST_ARRAY && test_cpp ) { cv::SparseMat h((CvSparseMat*)hist[0]->bins); cv::normalize(h, h, factor, CV_L1); cvReleaseSparseMat((CvSparseMat**)&hist[0]->bins); hist[0]->bins = (CvSparseMat*)h; } else cvNormalizeHist( hist[0], factor ); }
void CamShiftPlugin::ProcessStatic ( int i, ImagePlus *img, ImagePlus *oimg, int *hsizes, CvTermCriteria criteria, IplImage** &planes, CvHistogram* &hist, IplImage* &backproject, CvRect &orect, CvPoint &ocenter, CvRect &searchwin, CvMat* &rotation, CvMat* &shift, bool oready){ if (hist && hist->mat.dim[0].size!=hsizes[0]) cvReleaseHist(&hist); if( !hist ) hist = cvCreateHist( 3, hsizes, CV_HIST_ARRAY, NULL, 0); if( !backproject ) backproject = cvCreateImage( cvGetSize(img->orig), IPL_DEPTH_8U, 1 ); if( !planes ){ planes = (IplImage**) malloc(3 * sizeof(IplImage*)); for (int p=0; p<3; p++) planes[p] = cvCreateImage( cvGetSize(img->orig), 8, 1 ); } if (!rotation) rotation = cvCreateMat(2,3,CV_32FC1); if (!shift) shift = cvCreateMat(2,1,CV_32FC1); if (!oready){ orect = cvBoundingRect(oimg->contourArray[i],1); cvCvtPixToPlane( oimg->orig, planes[0], planes[1], planes[2], 0 ); for (int p=0; p<3; p++) cvSetImageROI(planes[p],orect); cvCalcHist( planes, hist, 0, NULL ); cvNormalizeHist(hist, 255); for (int p=0; p<3; p++) cvResetImageROI(planes[p]); searchwin = orect; //cvRect(0,0,img->orig->width, img->orig->height); ocenter = cvPoint(orect.x+orect.width/2, orect.y+orect.height/2); } //The following checks shouldn't be needed. RestrictRect(searchwin, cvRect(0,0,backproject->width,backproject->height)); cvCvtPixToPlane( img->orig, planes[0], planes[1], planes[2], 0 ); cvCalcBackProject( planes, backproject, hist ); CvBox2D track_box; CvConnectedComp track_comp; cvCamShift( backproject, searchwin, criteria, &track_comp, &track_box ); searchwin = track_comp.rect; cvmSet(shift,0,0,track_box.center.x - ocenter.x); cvmSet(shift,1,0,track_box.center.y - ocenter.y); // shift->data.fl[0] = track_box.center.x - ocenter.x; // shift->data.fl[1] = track_box.center.y - ocenter.y; cv2DRotationMatrix(track_box.center, track_box.angle, 1.0, rotation); cvTransform(oimg->contourArray[i],img->contourArray[i],rotation,shift); // CvMat *ofm = FeatPointsToMat(oimg->feats[i]); // Cvmat *fm = FeatPointsToMat(img->feats[i]); // cvTransform(ofm,img->contourArray[i],rotation,shift); TransformFeatPoints(oimg->feats[i], img->feats[i], rotation, shift); }
// If not ready , it initializes the cores // else it updates the nearest core to frame and updates the // number of updates for this particular core void KMeans::updateCore(IplImage *frame,IplImage *mask) { int i; if(!this->numOfSamples) return; if(this->index==0) { this->planeR = cvCreateImage(cvGetSize(frame),IPL_DEPTH_8U,1); this->planeG = cvCreateImage(cvGetSize(frame),IPL_DEPTH_8U,1); this->planeB = cvCreateImage(cvGetSize(frame),IPL_DEPTH_8U,1); } cvSplit(frame,this->planeR,this->planeG,this->planeB,0); cvCalcHist(&this->planeR,this->histR,0,mask); cvCalcHist(&this->planeG,this->histG,0,mask); cvCalcHist(&this->planeB,this->histB,0,mask); cvNormalizeHist(this->histR,1.0); cvNormalizeHist(this->histG,1.0); cvNormalizeHist(this->histB,1.0); float *runner=this->candyset[this->index++]; for(i=0;i<this->numOfBins;i++) { *runner++=cvQueryHistValue_1D(this->histR,i); } for(i=0;i<this->numOfBins;i++) { *runner++=cvQueryHistValue_1D(this->histG,i); } for(i=0;i<this->numOfBins;i++) { *runner++=cvQueryHistValue_1D(this->histB,i); } }
/******************************************************************** Utils::CalculateColorHistogram CalculateColorHistogram Exceptions: None *********************************************************************/ void Utils::CalculateColorHistogram( IplImage* pIplImage, CvHistogram* pHistogram, CvRect* pRectangle ) { try { IplImage* pObjectImage; if ( pRectangle != NULL ) { pObjectImage = OpenCvWrapper::Utils::CropImage( pIplImage, pRectangle ); } else { pObjectImage = pIplImage; } IplImage* h_plane = cvCreateImage( cvGetSize( pObjectImage ), 8, 1 ); IplImage* s_plane = cvCreateImage( cvGetSize( pObjectImage ), 8, 1 ); IplImage* v_plane = cvCreateImage( cvGetSize( pObjectImage ), 8, 1 ); IplImage* planes[] = { h_plane, s_plane }; //convert pixel to plane cvCvtPixToPlane( pObjectImage, h_plane, s_plane, v_plane, 0 ); //calculate the histogram cvCalcHist( planes, pHistogram, 0, 0 ); //normalize the histogram cvNormalizeHist( pHistogram, 1.0 ); //release the locally created images cvReleaseImage( &h_plane ); cvReleaseImage( &s_plane ); cvReleaseImage( &v_plane ); if ( pRectangle != NULL ) { cvReleaseImage( &pObjectImage ); } } EXCEPTION_CATCH_AND_ABORT( "Failed to Calculate Color Histogram" ); }
CvHistogram * Histogram::getHShistogramFromHS(IplImage* hChannel,IplImage* sChannel){ IplImage* planes[] = { hChannel, sChannel }; int hist_size[] = {this->h_bins, this->s_bins}; float h_ranges[] = { 0, 180 }; /* hue varies from 0 (~0°red) to 180 (~360°red again) */ float s_ranges[] = { 0, 255 }; /* saturation varies from 0 (black-gray-white) to 255 (pure spectrum color) */ float* ranges[] = { h_ranges, s_ranges }; CvHistogram* hist; hist = cvCreateHist( 2, hist_size, CV_HIST_ARRAY, ranges, 1 ); cvCalcHist( planes, hist, 0, 0 ); cvNormalizeHist(hist,1.0); return hist; }
void camshift_init(const IplImage* img_template, TrackObject* obj) { int size[] = {255}; float h_range[] = {0, 255}; float* range[] = {h_range}; obj->hist = cvCreateHist(1, size, CV_HIST_ARRAY, range, 1); IplImage* hsv= cvCreateImage(cvGetSize(img_template), IPL_DEPTH_8U, 3); //Size changes. No global or static IplImage* hue = cvCreateImage(cvGetSize(img_template), IPL_DEPTH_8U, 1); cvCvtColor(img_template, hsv, CV_BGR2HSV); cvSplit(hsv, hue, 0, 0, 0); cvCalcHist(&hue, obj->hist, 0, NULL); cvNormalizeHist(obj->hist, 1); obj->track_window= cvRect(0, 0, img_template->width, img_template->height); cvReleaseImage(&hue); cvReleaseImage(&hsv); }
void mvDumpHistogram (IplImage* img, const char* file_name, char delimiter) { FILE* fp = fopen (file_name, "w"); if (fp == NULL) return; const int step_size = 5; const int nbins = 255/step_size; int size[] = {nbins}; float range[] = {0, 255}; float* ranges[] = {range}; IplImage* channel1 = cvCreateImage (cvGetSize(img), IPL_DEPTH_8U, 1); IplImage* channel2 = cvCreateImage (cvGetSize(img), IPL_DEPTH_8U, 1); IplImage* channel3 = cvCreateImage (cvGetSize(img), IPL_DEPTH_8U, 1); // Split image onto the color planes. cvSplit(img, channel1, channel2, channel3, NULL); IplImage* planes[] = {channel1, channel2, channel3}; // here we create 3 1-dimensional histograms. Each hist only looks at one of the 3 colors of RGB for (int i = 0; i < 3; i++) { CvHistogram* hist = cvCreateHist (1, size, CV_HIST_ARRAY, ranges, 1); IplImage* curr_plane[] = {planes[i]}; cvCalcHist (curr_plane, hist, 0, NULL); cvNormalizeHist (hist, 10000); fprintf (fp, "\nChannel %d Histogram\nRange%cBin #%cCount\n", i+1,delimiter,delimiter); // print histogram for (int j = 0; j < nbins; j++) { int binval = cvQueryHistValue_1D(hist, j); fprintf (fp, "%d-%d%c%d%c%d\n", j*step_size,(j+1)*step_size,delimiter,j+1,delimiter,binval); } } cvReleaseImage (&channel1); cvReleaseImage (&channel2); cvReleaseImage (&channel3); fclose (fp); printf ("Dumped histogram to %s\n", file_name); }
static void calc_histogram (IplImage * src, CvHistogram * hist1) { // Compute the HSV image, and decompose it into separate planes. cvCvtColor (src, src, CV_BGR2HSV); IplImage *h_plane = cvCreateImage (cvGetSize (src), 8, 1); IplImage *s_plane = cvCreateImage (cvGetSize (src), 8, 1); IplImage *v_plane = cvCreateImage (cvGetSize (src), 8, 1); IplImage *planes[2]; planes[0] = h_plane; planes[1] = s_plane; cvSplit (src, h_plane, s_plane, v_plane, 0); cvCalcHist (planes, hist1, 0, 0); // Compute histogram cvNormalizeHist (hist1, 20 * 255); // Normalize it cvReleaseImage (&h_plane); cvReleaseImage (&s_plane); cvReleaseImage (&v_plane); }
// A Simple Camera Capture Framework int main() { CvCapture* capture = cvCaptureFromCAM( 0 ); if( !capture ) { fprintf( stderr, "ERROR: capture is NULL \n" ); return -1; } #ifdef HALF_SIZE_CAPTURE cvSetCaptureProperty(capture, CV_CAP_PROP_FRAME_WIDTH, 352/2); cvSetCaptureProperty(capture, CV_CAP_PROP_FRAME_HEIGHT, 288/2); #endif // Create a window in which the captured images will be presented cvNamedWindow( "Source Image Window", CV_WINDOW_AUTOSIZE ); cvNamedWindow( "Back Projected Image", CV_WINDOW_AUTOSIZE ); cvNamedWindow( "Brightness and Contrast Window", CV_WINDOW_AUTOSIZE ); cvNamedWindow( "Blob Output Window", CV_WINDOW_AUTOSIZE ); cvNamedWindow( "Histogram Window", 0); cvNamedWindow( "Rainbow Window", CV_WINDOW_AUTOSIZE ); // Capture one frame to get image attributes: source_frame = cvQueryFrame( capture ); if( !source_frame ) { fprintf( stderr, "ERROR: frame is null...\n" ); return -1; } cvCreateTrackbar("histogram\nnormalization", "Back Projected Image", &normalization_sum, 6000, NULL); cvCreateTrackbar("brightness", "Brightness and Contrast Window", &_brightness, 200, NULL); cvCreateTrackbar("contrast", "Brightness and Contrast Window", &_contrast, 200, NULL); cvCreateTrackbar("threshold", "Blob Output Window", &blob_extraction_threshold, 255, NULL); cvCreateTrackbar("min blob size", "Blob Output Window", &min_blob_size, 2000, NULL); cvCreateTrackbar("max blob size", "Blob Output Window", &max_blob_size, source_frame->width*source_frame->height/4, NULL); inputImage = cvCreateImage(cvGetSize(source_frame), IPL_DEPTH_8U, 1); histAdjustedImage = cvCreateImage(cvGetSize(source_frame), IPL_DEPTH_8U, 1); outputImage = cvCreateImage(cvGetSize(source_frame), IPL_DEPTH_8U, 3 ); hist_image = cvCreateImage(cvSize(320,200), 8, 1); rainbowImage = cvCreateImage(cvGetSize(source_frame), IPL_DEPTH_8U, 3 ); // object that will contain blobs of inputImage CBlobResult blobs; CBlob my_enumerated_blob; cvInitFont(&font, CV_FONT_HERSHEY_SIMPLEX|CV_FONT_ITALIC, hScale, vScale, 0, lineWidth); // Some brightness/contrast stuff: bright_cont_image = cvCloneImage(inputImage); lut_mat = cvCreateMatHeader( 1, 256, CV_8UC1 ); cvSetData( lut_mat, lut, 0 ); while( 1 ) { // Get one frame source_frame = cvQueryFrame( capture ); if( !source_frame ) { fprintf( stderr, "ERROR: frame is null...\n" ); getchar(); break; } cvShowImage( "Source Image Window", source_frame ); // Do not release the frame! cvCvtColor(source_frame, inputImage, CV_RGB2GRAY); // Histogram Stuff! my_hist = cvCreateHist(1, hist_size_array, CV_HIST_ARRAY, ranges, 1); cvCalcHist( &inputImage, my_hist, 0, NULL ); cvNormalizeHist(my_hist, normalization_sum); // NOTE: First argument MUST have an ampersand, or a segmentation fault will result cvCalcBackProject(&inputImage, histAdjustedImage, my_hist); // Histogram Picture int bin_w; float max_value = 0; cvGetMinMaxHistValue( my_hist, 0, &max_value, 0, 0 ); cvScale( my_hist->bins, my_hist->bins, ((double)hist_image->height)/max_value, 0 ); cvSet( hist_image, cvScalarAll(255), 0 ); bin_w = cvRound((double)hist_image->width/hist_size); for(int i = 0; i < hist_size; i++ ) cvRectangle( hist_image, cvPoint(i*bin_w, hist_image->height), cvPoint((i+1)*bin_w, hist_image->height - cvRound(cvGetReal1D(my_hist->bins,i))), cvScalarAll(0), -1, 8, 0 ); cvShowImage( "Histogram Window", hist_image ); cvShowImage("Back Projected Image", histAdjustedImage); // Brightness/contrast loop stuff: int brightness = _brightness - 100; int contrast = _contrast - 100; /* * The algorithm is by Werner D. Streidt * (http://visca.com/ffactory/archives/5-99/msg00021.html) */ if( contrast > 0 ) { double delta = 127.*contrast/100; double a = 255./(255. - delta*2); double b = a*(brightness - delta); for(int i = 0; i < 256; i++ ) { int v = cvRound(a*i + b); if( v < 0 ) v = 0; if( v > 255 ) v = 255; lut[i] = (uchar)v; } } else { double delta = -128.*contrast/100; double a = (256.-delta*2)/255.; double b = a*brightness + delta; for(int i = 0; i < 256; i++ ) { int v = cvRound(a*i + b); if( v < 0 ) v = 0; if( v > 255 ) v = 255; lut[i] = (uchar)v; } } cvLUT( inputImage, bright_cont_image, lut_mat ); cvShowImage( "Brightness and Contrast Window", bright_cont_image); // --------------- // Blob Manipulation Code begins here: // Extract the blobs using a threshold of 100 in the image blobs = CBlobResult( bright_cont_image, NULL, blob_extraction_threshold, true ); // discard the blobs with less area than 5000 pixels // ( the criteria to filter can be any class derived from COperadorBlob ) blobs.Filter( blobs, B_INCLUDE, CBlobGetArea(), B_GREATER_OR_EQUAL, min_blob_size); blobs.Filter( blobs, B_EXCLUDE, CBlobGetArea(), B_GREATER, max_blob_size); // build an output image equal to the input but with 3 channels (to draw the coloured blobs) cvMerge( bright_cont_image, bright_cont_image, bright_cont_image, NULL, outputImage ); // plot the selected blobs in a output image for (int i=0; i < blobs.GetNumBlobs(); i++) { blobs.GetNthBlob( CBlobGetArea(), i, my_enumerated_blob ); // Color 5/6 of the color wheel (300 degrees) my_enumerated_blob.FillBlob( outputImage, cv_hsv2rgb((float)i/blobs.GetNumBlobs() * 300, 1, 1)); } // END Blob Manipulation Code // --------------- sprintf(str, "Count: %d", blobs.GetNumBlobs()); cvPutText(outputImage, str, cvPoint(50, 25), &font, cvScalar(255,0,255)); cvShowImage("Blob Output Window", outputImage); /* // Rainbow manipulation: for (int i=0; i < CV_CAP_PROP_FRAME_WIDTH; i++) { for (int j=0; j < CV_CAP_PROP_FRAME_HEIGHT; j++) { // This line is not figure out yet... // pixel_color_set = ((uchar*)(rainbowImage->imageData + rainbowImage->widthStep * j))[i * 3] ((uchar*)(rainbowImage->imageData + rainbowImage->widthStep * j))[i * 3] = 30; ((uchar*)(rainbowImage->imageData + rainbowImage->widthStep * j))[i * 3 + 1] = 30; ((uchar*)(rainbowImage->imageData + rainbowImage->widthStep * j))[i * 3 + 2] = 30; } } cvShowImage("Rainbow Window", rainbowImage); */ //If ESC key pressed, Key=0x10001B under OpenCV 0.9.7(linux version), //remove higher bits using AND operator if( (cvWaitKey(10) & 255) == 27 ) break; } cvReleaseImage(&inputImage); cvReleaseImage(&histAdjustedImage); cvReleaseImage(&hist_image); cvReleaseImage(&bright_cont_image); cvReleaseImage(&outputImage); cvReleaseImage(&rainbowImage); // Release the capture device housekeeping cvReleaseCapture( &capture ); cvDestroyAllWindows(); return 0; }
int main(int argc, char* argv[]) { // Set up images IplImage* img = cvLoadImage("airplane.jpg"); IplImage* back_img = cvCreateImage( cvGetSize( img ), IPL_DEPTH_8U, 1 ); // Compute HSV image and separate into colors IplImage* hsv = cvCreateImage( cvGetSize(img), IPL_DEPTH_8U, 3 ); cvCvtColor( img, hsv, CV_BGR2HSV ); IplImage* h_plane = cvCreateImage( cvGetSize( img ), 8, 1 ); IplImage* s_plane = cvCreateImage( cvGetSize( img ), 8, 1 ); IplImage* v_plane = cvCreateImage( cvGetSize( img ), 8, 1 ); IplImage* planes[] = { h_plane, s_plane }; cvCvtPixToPlane( hsv, h_plane, s_plane, v_plane, 0 ); // Build and fill the histogram int h_bins = 30, s_bins = 32; CvHistogram* hist; { int hist_size[] = { h_bins, s_bins }; float h_ranges[] = { 0, 180 }; float s_ranges[] = { 0, 255 }; float* ranges[] = { h_ranges, s_ranges }; hist = cvCreateHist( 2, hist_size, CV_HIST_ARRAY, ranges, 1 ); } cvCalcHist( planes, hist, 0, 0 ); // Compute histogram cvNormalizeHist( hist, 20*255 ); // Normalize it cvCalcBackProject( planes, back_img, hist );// Calculate back projection cvNormalizeHist( hist, 1.0 ); // Normalize it // Create an image to visualize the histogram int scale = 10; IplImage* hist_img = cvCreateImage( cvSize( h_bins * scale, s_bins * scale ), 8, 3 ); cvZero ( hist_img ); // populate the visualization float max_value = 0; cvGetMinMaxHistValue( hist, 0, &max_value, 0, 0 ); for( int h = 0; h < h_bins; h++ ){ for( int s = 0; s < s_bins; s++ ){ float bin_val = cvQueryHistValue_2D( hist, h, s ); int intensity = cvRound( bin_val * 255 / max_value ); cvRectangle( hist_img, cvPoint( h*scale, s*scale ), cvPoint( (h+1)*scale - 1, (s+1)*scale - 1 ), CV_RGB( intensity, intensity, intensity ), CV_FILLED ); } } // Show original cvNamedWindow( "Source", 1) ; cvShowImage( "Source", img ); // Show back projection cvNamedWindow( "Back Projection", 1) ; cvShowImage( "Back Projection", back_img ); // Show histogram equalized cvNamedWindow( "H-S Histogram", 1) ; cvShowImage( "H-S Histogram", hist_img ); cvWaitKey(0); cvReleaseImage( &img ); cvReleaseImage( &back_img ); cvReleaseImage( &hist_img ); return 0; }
CvScalar GetSharpness(IplImage* in, IplImage* drawHist=0) { const short history_size = 5; static short history_index = 0; static short history[5]; static IplImage* data = 0; static IplImage* out = 0; static IplImage* out_8bit = 0; if( ! out ) { out=cvCreateImage(cvSize(in->roi->width,in->roi->height),IPL_DEPTH_16S,1); out_8bit=cvCreateImage(cvSize(in->roi->width,in->roi->height),IPL_DEPTH_8U,1); } // aperture size of 1 corresponds to the correct matrix cvLaplace(in, out, 1); short maxLap = -32767; short* imgData = (short*)out->imageData; int i=0; double avg=0.0; for(i=0;i<(out->imageSize/2);i++) { if(abs(imgData[i]) > maxLap) maxLap = abs(imgData[i]); avg += abs(imgData[i]); } avg /= out->imageSize; history[history_index++] = maxLap; history_index = (history_index + 1) % history_size; float mean = 0.0; for(i=0;i<history_size;i++) { mean+=history[i]; } mean /= history_size; if(drawHist) { cvConvertScale(out,out_8bit); CvHistogram* hist; int hist_size[] = { 256 }; float ranges_1[] = { 0, 256 }; float* ranges[] = { ranges_1 }; hist = cvCreateHist( 1, hist_size, CV_HIST_ARRAY, ranges, 1 ); cvCalcHist( &out_8bit, hist, 0, 0 ); // Compute histogram cvNormalizeHist( hist, 20*255 ); // Normalize it // populate the visualization float max_value = 0; cvGetMinMaxHistValue( hist, 0, &max_value, 0, 0 ); for( int s = 0; s < 256; s++ ){ float bin_val = cvQueryHistValue_1D( hist, s ); if(bin_val>0.0) { cvRectangle( drawHist, cvPoint( s, 100 ), cvPoint( s, 100- (bin_val/max_value*100)), CV_RGB( 0, 255, 0 ), CV_FILLED ); } } cvReleaseHist(&hist); } CvScalar r; r.val[0] = mean; r.val[1] = avg; return r; }
//命令模式: 程序名 文件名1 文件名2.。。。。。 int main( int argc, char** argv ) { IplImage* frame; if(argc<2) return 0; frame= cvLoadImage(argv[1],1); //获取直方图 IplImage* r_plane = cvCreateImage(cvGetSize(frame),8,1); IplImage* g_plane = cvCreateImage(cvGetSize(frame),8,1); IplImage* b_plane = cvCreateImage(cvGetSize(frame),8,1); IplImage* gray_plane = cvCreateImage(cvGetSize(frame),8,1); cvCvtPixToPlane(frame,b_plane,g_plane,r_plane,0); cvCvtColor(frame,gray_plane,CV_BGR2GRAY); //直方图均衡化 //cvEqualizeHist(gray_plane,gray_plane); int hist_size=256; float range[] = {0,255}; float* ranges[]={range}; CvHistogram* r_hist = cvCreateHist(1,&hist_size,CV_HIST_ARRAY,ranges,1); CvHistogram* g_hist = cvCreateHist(1,&hist_size,CV_HIST_ARRAY,ranges,1); CvHistogram* b_hist = cvCreateHist(1,&hist_size,CV_HIST_ARRAY,ranges,1); CvHistogram* gray_hist = cvCreateHist(1,&hist_size,CV_HIST_ARRAY,ranges,1); //CvRect rect={0,0,1,frame->height}; //cvSetImageROI(b_plane,rect); cvCalcHist( &r_plane, r_hist, 0, 0 ); cvCalcHist( &g_plane, g_hist, 0, 0 ); cvCalcHist( &b_plane, b_hist, 0, 0 ); cvCalcHist( &gray_plane, gray_hist, 0, 0 ); //归一成1.0为最高 cvNormalizeHist(gray_hist,1.0); #if 1 //替代的功能相同的直方图均衡化算法 float F[256]; unsigned char G[256]; for (int i=0;i<hist_size;i++) { float s= cvQueryHistValue_1D(gray_hist,i) *255; if(0==i) F[i]=s; else F[i]=F[i-1]+s; } for (int i=0;i<hist_size;i++) G[i]=(unsigned char)(int)F[i]; CvMat lookup=cvMat(1,256,CV_8U,G); cvLUT(gray_plane,gray_plane,&lookup); cvCalcHist( &gray_plane, gray_hist, 0, 0 ); //归一成1.0为最高 cvNormalizeHist(gray_hist,1.0); #endif //获取直方图的最大值和最大值处在的位置 float max_value = 0; int max_idx=0; cvGetMinMaxHistValue(gray_hist, NULL, &max_value, 0, &max_idx); double k=0; for (int i=0;i<hist_size;i++) { float bins= cvQueryHistValue_1D(gray_hist,i); // if(i>=179 &&i<=200) // k+=bins; // printf("%d=%f\n",i,bins); printf("%d=%f",i,bins); for(int j=0;j<(int)(bins/0.0001);j++) { printf("-"); } printf("\n"); } printf("%f",k); cvReleaseHist(&r_hist); cvReleaseHist(&g_hist); cvReleaseHist(&b_hist); cvReleaseHist(&gray_hist); cvSaveImage( argv[1], frame ); cvReleaseImage(&frame); return 0; }
void Histogram::normalize(double factor) { cvNormalizeHist(m_histogram, factor); }
int CV_CalcBackProjectPatchTest::prepare_test_case( int test_case_idx ) { int code = CV_BaseHistTest::prepare_test_case( test_case_idx ); if( code > 0 ) { CvRNG* rng = ts->get_rng(); int i, j, n, img_len = img_size.width*img_size.height; patch_size.width = cvTsRandInt(rng) % img_size.width + 1; patch_size.height = cvTsRandInt(rng) % img_size.height + 1; patch_size.width = MIN( patch_size.width, 30 ); patch_size.height = MIN( patch_size.height, 30 ); factor = 1.; method = cvTsRandInt(rng) % CV_CompareHistTest::MAX_METHOD; for( i = 0; i < CV_MAX_DIM + 2; i++ ) { if( i < cdims ) { int nch = 1; //cvTsRandInt(rng) % 3 + 1; images[i] = cvCreateImage( img_size, img_type == CV_8U ? IPL_DEPTH_8U : IPL_DEPTH_32F, nch ); channels[i] = cvTsRandInt(rng) % nch; cvRandArr( rng, images[i], CV_RAND_UNI, cvScalarAll(low), cvScalarAll(high) ); } else if( i >= CV_MAX_DIM ) { images[i] = cvCreateImage( cvSize(img_size.width - patch_size.width + 1, img_size.height - patch_size.height + 1), IPL_DEPTH_32F, 1 ); } } cvTsCalcHist( images, hist[0], 0, channels ); cvNormalizeHist( hist[0], factor ); // now modify the images a bit n = cvTsRandInt(rng) % (img_len/10+1); for( i = 0; i < cdims; i++ ) { char* data = images[i]->imageData; for( j = 0; j < n; j++ ) { int idx = cvTsRandInt(rng) % img_len; double val = cvTsRandReal(rng)*(high - low) + low; if( img_type == CV_8U ) ((uchar*)data)[idx] = (uchar)cvRound(val); else ((float*)data)[idx] = (float)val; } } } return code; }