OpenCV Color Histogram

Source: Internet
Author: User
Tags ranges

#include"stdafx.h"voidMyshowhist (iplimage* image1,iplimage*image2); Iplimage* Cvshowhist (iplimage*src);intMain () {//equalization of color imagesIplimage* Image= Cvloadimage ("e:\\c_vc_code\\text_photo\\girl004.jpg"); Iplimage* Eqlimage=cvcreateimage (cvgetsize (image), image->depth,3); //Channel Separationiplimage* Redimage=cvcreateimage (cvgetsize (image), image->depth,1); Iplimage* Greenimage=cvcreateimage (cvgetsize (image), image->depth,1); Iplimage* Blueimage=cvcreateimage (cvgetsize (image), image->depth,1); Cvsplit (image,blueimage,greenimage,redimage,null);//Use the Cvsplit function to decompose an image onto a single color channel  /*Cvnamedwindow ("Red", cv_window_autosize);  Cvnamedwindow ("green", cv_window_autosize);  Cvnamedwindow ("Blue", cv_window_autosize);  Cvshowimage ("Red", redimage);  Cvshowimage ("green", greenimage);  Cvshowimage ("Blue", blueimage); */  //cvequalizehist () is suitable for grayscale image histogram equalization, so the image must first be decomposed into a single channel//equalization of each channel separatelycvequalizehist (redimage,redimage);   Cvequalizehist (Greenimage,greenimage);     Cvequalizehist (Blueimage,blueimage); /*Cvnamedwindow ("Red2", cv_window_autosize);  Cvnamedwindow ("Green2", cv_window_autosize);  Cvnamedwindow ("Blue2", cv_window_autosize);  Cvshowimage ("Red2", redimage);  Cvshowimage ("Green2", greenimage);  Cvshowimage ("Blue2", blueimage); */    //Channel MergingCvmerge (blueimage,greenimage,redimage,null,eqlimage); //Show pictures and histogramsCvnamedwindow ("Source",1 ); Cvshowimage ("Source", image); Cvnamedwindow ("equalized",1 ); Cvshowimage ("equalized", Eqlimage); Cvsaveimage ("equalized.jpg", Eqlimage);  Myshowhist (Image,eqlimage); Cvwaitkey (0); Cvdestroywindow ("Source"); Cvdestroywindow ("result"); Cvreleaseimage (&image); Cvreleaseimage (&eqlimage); }voidMyshowhist (iplimage* image1,iplimage*image2) {Iplimage* hist_image1=cvshowhist (Image1); Iplimage* hist_image2=cvshowhist (Image2); Cvnamedwindow ("h-s Histogram1",1 ); Cvshowimage ("h-s Histogram1", Hist_image1); Cvnamedwindow ("h-s Histogram2",1 ); Cvshowimage ("h-s Histogram2", Hist_image2); Cvsaveimage ("histogram1.jpg", Hist_image1); Cvsaveimage ("histogram2.jpg", hist_image2);} Iplimage* Cvshowhist (iplimage*src) {Iplimage* HSV = cvcreateimage (cvgetsize (SRC),8,3 ); 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}; /** H component divided into 16 levels, s component divided into 8 levels*/  intH_bins = -, S_bins =8; intHist_size[] ={h_bins, s_bins}; /** Variation range of H component*/  floatH_ranges[] = {0, the }; /** Variation range of S component*/  floatS_ranges[] = {0,255 }; float* ranges[] ={h_ranges, s_ranges}; /** Input image conversion to HSV color space*/Cvcvtcolor (SRC, HSV, CV_BGR2HSV); Cvcvtpixtoplane (HSV, H_plane, S_plane, V_plane,0 ); /** Create histograms, two-dimensional, evenly divided on each dimension*/Cvhistogram* hist = cvcreatehist (2, Hist_size, Cv_hist_array, ranges,1 ); /** According to h,s two flat data statistic histogram*/cvcalchist (Planes, Hist,0,0 ); /** Gets the maximum value of the histogram statistic for displaying the histogram dynamically*/  floatMax_value; Cvgetminmaxhistvalue (hist,0, &max_value,0,0 ); /** Set histogram display image*/  intHeight = -; intwidth = (h_bins*s_bins*6); Iplimage* hist_img = Cvcreateimage (Cvsize (width,height),8,3 );   Cvzero (HIST_IMG); /** Temporary unit image used for HSV to RGB color conversion*/Iplimage* Hsv_color = Cvcreateimage (Cvsize (1,1),8,3); Iplimage* Rgb_color = Cvcreateimage (Cvsize (1,1),8,3); intBin_w = width/(h_bins *s_bins);  for(inth =0; H < H_bins; h++)  {     for(ints =0; s < s_bins; s++)    {      inti = H*s_bins +s; /** Get statistics in the histogram to calculate the height shown in the image*/      floatBin_val =cvqueryhistvalue_2d (hist, H, s); intintensity = Cvround (bin_val*height/max_value); /** Gets the color represented by the current histogram, converted to RGB for drawing*/cvset2d (Hsv_color,0,0, Cvscalar (h* the. f/h_bins,s*255. F/s_bins,255,0));      Cvcvtcolor (HSV_COLOR,RGB_COLOR,CV_HSV2BGR); Cvscalar Color= CVGET2D (Rgb_color,0,0); Cvrectangle (hist_img, Cvpoint (i*bin_w,height), Cvpoint ((i+1) *bin_w,height-intensity), color,-1,8,0 ); }  }  returnhist_img;}

OpenCV Color Histogram

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.