void adjustBrightnessContrast(IplImage *&src, int Brightness, int Contrast) { unsigned char LookupTableData[256]; CvMat *LookupTableMatrix; double Delta; double a, b; int y; IplImage *filterB = cvCreateImage(cvGetSize(src), (src)->depth, 1); IplImage *filterG = cvCreateImage(cvGetSize(src), (src)->depth, 1); IplImage *filterR = cvCreateImage(cvGetSize(src), (src)->depth, 1); cvSplit(src, filterB, filterG, filterR, 0); //Brightness/Contrast Formula if(Contrast > 0) { Delta = 127 * Contrast / 100; a=255 / (255 - Delta * 2); b = a * (Brightness - Delta); } else { Delta = -128 * Contrast / 100; a = (256 - Delta*2) / 255; b = a * Brightness + Delta; } for(int x = 0 ; x < 256 ; x++) { y=(int)(a * x + b); if(y < 0) y = 0; else if(y > 255) y = 255; LookupTableData[x]=(uchar)y; } LookupTableMatrix = cvCreateMatHeader(1, 256, CV_8UC1); cvSetData(LookupTableMatrix, LookupTableData, 0); cvLUT(filterB, filterB, LookupTableMatrix); cvLUT(filterG, filterG, LookupTableMatrix); cvLUT(filterR, filterR, LookupTableMatrix); IplImage *dst = cvCreateImage(cvGetSize(src), src->depth, src->nChannels); cvMerge(filterB, filterG, filterR, 0, dst); cvReleaseImage(&src); src = cvCloneImage(dst); cvReleaseImage(&dst); cvReleaseImage(&filterB); cvReleaseImage(&filterG); cvReleaseImage(&filterR); cvReleaseMat(&LookupTableMatrix); }//end Brightness/Contrast
void ASEF_Algorithm::detecteyes(Mat face_image) { if (isInitialized == false) this->initialize(); double xscale = ((double) scaled_face_image_8uc1->cols) / ((double) face_image.cols); double yscale = ((double) scaled_face_image_8uc1->rows) / ((double) face_image.rows); CvMat temp = face_image; cvResize(&temp, scaled_face_image_8uc1, CV_INTER_LINEAR); cvLUT(scaled_face_image_8uc1, scaled_face_image_32fc1, lut); cvDFT(scaled_face_image_32fc1, scaled_face_image_32fc1, CV_DXT_FORWARD, 0); cvMulSpectrums(scaled_face_image_32fc1, lfilter_dft, lcorr, CV_DXT_MUL_CONJ); cvMulSpectrums(scaled_face_image_32fc1, rfilter_dft, rcorr, CV_DXT_MUL_CONJ); cvDFT(lcorr, lcorr, CV_DXT_INV_SCALE, 0); cvDFT(rcorr, rcorr, CV_DXT_INV_SCALE, 0); minMaxLoc(Mat(lroi), &MinValuel, NULL, NULL, &left_eye); minMaxLoc(Mat(rroi), &MinValuer, NULL, NULL, &right_eye); left_eye.x = (lrect.x + left_eye.x) / xscale; left_eye.y = (lrect.y + left_eye.y) / yscale; right_eye.x = (rrect.x + right_eye.x) / xscale; right_eye.y = (rrect.y + right_eye.y) / yscale; }
void ofxCvBrightnessContrast::setBrightnessAndContrast(ofxCvImage& img, float brightnessAmount, float contrastAmount){ brightnessVal = MAX(-127, MIN(127, brightnessAmount)); contrastVal = MAX(-127, MIN(127, contrastAmount)); unsigned char data[ 256 ]; CvMat * matrix; double delta, a, b; matrix = cvCreateMatHeader( 1, 256, CV_8UC1 ); cvSetData( matrix, data, 0 ); if ( contrastVal>0 ) { delta = (127.0f*contrastVal) / 128.0f; a = 255.0f / ( 255.0f-(delta*2.0f) ); b = a * (brightnessVal-delta); } else { delta = (-128.0f*contrastVal) / 128.0f; a = ( 256.0f-(delta*2.0f) ) / 255.0f; b = ( a*brightnessVal )+delta; } for( int i=0; i<256; i++ ) { int value = cvRound( (a*i)+b ); data[i] = (unsigned char) min( max(0,value), 255 ); } cvLUT( img.getCvImage(), img.getCvImage(), matrix ); cvReleaseMat( &matrix ); }
//-------------------------------------------------------------- ofImage ofxContrast::setBrightness(ofImage& _img, float brightnessAmount){ ofxCvColorImage cvimg; cvimg.allocate(_img.width, _img.height); cvimg.setFromPixels(_img.getPixels(), _img.width, _img.height); float brightnessVal = MAX(-127, MIN(127, brightnessAmount)); unsigned char data[ 256 ]; CvMat * matrix; matrix = cvCreateMatHeader( 1, 256, CV_8UC1 ); cvSetData( matrix, data, 0 ); for( int i=0; i<256; i++ ) { int value = cvRound( i+brightnessVal ); data[i] = (unsigned char) min( max(0,value), 255 ); } cvLUT( cvimg.getCvImage(), cvimg.getCvImage(), matrix ); cvReleaseMat( &matrix ); ofImage ofimg; ofimg.allocate(_img.width, _img.height, OF_IMAGE_COLOR); ofimg.setFromPixels(cvimg.getPixels(), _img.width, _img.height, OF_IMAGE_COLOR); return ofimg; }
void asef_locate_eyes(AsefEyeLocator *asef){ asef->face_image.cols = asef->face_rect.width; asef->face_image.rows = asef->face_rect.height; asef->face_image.type = CV_8UC1; asef->face_image.step = asef->face_rect.width; cvGetSubRect(asef->input_image, &asef->face_image, asef->face_rect); double xscale = ((double)asef->scaled_face_image_8uc1->cols)/((double)asef->face_image.cols); double yscale = ((double)asef->scaled_face_image_8uc1->rows)/((double)asef->face_image.rows); cvResize(&asef->face_image, asef->scaled_face_image_8uc1, CV_INTER_LINEAR); cvLUT(asef->scaled_face_image_8uc1, asef->scaled_face_image_32fc1, asef->lut); cvDFT(asef->scaled_face_image_32fc1, asef->scaled_face_image_32fc1, CV_DXT_FORWARD, 0); cvMulSpectrums(asef->scaled_face_image_32fc1, asef->lfilter_dft, asef->lcorr, CV_DXT_MUL_CONJ); cvMulSpectrums(asef->scaled_face_image_32fc1, asef->rfilter_dft, asef->rcorr, CV_DXT_MUL_CONJ); cvDFT(asef->lcorr, asef->lcorr, CV_DXT_INV_SCALE, 0); cvDFT(asef->rcorr, asef->rcorr, CV_DXT_INV_SCALE, 0); cvMinMaxLoc(asef->lroi, NULL, NULL, NULL, &asef->left_eye, NULL); cvMinMaxLoc(asef->rroi, NULL, NULL, NULL, &asef->right_eye, NULL); asef->left_eye.x = (asef->lrect.x + asef->left_eye.x)/xscale + asef->face_rect.x; asef->left_eye.y = (asef->lrect.y + asef->left_eye.y)/yscale + asef->face_rect.y; asef->right_eye.x = (asef->rrect.x + asef->right_eye.x)/xscale + asef->face_rect.x; asef->right_eye.y = (asef->rrect.y + asef->right_eye.y)/yscale + asef->face_rect.y; }
void asef_locate_eyes(AsefEyeLocator *asef, IplImage *image, CvRect face_rect, CvPoint *leye, CvPoint *reye){ asef->face_img.cols = face_rect.width; asef->face_img.rows = face_rect.height; asef->face_img.type = CV_8UC1; asef->face_img.step = face_rect.width; cvGetSubRect(image, &asef->face_img, face_rect); double xscale = ((double)asef->image_tile->cols)/((double)asef->face_img.cols); double yscale = ((double)asef->image_tile->rows)/((double)asef->face_img.rows); cvResize(&asef->face_img, asef->image_tile, CV_INTER_LINEAR); cvLUT(asef->image_tile, asef->image, asef->lut); cvDFT(asef->image, asef->image, CV_DXT_FORWARD, 0); cvMulSpectrums(asef->image, asef->lfilter_dft, asef->lcorr, CV_DXT_MUL_CONJ); cvMulSpectrums(asef->image, asef->rfilter_dft, asef->rcorr, CV_DXT_MUL_CONJ); cvDFT(asef->lcorr, asef->lcorr, CV_DXT_INV_SCALE, 0); cvDFT(asef->rcorr, asef->rcorr, CV_DXT_INV_SCALE, 0); cvMinMaxLoc(asef->lroi, NULL, NULL, NULL, leye, NULL); cvMinMaxLoc(asef->rroi, NULL, NULL, NULL, reye, NULL); leye->x = (asef->lrect.x + leye->x)/xscale + face_rect.x; leye->y = (asef->lrect.y + leye->y)/yscale + face_rect.y; reye->x = (asef->rrect.x + reye->x)/xscale + face_rect.x; reye->y = (asef->rrect.y + reye->y)/yscale + face_rect.y; }
void onTrackbar(int) { int Brightness = BrightnessPosition - 100; int Contrast = ContrastPosition - 100; double Delta; double a,b; int y; IplImage *filterB = cvCreateImage(cvGetSize(img), img->depth, 1); IplImage *filterG = cvCreateImage(cvGetSize(img), img->depth, 1); IplImage *filterR = cvCreateImage(cvGetSize(img), img->depth, 1); cvSplit(img, filterB, filterG, filterR, 0); //Brightness/Contrast Formula if(Contrast > 0) { Delta = 127 * Contrast / 100; a=255 / (255 - Delta * 2); b = a * (Brightness - Delta); } else { Delta = -128 * Contrast / 100; a = (256 - Delta*2) / 255; b = a * Brightness + Delta; } for(int x = 0 ; x < 256 ; x++) { y=(int)(a * x + b); if(y < 0) y = 0; else if(y > 255) y = 255; LookupTableData[x]=(uchar)y; } cvLUT(filterB, filterB,LookupTableMatrix); cvLUT(filterG, filterG,LookupTableMatrix); cvLUT(filterR, filterR,LookupTableMatrix); cvMerge(filterB, filterG, filterR, 0, dst); cvShowImage(windowName, dst); cvReleaseImage(&filterB); cvReleaseImage(&filterG); cvReleaseImage(&filterR); }
//-------------------------------------------------------------------------------- void ofxCvGrayscaleImage::brightnessContrast(float brightness, float contrast){ int i; /* * The algorithm is by Werner D. Streidt * (http://visca.com/ffactory/archives/5-99/msg00021.html) * (note: uses values between -100 and 100) */ if( contrast > 0 ) { double delta = 127.*contrast; double a = 255./(255. - delta*2); double b = a*(brightness*100 - delta); for( i = 0; i < 256; i++ ) { int v = cvRound(a*i + b); if( v < 0 ) v = 0; if( v > 255 ) v = 255; briConLut[i] = (uchar)v; } } else { double delta = -128.*contrast; double a = (256.-delta*2)/255.; double b = a*brightness*100. + delta; for( i = 0; i < 256; i++ ) { int v = cvRound(a*i + b); if( v < 0 ) v = 0; if( v > 255 ) v = 255; briConLut[i] = (uchar)v; } } cvSetData( briConLutMatrix, briConLut, 0 ); cvLUT( cvImage, cvImageTemp, briConLutMatrix); swapTemp(); flagImageChanged(); }
//-------------------------------------------------------------------------------- void ofxCvGrayscaleImage::brightnessContrast(float brightness, float contrast){ if( !bAllocated ){ ofLog(OF_LOG_ERROR, "in brightnessContrast, image is not allocated"); return; } int i; /* * The algorithm is by Werner D. Streidt * (http://visca.com/ffactory/archives/5-99/msg00021.html) * (note: uses values between -1 and 1) */ double delta; double a; double b; if( contrast > 0 ) { delta = 127.*contrast; a = 255./(255. - delta*2); b = a*(brightness*100 - delta); }else{ delta = -128.*contrast; a = (256.-delta*2)/255.; b = a*brightness*100. + delta; } for( i = 0; i < 256; i++ ) { int v = cvRound(a*i + b); if( v < 0 ) v = 0; if( v > 255 ) v = 255; briConLutMatrix->data.ptr[i] = (uchar)v; //briConLut[i] = (uchar)v; } //cvSetData( briConLutMatrix, briConLut, 0 ); cvLUT( cvImage, cvImageTemp, briConLutMatrix); swapTemp(); flagImageChanged(); }
IplImage* onHistEZ(IplImage* gray){ IplImage *src; if(gray->nChannels!=1){ printf("不是单通道图像\n"); return NULL; } uchar* ImgData=(uchar*)(gray->imageData); int rows=gray->widthStep; int cols=gray->height; int gmax=0,gmin=255;//用来保存灰度范围 int ihist[256];//图像直方图 int nn[256];//直方图累加分布数组 uchar T[256];//保存均衡化后的直方图 CvMat *T_mat; int i=0,val=0; int sum; memset(ihist,0,sizeof(ihist)); sum=rows*cols; for(i=0;i<sum;i++){ ihist[*ImgData]++;//灰度统计 if((int)(*ImgData)>gmax) gmax=(int)(*ImgData); if((int)(*ImgData)<gmin) gmin=(int)(*ImgData); ImgData++; } //建立灰度累加分布直方图 for(i=0;i<256;i++){ val=val+ihist[i]; nn[i]=val; } //归一化直方图 T_mat=cvCreateMatHeader(1,256,CV_8UC1); for(i=0;i<256;i++){ T[i]=(uchar)(((nn[i]<<8)-nn[i])/sum);//灰度范围【0,255】 } T_mat=cvCreateMatHeader(1,256,CV_8UC1); cvSetData(T_mat,T,0);//接上数据块 cvLUT(gray,gray,T_mat);//反向映射 return gray; }
void HistgramEqualization(IplImage* src,IplImage* dst) { CvHistogram *hist = 0; const HDIM=256; int n = HDIM; double nn[HDIM]; uchar T[HDIM]; CvMat *T_mat; int x; int sum = 0; // sum of pixels of the source image 图像中象素点的总和 double val = 0; // calculate histgram 计算直方图 hist = cvCreateHist( 1, &n, CV_HIST_ARRAY, 0, 1 ); cvCalcHist( &src, hist, 0, 0 ); // Create Accumulative Distribute Function of histgram val = 0; for ( x = 0; x < n; x++) { val = val + cvGetReal1D (hist->bins, x); nn[x] = val; } // Compute intensity transformation 计算变换函数的离散形式 sum = src->height * src->width; for( x = 0; x < n; x++ ) { T[x] = (uchar) (255 * nn[x] / sum); // range is [0,255] } // Do intensity transform for source image cvCopyImage(src, dst); T_mat = cvCreateMatHeader( 1, 256, CV_8UC1 ); cvSetData( T_mat, T, 0 ); // directly use look-up-table function 直接调用内部函数完成 look-up-table 的过程 cvLUT( src, dst, T_mat ); cvReleaseHist ( &hist ); }
//-------------------------------------------------------------- void ofxCvGrayscaleAdvanced::applyMinMaxGamma(float gamma){ // for this, we will search for the minimum and maximum values, and apply a gamma, ie: // (a) search for min and max // (b) pct = val - min / max - min // (c) newVal = pow(pct, gamma) * 255 // // k ?? double minval; double maxval; if (cvImage != NULL){ cvMinMaxLoc(cvImage,&minval,&maxval); } //printf("%i %i ", (int)minval, (int)maxval); float spread = (float)(maxval - minval); for (int i=0; i<256; i++){ float pct = (float) (i - minval) / spread; if (pct < 0) pct = 0; if (pct > 1) pct = 1; gammaLut[i] = (int)(255.0 * powf(pct, gamma)); } cvSetData( gammaLutMatrix, gammaLut, 0 ); //CvMat lutTemp = cvMat(1,256,CV_8UC1,lut); cvLUT( cvImage, cvImageTemp, gammaLutMatrix); swapTemp(); flagImageChanged(); }
//-------------------------------------------------------------- ofImage ofxContrast::setBrightnessAndContrast(ofImage& _img, float brightnessAmount, float contrastAmount){ ofxCvColorImage cvimg; cvimg.allocate(_img.width, _img.height); cvimg.setFromPixels(_img.getPixels(), _img.width, _img.height); float brightnessVal = MAX(-127, MIN(127, brightnessAmount)); float contrastVal = MAX(-127, MIN(127, contrastAmount)); unsigned char data[ 256 ]; CvMat * matrix; double delta, a, b; matrix = cvCreateMatHeader( 1, 256, CV_8UC1 ); cvSetData( matrix, data, 0 ); if ( contrastVal>0 ) { delta = (127.0f*contrastVal) / 128.0f; a = 255.0f / ( 255.0f-(delta*2.0f) ); b = a * (brightnessVal-delta); } else { delta = (-128.0f*contrastVal) / 128.0f; a = ( 256.0f-(delta*2.0f) ) / 255.0f; b = ( a*brightnessVal )+delta; } for( int i=0; i<256; i++ ) { int value = cvRound( (a*i)+b ); data[i] = (unsigned char) min( max(0,value), 255 ); } cvLUT( cvimg.getCvImage(), cvimg.getCvImage(), matrix ); cvReleaseMat( &matrix ); ofImage ofimg; ofimg.allocate(_img.width, _img.height, OF_IMAGE_COLOR); ofimg.setFromPixels(cvimg.getPixels(), _img.width, _img.height, OF_IMAGE_COLOR); return ofimg; }
//--------------------------------------------------------------- //【関数名 】:cv_ColorExtraction //【処理概要】:色抽出 //【引数 】:src_img = 入力画像(8bit3ch) // :dst_img = 出力画像(8bit3ch) // :code = 色空間の指定(CV_BGR2HSV,CV_BGR2Labなど) // :ch1_lower = ch1のしきい値(小) // :ch1_upper = ch1のしきい値(大) // :ch2_lower = ch2のしきい値(小) // :ch2_upper = ch2のしきい値(大) // :ch3_lower = ch3のしきい値(小) // :ch3_upper = ch3のしきい値(大) //【戻り値 】:なし //【備考 】:lower <= upperの場合、lower以上upper以下の範囲を抽出、 // :lower > upperの場合、upper以下lower以上の範囲を抽出します。 //--------------------------------------------------------------- void cv_ColorExtraction(IplImage* src_img, IplImage* dst_img, int code, int ch1_lower, int ch1_upper, int ch2_lower, int ch2_upper, int ch3_lower, int ch3_upper ) { int i, k; IplImage *Color_img; IplImage *ch1_img, *ch2_img, *ch3_img; IplImage *Mask_img; int lower[3]; int upper[3]; int val[3]; CvMat *lut; //codeに基づいたカラー変換 Color_img = cvCreateImage(cvGetSize(src_img), src_img->depth, src_img->nChannels); cvCvtColor(src_img, Color_img, code); //3ChのLUT作成 lut = cvCreateMat(256, 1, CV_8UC3); lower[0] = ch1_lower; lower[1] = ch2_lower; lower[2] = ch3_lower; upper[0] = ch1_upper; upper[1] = ch2_upper; upper[2] = ch3_upper; for (i = 0; i < 256; i++) { for (k = 0; k < 3; k++) { if (lower[k] <= upper[k]) { if ((lower[k] <= i) && (i <= upper[k])) { val[k] = 255; } else { val[k] = 0; } } else { if ((i <= upper[k]) || (lower[k] <= i)) { val[k] = 255; } else { val[k] = 0; } } } //LUTの設定 cvSet1D(lut, i, cvScalar(val[0], val[1], val[2])); } //3ChごとのLUT変換(各チャンネルごとに2値化処理) cvLUT(Color_img, Color_img, lut); cvReleaseMat(&lut); //各チャンネルごとのIplImageを確保する ch1_img = cvCreateImage(cvGetSize(Color_img), Color_img->depth, 1); ch2_img = cvCreateImage(cvGetSize(Color_img), Color_img->depth, 1); ch3_img = cvCreateImage(cvGetSize(Color_img), Color_img->depth, 1); //チャンネルごとに二値化された画像をそれぞれのチャンネルに分解する cvSplit(Color_img, ch1_img, ch2_img, ch3_img, NULL); //3Ch全てのANDを取り、マスク画像を作成する。 Mask_img = cvCreateImage(cvGetSize(Color_img), Color_img->depth, 1); cvAnd(ch1_img, ch2_img, Mask_img); cvAnd(Mask_img, ch3_img, Mask_img); //入力画像(src_img)のマスク領域を出力画像(dst_img)へコピーする cvZero(dst_img); cvCopy(src_img, dst_img, Mask_img); //解放 cvReleaseImage(&Color_img); cvReleaseImage(&ch1_img); cvReleaseImage(&ch2_img); cvReleaseImage(&ch3_img); cvReleaseImage(&Mask_img); }
// 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; }
// This function is copied from http://mehrez.kristou.org/opencv-change-contrast-and-brightness-of-an-image/ boost::shared_ptr< Image > Image::ContrastBrightness( int contrast, int brightness ) const { if(contrast > 100) contrast = 100; if(contrast < -100) contrast = -100; if(brightness > 100) brightness = 100; if(brightness < -100) brightness = -100; uchar lut[256]; CvMat* lut_mat; int hist_size = 256; float range_0[]={0,256}; float* ranges[] = { range_0 }; int i; IplImage * dest = cvCloneImage(this); IplImage * GRAY; if (this->nChannels == 3) { GRAY = cvCreateImage(cvGetSize(this),this->depth,1); cvCvtColor(this,GRAY,CV_RGB2GRAY); } else { GRAY = cvCloneImage(this); } lut_mat = cvCreateMatHeader( 1, 256, CV_8UC1 ); cvSetData( lut_mat, lut, 0 ); /* * 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( i = 0; i < 256; i++ ) { int v = cvRound(a*i + b); if( v < 0 ) v = 0; if( v > 255 ) v = 255; lut[i] = v; } } else { double delta = -128.* contrast/100; double a = (256.-delta*2)/255.; double b = a* brightness + delta; for( i = 0; i < 256; i++ ) { int v = cvRound(a*i + b); if( v < 0 ) v = 0; if( v > 255 ) v = 255; lut[i] = v; } } if (this->nChannels ==3) { IplImage * R = cvCreateImage(cvGetSize(this),this->depth,1); IplImage * G = cvCreateImage(cvGetSize(this),this->depth,1); IplImage * B = cvCreateImage(cvGetSize(this),this->depth,1); cvCvtPixToPlane(this,R,G,B,NULL); cvLUT( R, R, lut_mat ); cvLUT( G, G, lut_mat ); cvLUT( B, B, lut_mat ); cvCvtPlaneToPix(R,G,B,NULL,dest); cvReleaseImage(&R); cvReleaseImage(&G); cvReleaseImage(&B); } else { cvLUT( GRAY, dest, lut_mat ); } cvReleaseImage(&GRAY); cvReleaseMat( &lut_mat); return boost::shared_ptr< Image >( new Image( dest, true ) ); }
void COpenCVMFCView::OnHistEqualize() { // TODO: Add your command handler code here IplImage *src = 0, *dst = 0; CvHistogram *hist = 0; int n = HDIM; double nn[HDIM]; uchar T[HDIM]; CvMat *T_mat; int x; int sum = 0; // sum of pixels of the source image double val = 0; src = workImg; cvNamedWindow( "source", 1 ); cvNamedWindow( "result", 1 ); // Caculate Hist hist = cvCreateHist( 1, &n, CV_HIST_ARRAY, 0, 1 ); cvCalcHist( &src, hist, 0, 0 ); // Create Accumulative Distribute Function of histgram val = 0; for ( x = 0; x < n; x++) { val = val + cvGetReal1D (hist->bins, x); nn[x] = val; } // Normalization sum = src->height * src->width; for( x = 0; x < n; x++ ) { T[x] = (uchar) (255 * nn[x] / sum); // range is [0,255] } // Using look-up table to perform intensity transform for source image dst = cvCloneImage( src ); T_mat = cvCreateMatHeader( 1, 256, CV_8UC1 ); cvSetData( T_mat, T, 0 ); // invoke to complete look-up-table cvLUT( src, dst, T_mat ); cvShowImage( "source", src ); cvFlip(dst); cvShowImage( "result", dst ); cvReleaseHist ( &hist ); cvWaitKey(0); cvDestroyWindow("source"); cvDestroyWindow("result"); cvFlip(dst); m_dibFlag = imageReplace(dst,&workImg); cvReleaseMat(&T_mat); //cvReleaseImage( &src ); cvReleaseImage( &dst ); Invalidate(); }
//命令模式: 程序名 文件名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; }