demhist.c

Przykład użycia funkcji cvLoadImage

00001 #ifdef _CH_
00002 #pragma package <opencv>
00003 #endif
00004 
00005 #ifndef _EiC
00006 #include "cv.h"
00007 #include "highgui.h"
00008 #include <stdio.h>
00009 #endif
00010 
00011 char file_name[] = "baboon.jpg";
00012 
00013 int _brightness = 100;
00014 int _contrast = 100;
00015 
00016 int hist_size = 64;
00017 float range_0[]={0,256};
00018 float* ranges[] = { range_0 };
00019 IplImage *src_image = 0, *dst_image = 0, *hist_image = 0;
00020 CvHistogram *hist;
00021 uchar lut[256];
00022 CvMat* lut_mat;
00023 
00024 /* brightness/contrast callback function */
00025 void update_brightcont( int arg )
00026 {
00027     int brightness = _brightness - 100;
00028     int contrast = _contrast - 100;
00029     int i, bin_w;
00030     float max_value = 0;
00031 
00032     /*
00033      * The algorithm is by Werner D. Streidt
00034      * (http://visca.com/ffactory/archives/5-99/msg00021.html)
00035      */
00036     if( contrast > 0 )
00037     {
00038         double delta = 127.*contrast/100;
00039         double a = 255./(255. - delta*2);
00040         double b = a*(brightness - delta);
00041         for( i = 0; i < 256; i++ )
00042         {
00043             int v = cvRound(a*i + b);
00044             if( v < 0 )
00045                 v = 0;
00046             if( v > 255 )
00047                 v = 255;
00048             lut[i] = (uchar)v;
00049         }
00050     }
00051     else
00052     {
00053         double delta = -128.*contrast/100;
00054         double a = (256.-delta*2)/255.;
00055         double b = a*brightness + delta;
00056         for( i = 0; i < 256; i++ )
00057         {
00058             int v = cvRound(a*i + b);
00059             if( v < 0 )
00060                 v = 0;
00061             if( v > 255 )
00062                 v = 255;
00063             lut[i] = (uchar)v;
00064         }
00065     }
00066 
00067     cvLUT( src_image, dst_image, lut_mat );
00068     cvShowImage( "image", dst_image );
00069 
00070     cvCalcHist( &dst_image, hist, 0, NULL );
00071     cvZero( dst_image );
00072     cvGetMinMaxHistValue( hist, 0, &max_value, 0, 0 );
00073     cvScale( hist->bins, hist->bins, ((double)hist_image->height)/max_value, 0 );
00074     /*cvNormalizeHist( hist, 1000 );*/
00075 
00076     cvSet( hist_image, cvScalarAll(255), 0 );
00077     bin_w = cvRound((double)hist_image->width/hist_size);
00078 
00079     for( i = 0; i < hist_size; i++ )
00080         cvRectangle( hist_image, cvPoint(i*bin_w, hist_image->height),
00081                      cvPoint((i+1)*bin_w, hist_image->height - cvRound(cvGetReal1D(hist->bins,i))),
00082                      cvScalarAll(0), -1, 8, 0 );
00083    
00084     cvShowImage( "histogram", hist_image );
00085 }
00086 
00087 
00088 int main( int argc, char** argv )
00089 {
00090     // Load the source image. HighGUI use.
00091     src_image = cvLoadImage( argc == 2 ? argv[1] : file_name, 0 );
00092 
00093     if( !src_image )
00094     {
00095         printf("Image was not loaded.\n");
00096         return -1;
00097     }
00098 
00099     dst_image = cvCloneImage(src_image);
00100     hist_image = cvCreateImage(cvSize(320,200), 8, 1);
00101     hist = cvCreateHist(1, &hist_size, CV_HIST_ARRAY, ranges, 1);
00102     lut_mat = cvCreateMatHeader( 1, 256, CV_8UC1 );
00103     cvSetData( lut_mat, lut, 0 );
00104 
00105     cvNamedWindow("image", 0);
00106     cvNamedWindow("histogram", 0);
00107 
00108     cvCreateTrackbar("brightness", "image", &_brightness, 200, update_brightcont);
00109     cvCreateTrackbar("contrast", "image", &_contrast, 200, update_brightcont);
00110 
00111     update_brightcont(0);
00112     cvWaitKey(0);
00113     
00114     cvReleaseImage(&src_image);
00115     cvReleaseImage(&dst_image);
00116 
00117     cvReleaseHist(&hist);
00118 
00119     return 0;
00120 }
00121 
00122 #ifdef _EiC
00123 main(1,"demhist.c");
00124 #endif
00125 

Wygenerowano Thu Mar 1 12:15:24 2007 dla OpenCV programem  doxygen 1.4.7