//pL路径集, hsv:颜色集 //功能:图片集与hsv集进行匹配,对图片集进行重新排序 void ColorFilter::match(vector<int> &pL, vector<CvScalar> &hsv, vector<CvScalar> ¬Hsv, exData &database) { double rateH, rateS; //hist.buildHist(pL, database); hist.readFewData(pL, database); pcm.clear(); rateH = 180.0 / h_bins, rateS = 255.0 / s_bins; for(int i = 0; i < hist.data.size(); i++) { PCM p; bool notMatch = false; double t; t = 0; for(int j = 0; j < hsv.size(); j++) { int hCor = abs((hsv[j].val[0] - 1) / rateH); int sCor = abs((hsv[j].val[1] - 1) / rateS); double x; x = cvQueryHistValue_2D(hist.data[i].hist, hCor, sCor); if(x <= 0) { notMatch = true; break; } t += x; } for(int j = 0; j < notHsv.size(); j++) { int hCor = abs((notHsv[j].val[0] - 1) / rateH); int sCor = abs((notHsv[j].val[1] - 1) / rateS); double x; x = cvQueryHistValue_2D(hist.data[i].hist, hCor, sCor); if(x > 0) { notMatch = true; break; } } if(notMatch) continue; p.loc = hist.data[i].loc; p.index = hist.data[i].index; p.matchValue = t; pcm.push_back(p); } sort(pcm.begin(), pcm.end(), cmp); pL.clear(); for(int i = 0; i < pcm.size(); i++) pL.push_back(pcm[i].index); }
Mask* Histogram::representInMask() { int h_bins = 30, s_bins = 32; int scale = 10; IplImage* hist_image = cvCreateImage(cvSize( h_bins * scale, s_bins * scale ), 8, 3); cvZero( hist_image ); /* populate our visualization with little gray squares. */ float max_value = 0; cvGetMinMaxHistValue( m_histogram, 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( m_histogram, h, s ); int intensity = cvRound( bin_val * 255 / max_value ); cvRectangle(hist_image, cvPoint( h*scale, s*scale ), cvPoint( (h+1)*scale - 1, (s+1)*scale - 1), CV_RGB(intensity,intensity,intensity), CV_FILLED); } } return new Image(hist_image); }
// ------------------------------------------------------------------------ double LABHistogram2D::GetValue(double a, double b) { if (a < a_min || a >= a_max || b < b_min || b >= b_max) return 0; int A = (int)((a-a_min)*a_bins/(a_max-a_min)); int B = (int)((b-b_min)*b_bins/(b_max-b_min)); return cvQueryHistValue_2D(h, A, B); }
// ------------------------------------------------------------------------ System::String^ LABHistogram2D::ToString() { if( !CV_IS_HIST(h)) ASSERT(false); int size[CV_MAX_DIM]; int dims = cvGetDims( this->h->bins, size ); int total = 1; for(int i = 0; i < dims; i++ ) total *= size[i]; System::String ^s; float *ptr = 0; cvGetRawData( this->h->bins, (uchar**)&ptr); for(int i=0; i<a_bins;i++ ) { for(int j=0; j<b_bins;j++ ) { float bin_val = cvQueryHistValue_2D( h, i, j ); s += System::String::Format("{0:f4}, ", bin_val); } s += "\n"; } return s; }
// ------------------------------------------------------------------------ void LABHistogram2D::Draw(CDC* pDC) { CPen pen(PS_SOLID, 1, RGB(0, 0, 0)); CPen* pOldPen = pDC->SelectObject(&pen); float min_value = 0, max_value = 0; cvGetMinMaxHistValue( h, &min_value, &max_value, 0, 0 ); CRect box; pDC->GetWindow()->GetWindowRect(&box); int iscale = box.Width()/a_bins; int jscale = box.Height()/b_bins; for(int i=0; i<a_bins;i++ ) { for(int j=0; j<b_bins;j++ ) { float bin_val = cvQueryHistValue_2D( h, i, j ); int intensity = cvRound((bin_val-min_value)*255/(max_value-min_value)); CBrush fill; fill.CreateSolidBrush(RGB(0,0,intensity)); CRect r(i*iscale, j*jscale, (i+1)*iscale - 1, (j+1)*jscale - 1); pDC->FillRect(&r, &fill); } } pDC->SelectObject(pOldPen); }
void Draw_hist(IplImage* img,CvRect Rect) { if(Rect.x<=0||Rect.y<=0||Rect.width<=0||Rect.height<=0) { return; } cvSetImageROI(img,Rect); IplImage* hsv=cvCreateImage(cvGetSize(img),8,3); IplImage* h_plane=cvCreateImage(cvGetSize(img),8,1); IplImage* s_plane=cvCreateImage(cvSize(Rect.width,Rect.height),8,1); IplImage* v_plane=cvCreateImage(cvSize(Rect.width,Rect.height),8,1); IplImage* planes[]={h_plane,s_plane}; int h_bins=16,s_bins=8; int hist_size[]={h_bins,s_bins}; float h_ranges[]={0,180}; float s_ranges[]={0,255}; float* ranges[]={h_ranges,s_ranges}; cvCvtColor(img,hsv,CV_BGR2HSV); cvResetImageROI(img); cvSplit(hsv,h_plane,s_plane,v_plane,0); CvHistogram *hist=cvCreateHist(2,hist_size,CV_HIST_ARRAY,ranges,1); cvCalcHist(planes,hist,0,0); float max_value; cvGetMinMaxHistValue(hist,0,&max_value,0,0); int height=480; int width=(h_bins*s_bins*6); IplImage* hist_img=cvCreateImage(cvSize(width,height),8,3); cvZero(hist_img); IplImage* hsv_color=cvCreateImage(cvSize(1,1),8,3); IplImage* rgb_color=cvCreateImage(cvSize(1,1),8,3); int bin_w=width/(h_bins*s_bins); for(int h=0;h<h_bins;h++) { for(int s=0;s<s_bins;s++) { int i=h*s_bins+s; //获取直方图中统计的次数,计算显示在图像中的高度 float bin_val=cvQueryHistValue_2D(hist,h,s); int intensity=cvRound(bin_val*height/max_value); cvSet2D(hsv_color,0,0,cvScalar(h*180.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); } } cvNamedWindow("H-S Histogram",1); cvShowImage("H-S Histogram",hist_img); }
int hist2sig(CvHistogram* hist1,CvHistogram* hist2,int h_bins, int s_bins) { int numrows = h_bins*s_bins; // Create matrices to store the signature in CvMat* sig1 = cvCreateMat(numrows, 3, CV_32FC1);//1 count + 2 coords = 3 CvMat* sig2 = cvCreateMat(numrows, 3, CV_32FC1);//sigs are of type float // Fill signatures for the two histograms for (int h = 0; h < h_bins; h++) { for (int s = 0; s < s_bins; s++) { float bin_val = cvQueryHistValue_2D(hist1, h, s); cvSet2D(sig1, h*s_bins + s, 0, cvScalar(bin_val));// bin value cvSet2D(sig1, h*s_bins + s, 1, cvScalar(h));//Coord 1 cvSet2D(sig1, h*s_bins + s, 2, cvScalar(s)); bin_val = cvQueryHistValue_2D(hist2, h, s); cvSet2D(sig2, h*s_bins + s, 0, cvScalar(bin_val));//bin value cvSet2D(sig2, h*s_bins + s, 1, cvScalar(h));//Coord 1 cvSet2D(sig2, h*s_bins + s, 2, cvScalar(s));//Coord 2 } } return 0; }
Signature* Histogram::createSignature(const Histogram* histogram) { int hSize = histogram->dimensionSize(0), sSize = histogram->dimensionSize(1); int rowsCount = hSize * sSize; //get size if (!rowsCount) return NULL; // Only works with 2D (or more) histograms Signature* signature = cvCreateMat(rowsCount, 3, CV_32FC1); //Signature is a Mat /* Set values for signature */ for (int h = hSize; h-- /* NOT --h */; ) // We supouse that it's HSV histogram for (int s = sSize; s-- /* NOT --s */; ) { float value = cvQueryHistValue_2D(histogram->m_histogram, h, s); cvSet2D(signature, h * sSize + s, 0, cvScalar(value)); cvSet2D(signature, h * sSize + s, 1, cvScalar(h)); cvSet2D(signature, h * sSize + s, 2, cvScalar(s)); } return signature; }
// ------------------------------------------------------------------------ void LABHistogram2D::Draw(IplImage* frame, int scale, CvPoint p) { int iscale = scale, jscale = scale; if (scale == 0) { iscale = frame->width/a_bins; jscale = frame->height/b_bins; } else { iscale = scale; jscale = scale; } float min_val = 0, max_val = 0; cvGetMinMaxHistValue( h, &min_val, &max_val, 0, 0 ); for(int i=0; i<a_bins;i++ ) { for(int j=0; j<b_bins;j++ ) { float bin_val = cvQueryHistValue_2D( h, i, j ); int index = (int)(NTEMPERATURES*(bin_val-min_val-1)/(max_val-min_val)); cvRectangle( frame, cvPoint( p.x+i*iscale, p.y+j*jscale ), cvPoint( (i+1)*iscale - 1 + p.x, (j+1)*jscale - 1 + p.y), CV_RGB(HEAT_COLOR[index].R, HEAT_COLOR[index].G, HEAT_COLOR[index].B), CV_FILLED ); } } }
int main( int argc, char** argv ) { IplImage* src; if( argc == 2 && (src=cvLoadImage(argv[1], 1))!= 0) { // Compute the HSV image, and decompose it into separate planes. // IplImage* hsv = cvCreateImage( cvGetSize(src), 8, 3 ); cvCvtColor( src, hsv, 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[] = { h_plane, s_plane }; cvCvtPixToPlane( hsv, h_plane, s_plane, v_plane, 0 ); // Build the histogram and compute its contents. // int h_bins = 30, s_bins = 32; CvHistogram* hist; { int hist_size[] = { h_bins, s_bins }; float h_ranges[] = { 0, 180 }; // hue is [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 ); // Create an image to use to visualize our histogram. // int scale = 10; IplImage* hist_img = cvCreateImage( cvSize( h_bins * scale, s_bins * scale ), 8, 3 ); cvZero( hist_img ); // populate our visualization with little gray squares. // 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 ); } } cvNamedWindow( "Source", 1 ); cvShowImage( "Source", src ); cvNamedWindow( "H-S Histogram", 1 ); cvShowImage( "H-S Histogram", hist_img ); cvWaitKey(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; }