KMeans::KMeans(int anumOfCores,int anumOfSamples,int anumOfBins) { int i; this->numOfCores= anumOfCores; this->numOfSamples=anumOfSamples; this->numOfBins=anumOfBins; this->itemNum=new int [this->numOfCores]; this->componentLength = 3*this->numOfBins; this->planeB=this->planeG=this->planeR=NULL; this->cores = new float * [this->numOfCores]; this->old_cores = new float * [this->numOfCores]; for(i=0;i<this->numOfCores;i++) { this->cores[i] = new float[this->componentLength]; this->old_cores[i] = new float[this->componentLength]; for(int j=0;j<this->componentLength;j++) { this->cores[i][j]=-2; this->old_cores[i][j]=-2; } } if(this->numOfSamples) { this->candyset = new float * [this->numOfSamples]; for(i=0;i<this->numOfSamples;i++) this->candyset[i] = new float[this->componentLength]; } else this->candyset=NULL; this->imagehist=new float[this->componentLength]; this->rangesR = new float * [1]; this->rangesR[0] = new float[2]; this->rangesR[0][0]=0;this->rangesR[0][1]=255; this->rangesG = new float * [1]; this->rangesG[0] = new float[2]; this->rangesG[0][0]=0;this->rangesG[0][1]=255; this->rangesB = new float * [1]; this->rangesB[0] = new float[2]; this->rangesB[0][0]=0;this->rangesB[0][1]=255; this->histR = cvCreateHist(1,&this->numOfBins,CV_HIST_ARRAY,rangesR,1); this->histG = cvCreateHist(1,&this->numOfBins,CV_HIST_ARRAY,rangesG,1); this->histB = cvCreateHist(1,&this->numOfBins,CV_HIST_ARRAY,rangesB,1); this->sorted = new vector<KMeansTriple *> [this->numOfCores]; this->index=0; }
void AdaptiveHistogramCamshift::Init(const CvSize& frameSize, const InitParams& initParams) { // Get frame size m_frameSize = frameSize; // Load init params. m_histDims = initParams.histDims; m_vMin = initParams.vMin; m_vMax = initParams.vMax; m_sMin = initParams.sMin; m_sBox = initParams.sBox; m_histRanges[0] = initParams.histRanges[0] * 0.5f; m_histRanges[1] = initParams.histRanges[1] * 0.5f; // Create histograms float* histRanges[2] = { &m_histRanges[0], &m_histRanges[1] }; m_hist = cvCreateHist(1, &m_histDims, CV_HIST_ARRAY, histRanges, 1); m_histSubdiv = cvCreateHist(1, &m_histDims, CV_HIST_ARRAY, histRanges, 1); m_histTrackWnd = cvCreateHist(1, &m_histDims, CV_HIST_ARRAY, histRanges, 1); // Create images m_imgHue = cvCreateImage(m_frameSize, 8, 1); m_imgMask = cvCreateImage(m_frameSize, 8, 1); m_imgHSV = cvCreateImage(m_frameSize, 8, 3); m_imgBackproject = cvCreateImage(m_frameSize, 8, 1); cvZero(m_imgBackproject); // Create histogram image m_histImg = cvCreateImage( m_frameSize, 8, 3 ); cvZero( m_histImg ); // Show controlsGUI, backproject, histogram if (initParams.showControlsGUI) { ShowControlsGUI(); } if (initParams.showBackproject) { ShowBackproject(); } if (initParams.showHistogram) { ShowHistogram(); } // Set initialized flag m_initialized = true; }
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; }
Histogram* Histogram::createHSHistogram(const Image* image, const Mask* mask, int sizeH, int sizeS) { Histogram* hist = new Histogram(); IplImage* hsv = cvCreateImage(image->size(), 8, 3); //Create HSV image from BGR image cvCvtColor(image->cvImage(), hsv, CV_BGR2HSV); IplImage* h = cvCreateImage(image->size(), 8, 1); // create diferents planes IplImage* s = cvCreateImage(image->size(), 8, 1); IplImage* v = cvCreateImage(image->size(), 8, 1); IplImage* planes[] = {h, s}; cvCvtPixToPlane(hsv, h, s, v, NULL); int histSize[] = {sizeH, sizeS}; float hRanges[] = { 0, 180 }; /* hue varies from 0 (~0°red) to 180 (~360°red again) */ float sRanges[] = { 0, 255 }; /* saturation varies from 0 (black-gray-white) to 255 (pure spectrum color) */ float* ranges[] = {hRanges, sRanges}; hist->m_histogram = cvCreateHist(2, histSize, CV_HIST_ARRAY, ranges, 1); cvCalcHist(planes, hist->m_histogram, false, mask ? mask->cvImage() : NULL); cvReleaseImage(&hsv); cvReleaseImage(&h); cvReleaseImage(&s); cvReleaseImage(&v); return hist; }
//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; }
CamShift::CamShift(const ImgBgr& img) { hist = NULL; hue = NULL; hdims = 16; hsv = NULL; backproject = NULL; histimg = NULL; mask = NULL; float hranges_arr[] = {0,180}; float* hranges = hranges_arr; ImgIplImage frame(img); image = cvCreateImage( cvGetSize(frame), 8, 3 ); image->origin = ((IplImage*) frame)->origin; hsv = cvCreateImage( cvGetSize(frame), 8, 3 ); hue = cvCreateImage( cvGetSize(frame), 8, 1 ); mask = cvCreateImage( cvGetSize(frame), 8, 1 ); backproject = cvCreateImage( cvGetSize(frame), 8, 1 ); hist = cvCreateHist( 1, &hdims, CV_HIST_ARRAY, &hranges, 1 ); histimg = cvCreateImage( cvSize(320,200), 8, 3 ); cvZero( histimg ); //cvReleaseImage( &frame ); vmin = 10; vmax = 256; smin = 30; }
Tracker::Tracker(const IplImage * pImg) { // Parameters nHistBins = 30; // number of histogram bins rangesArr[0]= 0; // histogram range rangesArr[1] = 180; vmin = 65; vmax = 256; smin = 55; // limits for calculating hue // File-level variables pHSVImg = 0; // the input image converted to HSV color mode pHueImg = 0; // the Hue channel of the HSV image pMask = 0; // this image is used for masking pixels pProbImg = 0; // the face probability estimates for each pixel pHist = 0; // histogram of hue in the original face image nFrames = 0; // Allocate the main data structures ahead of time float * pRanges = rangesArr; pHSVImg = cvCreateImage( cvGetSize(pImg), 8, 3 ); pHueImg = cvCreateImage( cvGetSize(pImg), 8, 1 ); pMask = cvCreateImage( cvGetSize(pImg), 8, 1 ); pProbImg = cvCreateImage( cvGetSize(pImg), 8, 1 ); pHist = cvCreateHist( 1, &nHistBins, CV_HIST_ARRAY, &pRanges, 1 ); }
int LocalisationPupil::estimateThreshold(IplImage *im){ IplImage* im2 = cvCloneImage(im); // supprimer les bruits cvSmooth( im2, im2, CV_GAUSSIAN,3 ,3); int bins = 50; int index_min,index_max; int hsize[] = {bins}; float max_value = 0, min_value = 0; //ranges - grayscale 0 to 50 float xranges[] = { 0, 50 }; float* ranges[] = { xranges }; CvHistogram* hist = cvCreateHist( 1, hsize, CV_HIST_ARRAY, ranges,1); cvCalcHist( &im2, hist, 0, NULL); cvGetMinMaxHistValue( hist, &min_value, &max_value,&index_min,&index_max); int thresh; thresh = index_max+10; cvReleaseImage( &im2); return thresh; }
CvAdaptiveSkinDetector::Histogram::Histogram() { int histogramSize[] = { HistogramSize }; float range[] = { GSD_HUE_LT, GSD_HUE_UT }; float *ranges[] = { range }; fHistogram = cvCreateHist(1, histogramSize, CV_HIST_ARRAY, ranges, 1); cvClearHist(fHistogram); };
static UNUSED IplImage* test_find_edges_hist(IplImage *im) { int w = im->width; int h = im->height; CvSize small_size = {w / 8, h / 8}; CvPoint middle = {w/2, h/2}; IplImage *small = cvCreateImage(small_size, IPL_DEPTH_8U, 1); /*for quicker histogram */ IplImage *mask = cvCreateImage(cvGetSize(im), IPL_DEPTH_8U, 1); IplImage *green = cvCreateImage(cvGetSize(im), IPL_DEPTH_8U, 1); cvSplit(im, NULL, green, NULL, NULL); cvResize(green, small, CV_INTER_NN); //small = green; int hist_size[] = {255}; float range[] = {0, 255}; float *ranges[] = {range}; CvHistogram* hist = cvCreateHist(1, hist_size, CV_HIST_ARRAY, ranges, 1); cvCalcHist(&small, hist, 0, NULL); int pixels = small->width * small->height; int min_black = pixels / 8; int max_black = pixels / 2; int totals[256] = {0}; int best_d = pixels + 1; int best_t = 0; int total = 0; for (int i = 0; i < 255; i++){ int v = (int)cvQueryHistValue_1D(hist, i + 2); total += v; totals[i] = total; if (total > min_black){ if (i > 5){ int diff = totals[i] - totals[i - 5]; if (diff < best_d){ best_d = diff; best_t = i; } if (total >= max_black){ break; } } } } best_t -= 2; printf("found best threshold %d -- %d pixel change at %d/%d pixels\n", best_t, best_d, totals[best_t], pixels); cvCmpS(green, best_t, mask, CV_CMP_GT); IplImage *mask2 = cvCreateImage(cvGetSize(im), IPL_DEPTH_8U, 1); memset(mask2->imageData, 255, w*h); floodfill_mono_superfast(mask, mask2, middle); return mask2; }
HandDetect::HandDetect() { numColorBins = 256; max_val = 0.f; hand1 = cvLoadImage("hand.png"); hand2 = cvLoadImage("hand2.png"); hist1 = cvCreateHist(1, &numColorBins, CV_HIST_ARRAY); hist2 = cvCreateHist(1, &numColorBins, CV_HIST_ARRAY); rad=0; vmin=10, vmax=256, smin=30; capture = cvCaptureFromCAM(0); setImage(); backproject = cvCreateImage(cvGetSize(image), 8, 1); gray = cvCreateImage(cvGetSize(image), 8, 1); track_window = cvRect((int)image->width/2, (int)image->height/2, 1, 1); track_box.center.x=-1; track_box.center.y=-1; hsvHand1 = cvCreateImage(cvGetSize(hand1), 8, 3); mskHand1 = cvCreateImage(cvGetSize(hand1), 8, 1); hueHand1 = cvCreateImage(cvGetSize(hand1), 8, 1); hsvHand2 = cvCreateImage(cvGetSize(hand2), 8, 3); mskHand2 = cvCreateImage(cvGetSize(hand2), 8, 1); hueHand2 = cvCreateImage(cvGetSize(hand2), 8, 1); cvCvtColor(hand1, hsvHand1, CV_RGB2HSV); cvInRangeS(hsvHand1, cvScalar(0, smin, MIN(vmin, vmax), 0), cvScalar(180, 256, MAX(vmin, vmax), 0), mskHand1); cvSplit(hsvHand1, hueHand1, 0, 0, 0); cvCalcHist(&hueHand1, hist1, 0, mskHand1); cvGetMinMaxHistValue(hist1, 0, &max_val, 0, 0); cvConvertScale(hist1->bins, hist1->bins, max_val ? 255. / max_val : 0., 0); cvCvtColor(hand2, hsvHand2, CV_RGB2HSV); cvInRangeS(hsvHand2, cvScalar(0, smin, MIN(vmin, vmax), 0), cvScalar(180, 256, MAX(vmin, vmax), 0), mskHand2); cvSplit(hsvHand2, hueHand2, 0, 0, 0); cvCalcHist(&hueHand2, hist2, 0, mskHand2); cvGetMinMaxHistValue(hist2, 0, &max_val, 0, 0); cvConvertScale(hist2->bins, hist2->bins, max_val ? 255. / max_val : 0., 0); }
int main() { while(true) { int hist_size[] = {40}; float range[] = {0.0f,255.0f}; float* ranges[] = {range}; CvHistogram* hist = cvCreateHist(1, hist_size, CV_HIST_ARRAY, ranges, 1); cvReleaseHist(&hist); } }
Histogram::Histogram(int dims, int* sizes, float** ranges, int type, Histogram* histToAcomul) : m_histogram(histToAcomul ? histToAcomul->m_histogram : NULL) { m_histogram = cvCreateHist(dims, sizes, type, ranges, histToAcomul ? 0 : 1); }
CvHistogram* create_histogram(IplImage* plane, int range, int* bins) { float ranges[] = { 0, range }; float* temp_range[] = { ranges }; CvHistogram* hist = cvCreateHist( 1, bins, CV_HIST_ARRAY, temp_range, 1 ); cvCalcHist(&plane, hist, 0, 0 ); // Compute histogram return hist; }
CvHistogram *make_hist(IplImage *img) { int numBins = 256; float range[] = {0, 255}; float *ranges[] = { range }; CvHistogram *hist = cvCreateHist(1, &numBins, CV_HIST_ARRAY, ranges, 1); cvCalcHist(&img, hist, 0, 0); return hist; }
void Kinect::camshiftarr() { //cvSetMouseCallback("CamShiftDemo", on_mouse, 0); cvCreateTrackbar("Vmin", "CamShiftDemo", &vmin, 256, 0); cvCreateTrackbar("Vmax", "CamShiftDemo", &vmax, 256, 0); cvCreateTrackbar("Smin", "CamShiftDemo", &smin, 256, 0); //if (!KinectColorImg) //{ hsv = cvCreateImage(cvGetSize(KinectColorImg), 8, 3); hue = cvCreateImage(cvGetSize(KinectColorImg), 8, 1); mask = cvCreateImage(cvGetSize(KinectColorImg), 8, 1); backproject = cvCreateImage(cvGetSize(KinectColorImg), 8, 1); //히스토그램 생성 (채널,size ,표현방식,x축범위,막대간격 hist = cvCreateHist(1, &hdims, CV_HIST_ARRAY, &hranges, 1); //히스토 그램 출력할 공간 생성 후 초기화 histimg = cvCreateImage(cvSize(320, 200), 8, 3); cvZero(histimg); //} // //cvCvtColor(KinectColorImg, hsv, CV_BGR2HSV); //// if (!hsv) { hsv = cvCreateImage(cvGetSize(KinectColorImg), 8, 3); hue = cvCreateImage(cvGetSize(KinectColorImg), 8, 1); mask = cvCreateImage(cvGetSize(KinectColorImg), 8, 1); backproject = cvCreateImage(cvGetSize(KinectColorImg), 8, 1); //히스토그램 생성 (채널,size ,표현방식,x축범위,막대간격 hist = cvCreateHist(1, &hdims, CV_HIST_ARRAY, &hranges, 1); //히스토 그램 출력할 공간 생성 후 초기화 histimg = cvCreateImage(cvSize(320, 200), 8, 3); cvZero(histimg); } if (trackObject) { } }
/* 获取灰度图的一维直方图 * grayImageData 灰度图像数据 * hist_size 直方图中矩形条的数目 * ranges 灰度级的范围列表 */ CvHistogram* getHistogram(IplImage* grayImageData, int hist_size, float** ranges) { //创建一维直方图,统计图像在[0 255]像素的均匀分布 CvHistogram* gray_hist = cvCreateHist(1,&hist_size,CV_HIST_ARRAY,ranges,1); //计算灰度图像的一维直方图 cvCalcHist(&grayImageData,gray_hist,0,0); //归一化直方图 //cvNormalizeHist(gray_hist,1.0); return gray_hist; }
// ------------------------------------------------------------------------ LABHistogram2D::LABHistogram2D(void) { /* hue varies from 0 (~0°red) to 180 (~360°red again) */ /* saturation varies from 0 (black-gray-white) to 255 (pure spectrum color) */ int hist_size[] = {a_bins, b_bins}; float a_ranges[] = { (float)a_min, (float)a_max }; /* A varies from a_min to a_max */ float b_ranges[] = { (float)b_min, (float)b_max }; /* B varies from b_min to b_max */ float* ranges[] = { a_ranges, b_ranges }; h = cvCreateHist( 2, hist_size, CV_HIST_ARRAY, ranges, 1 ); cvClearHist(h); }
int main( int argc, char** argv ) { IplImage *src = 0; IplImage *histimg = 0; CvHistogram *hist = 0; int hbins = 255; // 划分HIST的个数,越高越精确 int maxcount = 255; float hranges_arr[] = {0,maxcount};// float* hranges = hranges_arr; int bin_w; float max_val; int i; if((src=cvLoadImage("../cvtest/7newsample.jpg", CV_LOAD_IMAGE_GRAYSCALE)) == NULL) // force to gray image return -1; cvNamedWindow( "Histogram", 0 ); cvNamedWindow( "src", 0); hist = cvCreateHist( 1, &hbins, CV_HIST_ARRAY, &hranges, 1 ); // 计算直方图 histimg = cvCreateImage( cvSize(320,200), 8, 3 ); cvZero( histimg ); cvCalcHist( &src, hist, 0, 0 ); // 计算直方图 float min_val = 0; cvGetMinMaxHistValue( hist, &min_val, &max_val, 0, 0 ); // 只找最大值 printf("max_val:%.4f,min:%.4f\n",max_val,min_val); cvConvertScale( hist->bins, hist->bins, max_val ? 255. / max_val : 0., 0 ); // 缩放 bin 到区间 [0,255] cvZero( histimg ); bin_w = histimg->width / hbins; // hbins: 条的个数,则 bin_w 为条的宽度 // 画直方图 for( i = 0; i < hbins; i++ ) { double val = ( cvGetReal1D(hist->bins,i)/*255份中的几份*/*histimg->height/maxcount ); CvScalar color = CV_RGB(255,255,0); //(hsv2rgb(i*180.f/hbins); cvRectangle( histimg, cvPoint(i*bin_w,histimg->height), cvPoint((i+1)*bin_w,(int)(histimg->height - val)), color, 1, 8, 0 ); printf("(%d,%d),(%d,%d)\n",i*bin_w,histimg->height,(i+1)*bin_w,(int)(histimg->height - val)); } cvShowImage( "src", src); cvShowImage( "Histogram", histimg ); cvWaitKey(0); cvDestroyWindow("src"); cvDestroyWindow("Histogram"); cvReleaseImage( &src ); cvReleaseImage( &histimg ); cvReleaseHist ( &hist ); return 0; }
void determine_optimal_sign_classification( IplImage* original_image, IplImage* red_point_image, CvSeq* red_components, CvSeq* background_components, IplImage* result_image ) { int width_step=original_image->widthStep; int pixel_step=original_image->widthStep/original_image->width; IplImage* mask_image = cvCreateImage( cvGetSize(original_image), 8, 1 ); IplImage* grayscale_image = cvCreateImage( cvGetSize(original_image), 8, 1 ); cvConvertImage( original_image, grayscale_image ); IplImage* thresholded_image = cvCreateImage( cvGetSize(original_image), 8, 1 ); cvZero( thresholded_image ); cvZero( result_image ); int row=0,col=0; CvSeq* curr_red_region = red_components; while (curr_red_region != NULL) { cvZero( mask_image ); CvScalar color = CV_RGB( 255, 255, 255 ); CvScalar mask_value = cvScalar( 255 ); //white point // Determine which background components are contained within the red component (i.e. holes) // and create a binary mask of those background components. CvSeq* curr_background_region = curr_red_region->v_next; if (curr_background_region != NULL) { while (curr_background_region != NULL) { cvDrawContours( mask_image, curr_background_region, mask_value, mask_value, -1, CV_FILLED, 8 ); cvDrawContours( result_image, curr_background_region, color, color, -1, CV_FILLED, 8 ); curr_background_region = curr_background_region->h_next; } int hist_size=256; CvHistogram* hist = cvCreateHist( 1, &hist_size, CV_HIST_ARRAY ); cvCalcHist( &grayscale_image, hist, 0, mask_image ); // Determine an optimal threshold on the points within those components (using the mask) int optimal_threshold = determine_optimal_threshold( hist ); apply_threshold_with_mask(grayscale_image,result_image,mask_image,optimal_threshold); } curr_red_region = curr_red_region->h_next; } for (row=0; row < result_image->height; row++) { unsigned char* curr_red = GETPIXELPTRMACRO( red_point_image, 0, row, width_step, pixel_step ); unsigned char* curr_result = GETPIXELPTRMACRO( result_image, 0, row, width_step, pixel_step ); for (col=0; col < result_image->width; col++) { curr_red += pixel_step; curr_result += pixel_step; if (curr_red[0] > 0) curr_result[2] = 255; } } cvReleaseImage( &mask_image ); }
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); }
size_t calcularHistograma(IplImage *src, size_t *binsCount, size_t **bins) { if (src == NULL || bins == NULL) { return -1; } static int _trueChannels = 1; static int _trueChannel = 1; int channels = src->nChannels; int hist_size = 256; if (*bins == NULL) { *bins = (size_t *)calloc(channels * hist_size, sizeof(size_t)); *binsCount = channels; } //if (channels == 3) printf("%d (%p)", *binsCount, *bins); //Actuo en funcion la cantidad de colores de la imagen if (channels == 1) { float range[] = { 0, 256 }; float* ranges[] = { range }; CvHistogram *hist_bw = cvCreateHist(1, &hist_size, CV_HIST_ARRAY, ranges, 1); cvCalcHist(&src, hist_bw, 0, NULL); for (int i = 0; i < hist_size; i++) { (*bins)[_trueChannel * hist_size + i] = cvRound(cvGetReal1D(hist_bw->bins, i)); } cvReleaseHist(&hist_bw); } else if (src->nChannels == 3) { _trueChannels = 3; IplImage *channelA = cvCreateImage(cvGetSize(src), IPL_DEPTH_8U, 1); IplImage *channelB = cvCreateImage(cvGetSize(src), IPL_DEPTH_8U, 1); IplImage *channelC = cvCreateImage(cvGetSize(src), IPL_DEPTH_8U, 1); cvSplit(src, channelA, channelB, channelC, NULL); _trueChannel = 0; calcularHistograma(channelA, binsCount, bins); cvReleaseImage(&channelA); _trueChannel = 1; calcularHistograma(channelB, binsCount, bins); cvReleaseImage(&channelB); _trueChannel = 2; calcularHistograma(channelC, binsCount, bins); cvReleaseImage(&channelC); _trueChannels = 1; _trueChannel = 1; } else { return -1; } return 0; }
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); }
size_t calcularHistograma(IplImage *src, size_t *binsCount, size_t **bins) { if (src == NULL || bins == NULL) { return -1; } int channels = src->nChannels; int hist_size = 256; if (*bins == NULL) { *bins = (size_t*)calloc(channels * hist_size, sizeof(size_t)); *binsCount = channels; } //if (channels == 3) printf("%d (%p)", *binsCount, *bins); //Actúo en función de la cantidad de colores de la imágen if (channels == 1) { float range[] = { 0, 256 }; float* ranges[] = { range }; float max = 0.0; float w_scale = 0.0; CvHistogram *hist_bw = cvCreateHist(1, &hist_size, CV_HIST_ARRAY, ranges, 1); cvCalcHist(&src, hist_bw, 0, NULL); //float max_value = 0.0; //cvGetMinMaxHistValue(hist_bw, 0, &max_value, 0, 0); //cvScale(hist_bw->bins, hist_bw->bins, (float)(src->width*src->height) / max_value, 0); for (int i = 1; i < hist_size; i++) { (*bins)[(*binsCount - 1) * hist_size + i] = cvRound(cvGetReal1D(hist_bw->bins, i)); } cvReleaseHist(&hist_bw); } else if (src->nChannels == 3) { IplImage *channelA = cvCreateImage(cvGetSize(src), IPL_DEPTH_8U, 1); IplImage *channelB = cvCreateImage(cvGetSize(src), IPL_DEPTH_8U, 1); IplImage *channelC = cvCreateImage(cvGetSize(src), IPL_DEPTH_8U, 1); cvSplit(src, channelA, channelB, channelC, NULL); calcularHistograma(channelA, binsCount, bins); cvReleaseImage(&channelA); calcularHistograma(channelB, binsCount, bins); cvReleaseImage(&channelB); calcularHistograma(channelC, binsCount, bins); cvReleaseImage(&channelC); } else { return -1; } return 0; }
////////////////////////////////// // createTracker() // int createTracker(const IplImage * pImg) { // Allocate the main data structures ahead of time float * pRanges = rangesArr; pHSVImg = cvCreateImage( cvGetSize(pImg), 8, 3 ); pHueImg = cvCreateImage( cvGetSize(pImg), 8, 1 ); pMask = cvCreateImage( cvGetSize(pImg), 8, 1 ); pProbImg = cvCreateImage( cvGetSize(pImg), 8, 1 ); pHist = cvCreateHist( 1, &nHistBins, CV_HIST_ARRAY, &pRanges, 1 ); return 1; }
int th_create_hist(CvHistogram** hist, int dims, int* sizes, int type, float** ranges, int uniform) { int R = 0; CvHistogram* src = *hist; if (src == 0x0) { // yes it exists, but has wrong properties -> delete it! if (src != 0x0) cvReleaseHist(hist); // now the new one can safely be created *hist = cvCreateHist(dims, sizes, type, ranges, uniform); R = 1; } return R; }
int BoatDetecting::setupImages(int frameWidth, int frameHeight) { image = cvCreateImage(cvSize(frameWidth, frameHeight), IPL_DEPTH_8U, 3); hsv = cvCreateImage(cvSize(frameWidth, frameHeight), IPL_DEPTH_8U, 3); hue = cvCreateImage(cvSize(frameWidth, frameHeight), IPL_DEPTH_8U, 1); mask = cvCreateImage(cvSize(frameWidth, frameHeight), IPL_DEPTH_8U, 1); backproject = cvCreateImage(cvSize(frameWidth, frameHeight), IPL_DEPTH_8U, 1); int hdims = 16; float hranges_arr[] = { 0, 180 }; float *hranges = hranges_arr; hist = cvCreateHist(1, &hdims, CV_HIST_ARRAY, &hranges, 1); return 1; }
int main(int argc, char* argv[]) { cvNamedWindow("Original",CV_WINDOW_AUTOSIZE); cvNamedWindow("Binaria",CV_WINDOW_AUTOSIZE); cvNamedWindow("Histograma",CV_WINDOW_AUTOSIZE); imatge = cvLoadImage("C:\\EUPMT\\Projects\\OpenCV_Contours\\exemple03.jpg", 0); IplImage* binaria = cvCreateImage(cvGetSize(imatge), 8, 1); imgHistogram = 0; int hsize[] = {bins}; float max_value = 0, min_value = 0; float xranges[] = { 0, 256 }; float* ranges[] = { xranges }; hist = cvCreateHist( 1, hsize, CV_HIST_ARRAY, ranges,1); cvCalcHist( &imatge, hist, 0, NULL); cvGetMinMaxHistValue( hist, &min_value, &max_value); dibuixarHistograma(max_value); int llindar = llindar_gaussian(imatge, hist); int step =imatge->widthStep; int canals = imatge->nChannels; uchar* imgData = (uchar*) imatge->imageData; uchar* imgDataBinaria = (uchar*) binaria->imageData; for( int i = 0; i < imatge->height; i++) for( int j = 0; j < imatge->width; j++) { if((imgData[i*step+j*canals]) > llindar) { imgDataBinaria[i*binaria->widthStep+j*binaria->nChannels]=0; } else { imgDataBinaria[i*binaria->widthStep+j*binaria->nChannels]=255; } } cvShowImage("Original", imatge); cvShowImage("Histograma", imgHistogram); cvShowImage("Binaria", binaria); cvWaitKey(0); cvDestroyAllWindows(); return 0; }
IplImage* CamShiftPatch::drawHistImg(CvRect selectROI, CvScalar maskRange) { IplImage* hue = 0; hue = cvCreateImage(cvGetSize(originImage), 8, 1); IplImage *mask = getInRangeMask(maskRange, hue);//CvScalar 0->Vmin 1->Vmax 2->Smin //---設定ROI和畫出直方圖--- float max_val = 0.f; cvSetImageROI(hue, selectROI); cvSetImageROI(mask, selectROI); int hdims = 48; //劃分HIST的個數,越高越精確 float hranges_arr[] = { 0, 180 }; //直方圖範圍 float* hranges = hranges_arr;//指向值方圖範圍 histogram = cvCreateHist(1, &hdims, CV_HIST_ARRAY, &hranges, 1); //設定直方圖的格式 cvCalcHist(&hue, histogram, 0, mask); //以hue資訊 得到直方圖 (只有ROI部分的資訊) cvGetMinMaxHistValue(histogram, 0, &max_val, 0, 0); //只找最大值 cvConvertScale(histogram->bins, histogram->bins, max_val ? 255. / max_val : 0., 0); //縮放bin到區間[0,255] //input output 尺度放大或縮小 全部顏色的增減 cvResetImageROI(hue); // remove ROI cvResetImageROI(mask); //track_window = selectROI; //搜索窗即一開始ROI框 //----畫直方圖hist to histimg------------------ IplImage* histimg = cvCreateImage(cvSize(320, 200), 8, 3); //直方圖顯示空間,三通道 cvZero(histimg); //置背景為黑色 int bin_w = 0; bin_w = histimg->width / hdims; // hdims:條的個數,則bin_w為條的寬度 for (int i = 0; i < hdims; i++) { int val = cvRound(cvGetReal1D(histogram->bins, i)*histimg->height / 255); //cvGetReal1D(直方條高度,第幾條) CvScalar color = hsv2rgb(i*180.f / hdims); //在RGB空間上,直方圖每條的顏色計算 180/hdims表示每一格多寬 , i*180/hdims代表到橫軸的哪個位置了 由那個位置的顏色資訊轉換成RGB cvRectangle(histimg, cvPoint(i*bin_w, histimg->height), cvPoint((i + 1)*bin_w, histimg->height - val), color, -1, 8, 0);//畫出不同顏色的直方長條來 } //---設定ROI和畫出直方圖 end--- IplImage* returnImg = nullptr; returnImg = cvCloneImage(histimg); cvReleaseImage(&hue); cvReleaseImage(&mask); cvReleaseImage(&histimg); return returnImg; }
Histogram* Histogram::create1ChHistogram(const Image* image, const Mask* mask) { Histogram* hist = new Histogram(); int histSize[] = {256}; float range[] = { 0, 255 }; float* ranges[] = {range}; IplImage* cvimage = image->cvImage(); hist->m_histogram = cvCreateHist(1, histSize, CV_HIST_ARRAY, ranges, 1); cvCalcHist(&cvimage , hist->m_histogram, false, mask ? mask->cvImage() : NULL); hist->normalize(); return hist; }