コード例 #1
0
ファイル: Histogram.cpp プロジェクト: margguo/tpf-robotica
//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;
	
}
コード例 #2
0
ファイル: tracker.cpp プロジェクト: amnosuperman/Sign2Text
// startTracking()
//
void startTracking(IplImage * pImg, CvRect pHandRect,KalmanFilter &kfilter)
{
	float maxVal = 0.f;

	// Make sure internal data structures have been allocated
	if( !pHist ) createTracker(pImg);

	// Create a new hue image
	updateHueImage(pImg);

    if(!((pHandRect.x<0)||(pHandRect.y<0)||((pHandRect.x+pHandRect.width)>pImg->width)||((pHandRect.y+pHandRect.height)>pImg->height))) {

	// Create a histogram representation for the hand
    cvSetImageROI( pHueImg, pHandRect );
    cvSetImageROI( pMask,   pHandRect );
    cvCalcHist( &pHueImg, pHist, 0, pMask );
    cvGetMinMaxHistValue( pHist, 0, &maxVal, 0, 0 );
    cvConvertScale( pHist->bins, pHist->bins, maxVal? 255.0/maxVal : 0, 0 );
    cvResetImageROI( pHueImg );
    cvResetImageROI( pMask );

	}
	// Store the previous hand location
	prevHandRect =pHandRect;
	prevHandRect2 =pHandRect;

	//Pass the hand location to kalman initializer
	kfilter.predictionBegin(prevHandRect);
	
}
コード例 #3
0
	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;
	}
コード例 #4
0
ファイル: Histogram.cpp プロジェクト: endSly/CVTrackpad
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;
}
コード例 #5
0
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;
}
コード例 #6
0
ファイル: HandDetect.cpp プロジェクト: gunbaek/Hand2Mouse
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);
}
コード例 #7
0
ファイル: obsdet.c プロジェクト: jlefley/igvrt-uiuc
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;
}
コード例 #8
0
ファイル: cd.c プロジェクト: j0sh/thesis
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;
}
コード例 #9
0
ファイル: KMeans.cpp プロジェクト: fithisux/XSEP
//It tries to find out the class
//that frame belongs to.
//It does it using the L2 norm. It sorts the results
//and select the nearest core. If cores are empty
//it returns -1
KMeansTriple KMeans::findClass(IplImage *frame)
{	
	float Diff;
	KMeansTriple props;
	int m,j,i,temp;

	cvSplit(frame,this->planeR,this->planeG,this->planeB,0);
	cvCalcHist(&this->planeR,this->histR,0,NULL);
    cvCalcHist(&this->planeG,this->histG,0,NULL);
    cvCalcHist(&this->planeB,this->histB,0,NULL);
    
	for(i=0;i<this->numOfBins;i++)
	{ 
     this->imagehist[i]= cvQueryHistValue_1D(this->histR,i);     
	}

	for(temp=this->numOfBins,i=0;i<this->numOfBins;i++)
	{ 
     this->imagehist[i+temp]= cvQueryHistValue_1D(this->histG,i);     
	}

	for(temp+=this->numOfBins,i=0;i<this->numOfBins;i++)
	{ 
     this->imagehist[i+temp]= cvQueryHistValue_1D(this->histB,i);     
	}

	for(props.classID=-1,props.Diff=10000,m=0;m<this->numOfCores;m++)
	{
		for(Diff=0,j=0;j<this->componentLength;j++)	   
		{
		   Diff+=(float) fabs(this->imagehist[j]-this->cores[m][j]);
		}
                
		if(props.Diff>Diff)
		{
			props.Diff=Diff;
			props.classID=m;
		}
	}	

	props.Id=0;
	return props;
}
コード例 #10
0
ファイル: pic_binarization.cpp プロジェクト: littledeer/OCSR
/* 获取灰度图的一维直方图
 * 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;
}
コード例 #11
0
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; 
}
コード例 #12
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 );
}
コード例 #13
0
ファイル: CamShiftPlugin.cpp プロジェクト: conanhung/examples
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);
}
コード例 #14
0
ファイル: ocv_histogram.c プロジェクト: changeyourdestiny/DIP
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;
}
コード例 #15
0
ファイル: gui.c プロジェクト: Astrocoderguy/openStarStack
void UpdateButton(GtkObject *object, gpointer user_data) {

	int 		numBins = 256;
	float 		range[] = {0, 255};
	float 		*ranges[] = { range };


	cvReleaseImage(&imgStack8bit);
	imgStack8bit = AdjustHistogram(imgStack, (float)gtk_adjustment_get_value(adjHistLow), (float)gtk_adjustment_get_value(adjHistHigh));
	UpdateMainImage(imgStack8bit);	

	//Generate histogram from 8bit stacked image
	hist = cvCreateHist(1, &numBins, CV_HIST_ARRAY, ranges, 1);
	cvClearHist(hist);
	
	imgRed = cvCreateImage(cvGetSize(imgStack8bit), 8, 1);
	imgGreen = cvCreateImage(cvGetSize(imgStack8bit), 8, 1);
	imgBlue = cvCreateImage(cvGetSize(imgStack8bit), 8, 1);

	cvSplit(imgStack8bit, imgBlue, imgGreen, imgRed, NULL);

	cvCalcHist(&imgRed, hist, 0, 0);
	imgHistRed = DrawHistogram(hist,1,1);
	cvClearHist(hist);

	cvCalcHist(&imgGreen, hist, 0, 0);
	imgHistGreen = DrawHistogram(hist,1,1);
	cvClearHist(hist);
 
	cvCalcHist(&imgBlue, hist, 0, 0);
	imgHistBlue = DrawHistogram(hist,1,1);
 	cvClearHist(hist);

	//Display the histogram images
	UpdateRedHistImage(imgHistRed);
	UpdateGreenHistImage(imgHistGreen);	 
	UpdateBlueHistImage(imgHistBlue);	 


}
コード例 #16
0
ファイル: KMeans.cpp プロジェクト: fithisux/XSEP
// If not ready , it initializes the cores
// else it updates the nearest core to frame and updates the
// number of updates for this particular core
void KMeans::updateCore(IplImage *frame,IplImage *mask)
{	
	int i;

	if(!this->numOfSamples) return;

	if(this->index==0)
	{
		this->planeR = cvCreateImage(cvGetSize(frame),IPL_DEPTH_8U,1);
		this->planeG = cvCreateImage(cvGetSize(frame),IPL_DEPTH_8U,1);
		this->planeB = cvCreateImage(cvGetSize(frame),IPL_DEPTH_8U,1);
	}
	
	cvSplit(frame,this->planeR,this->planeG,this->planeB,0);

	cvCalcHist(&this->planeR,this->histR,0,mask);
	cvCalcHist(&this->planeG,this->histG,0,mask);
	cvCalcHist(&this->planeB,this->histB,0,mask);

	cvNormalizeHist(this->histR,1.0);
	cvNormalizeHist(this->histG,1.0);
	cvNormalizeHist(this->histB,1.0);
	
	float *runner=this->candyset[this->index++];
	
	for(i=0;i<this->numOfBins;i++)
	{ 
		*runner++=cvQueryHistValue_1D(this->histR,i);
	}

	for(i=0;i<this->numOfBins;i++)
	{ 
     *runner++=cvQueryHistValue_1D(this->histG,i);
	}

	for(i=0;i<this->numOfBins;i++)
	{ 
     *runner++=cvQueryHistValue_1D(this->histB,i);
	}
}
コード例 #17
0
ファイル: 4.3.cpp プロジェクト: blackcell000/learning_opencv
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);
}
コード例 #18
0
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;
}
コード例 #19
0
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;
}
コード例 #20
0
ファイル: cria_arquivo.c プロジェクト: RxDx/Photomosaic
void maxHSV(IplImage *img, No *imagem){
	/* variaveis do histograma */
	int numBins = 256;
	float range[] = {0, 255};
	float *ranges[] = {range};
	
	/* cria um histograma */
	CvHistogram *hist = cvCreateHist(1, &numBins, CV_HIST_ARRAY, ranges, 1);
	cvClearHist(hist);
	
	/* aloca as imagens auxiliares */
	IplImage *imgHSV = cvCreateImage(cvGetSize(img), 8, 3);
	IplImage *imgH = cvCreateImage(cvGetSize(img), 8, 1);
	IplImage *imgS = cvCreateImage(cvGetSize(img), 8, 1);
	IplImage *imgV = cvCreateImage(cvGetSize(img), 8, 1);

	cvCvtColor(img, imgHSV, CV_BGR2HSV);			// converte a img (RGB) para imgHSV (HSV)
	cvSplit(imgHSV, imgH, imgS, imgV, NULL);		// separa os canais da imgHSV
	
	cvCalcHist(&imgH, hist, 0, 0);					// calcula o histograma da imgH
	cvGetMinMaxHistValue(hist, NULL, NULL, NULL, &imagem->max_h); // coleta a cor dominante
	cvClearHist(hist);
	
	cvCalcHist(&imgS, hist, 0, 0);
	cvGetMinMaxHistValue(hist, NULL, NULL, NULL, &imagem->max_s);
	cvClearHist(hist);
	
	cvCalcHist(&imgV, hist, 0, 0);
	cvGetMinMaxHistValue(hist, NULL, NULL, NULL, &imagem->max_v);
	cvClearHist(hist);
	
	/* libera as imagens auxiliares */
	cvReleaseImage(&imgHSV);
	cvReleaseImage(&imgH);
	cvReleaseImage(&imgS);
	cvReleaseImage(&imgV);
}
コード例 #21
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;
}
コード例 #22
0
ファイル: Histogram.cpp プロジェクト: endSly/CVTrackpad
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;
}
コード例 #23
0
ファイル: OpenCVMFCView.cpp プロジェクト: huihui891/OpenCVMFC
void COpenCVMFCView::OnImageHistogram()
{
	// TODO: Add your command handler code here

	IplImage *src;
	IplImage *histimg = 0;
	CvHistogram *hist = 0;

	int hdims = 256;     // Divide Number of HIST, the more the accurator
	float hranges_arr[] = {0,255};
	float* hranges = hranges_arr;
	int bin_w;  
	float max_val;
	int i;

	src = workImg;

	cvNamedWindow( "Histogram", 0 );

	hist = cvCreateHist( 1, &hdims, CV_HIST_ARRAY, &hranges, 1 );  // Create Hist
	histimg = cvCreateImage( cvSize(320,200), 8, 3 );
	cvZero( histimg );
	cvCalcHist( &src, hist, 0, 0 ); // Caculate Hist
	cvGetMinMaxHistValue( hist, 0, &max_val, 0, 0 );  // just wanna the Max
	cvConvertScale( hist->bins, hist->bins, 
		max_val ? 255. / max_val : 0., 0 ); // resize bin to [0,255] 
	cvZero( histimg );
	bin_w = histimg->width / hdims;

	// Draw It
	for( i = 0; i < hdims; i++ )
	{
		double val = ( cvGetReal1D(hist->bins,i)*histimg->height/255 );
		CvScalar color = CV_RGB(255,255,0); //(hsv2rgb(i*180.f/hdims);
		cvRectangle( histimg, cvPoint(i*bin_w,histimg->height),
			cvPoint((i+1)*bin_w,(int)(histimg->height - val)),
			color, 1, 8, 0 );
	}

	cvShowImage( "Histogram", histimg );

	cvReleaseImage( &histimg );
	cvReleaseHist ( &hist );
	cvWaitKey(0);

	cvDestroyWindow("Histogram");
}
コード例 #24
0
ファイル: camshift.c プロジェクト: darksidelemm/ASG-Software
void initTracking(camshift * cs, IplImage * img, CvRect * faceRect)
{
  float maxVal = 0.f;

  updateHueImage(cs, img);

  cvSetImageROI( cs->hueImg, *faceRect );
  cvSetImageROI( cs->mask,   *faceRect );
  cvCalcHist( &cs->hueImg, cs->hist, 0, cs->mask );
  cvGetMinMaxHistValue( cs->hist, 0, &maxVal, 0, 0 );
  cvConvertScale( cs->hist->bins, cs->hist->bins,
      maxVal? 255.0/maxVal : 0, 0 );
  cvResetImageROI( cs->hueImg );
  cvResetImageROI( cs->mask );

  cs->prevFaceRect = *faceRect;
}
コード例 #25
0
ファイル: widget.cpp プロジェクト: xian0gang/test
void Widget::zhifangtu(IplImage *hist_img)
{
    IplImage *histimg = 0;
    CvHistogram *hist = 0;                    //直方图
    int hdims=255;
     float hranges_arr[] = {0,255};
        float* hranges = hranges_arr;
        int bin_w;
        float max_val,min_val;
        int i;

        cvNamedWindow( "Histogram", 0 );
//        cvNamedWindow( "src", 0);
hist = cvCreateHist( 1, &hdims, CV_HIST_ARRAY, &hranges, 1 );   //创建直方图
histimg = cvCreateImage( cvSize(320,200), 8, 3 );
    cvZero( histimg );                                            //清零

    cvCalcHist( &hist_img, hist, 0, 0 );                               // 计算直方图,即每个bin的大小
    cvGetMinMaxHistValue( hist, &min_val, &max_val, 0, 0 );              // 只找最大值
    cvThreshHist(hist,50);
//qDebug("max:%4.2f",max_val);
//qDebug("min:%4.2f",min_val);
 cvConvertScale( hist->bins,hist->bins, max_val ? 255. / max_val : 0., 0 );             // 缩放 bin 到区间 [0,255]

    cvZero( histimg );
    bin_w = histimg->width / hdims;                               // hdims: 直方图竖条的个数,则 bin_w 为条的宽度

    // 画直方图
    for( i = 0; i < hdims; i++ )
    {
        double val = ( cvGetReal1D(hist->bins,i)*histimg->height/255 );
        CvScalar color = CV_RGB(255,255,0);                                 //(hsv2rgb(i*180.f/hdims);
        cvRectangle( histimg, cvPoint(i*bin_w,histimg->height),
            cvPoint((i+1)*bin_w,(int)(histimg->height - val)),
            color, 1, 8, 0 );
    }

//    cvShowImage( "src", hue);
        cvShowImage( "Histogram", histimg );
cv::waitKey(0);
//    cvDestroyWindow("src");
    cvDestroyWindow("Histogram");
    //cvReleaseImage( &src );
    cvReleaseImage( &histimg );
    cvReleaseHist ( &hist );
}
コード例 #26
0
    /********************************************************************
    Utils::CalculateColorHistogram
        CalculateColorHistogram
    Exceptions:
        None
    *********************************************************************/
    void Utils::CalculateColorHistogram( IplImage*     pIplImage,
                                         CvHistogram*  pHistogram,
                                         CvRect*       pRectangle )
    {
        try
        {
            IplImage* pObjectImage;

            if ( pRectangle != NULL )
            {
                pObjectImage = OpenCvWrapper::Utils::CropImage( pIplImage, 
                                                            pRectangle );
            }
            else
            {
                pObjectImage = pIplImage;
            }


            IplImage* h_plane = cvCreateImage( cvGetSize( pObjectImage ), 8, 1 );
            IplImage* s_plane = cvCreateImage( cvGetSize( pObjectImage ), 8, 1 );
            IplImage* v_plane = cvCreateImage( cvGetSize( pObjectImage ), 8, 1 );
            IplImage* planes[] = { h_plane, s_plane };

            //convert pixel to plane
            cvCvtPixToPlane( pObjectImage, h_plane, s_plane, v_plane, 0 );

            //calculate the histogram
            cvCalcHist( planes, pHistogram, 0, 0 );

            //normalize the histogram
            cvNormalizeHist( pHistogram, 1.0 );

            //release the locally created images
            cvReleaseImage( &h_plane );
            cvReleaseImage( &s_plane );
            cvReleaseImage( &v_plane );

            if ( pRectangle != NULL )
            {
                cvReleaseImage( &pObjectImage );
            }
        }
        EXCEPTION_CATCH_AND_ABORT( "Failed to Calculate Color Histogram" );
    }
コード例 #27
0
ファイル: Histogram.cpp プロジェクト: margguo/tpf-robotica
CvHistogram * Histogram::getHShistogramFromHS(IplImage* hChannel,IplImage* sChannel){
	
	IplImage* planes[] = { hChannel, sChannel };
	
	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;

	
	hist = cvCreateHist( 2, hist_size, CV_HIST_ARRAY, ranges, 1 );
	cvCalcHist( planes, hist, 0, 0 );
	cvNormalizeHist(hist,1.0);
	
	return hist;
	
}
コード例 #28
0
void
camshift_init(const IplImage* img_template, TrackObject* obj)
{
  int size[] = {255};
  float  h_range[] = {0, 255};
  float* range[] = {h_range};
  obj->hist = cvCreateHist(1,  size, CV_HIST_ARRAY, range, 1);
  IplImage* hsv=  cvCreateImage(cvGetSize(img_template), IPL_DEPTH_8U, 3); //Size changes. No global or static
  IplImage* hue	= cvCreateImage(cvGetSize(img_template), IPL_DEPTH_8U, 1);
  cvCvtColor(img_template, hsv, CV_BGR2HSV);
  cvSplit(hsv, hue, 0, 0, 0);
  cvCalcHist(&hue, obj->hist, 0, NULL);
  cvNormalizeHist(obj->hist, 1);

  obj->track_window= cvRect(0, 0, img_template->width, img_template->height);

  cvReleaseImage(&hue);
  cvReleaseImage(&hsv);
}
コード例 #29
0
//--------------------------------------------------------------
void testApp::computeDepthHistogram (ofxCvGrayscaleImage depthImage) {
    // Compute the histogram of the depth-colored difference-from-background image.

    IplImage*  iplDepthImg = depthImage.getCvImage();
    cvCalcHist( &iplDepthImg, depthHistogram, 0, NULL );
    float *depthHistArr = cvGetHistValue_1D (depthHistogram, 0);

    int maxVal = 0;
    int startIndex = 1; // don't count black pixels.
    for (int i=startIndex; i<depthHistogramSize; i++) {
        if (depthHistArr[i] > maxVal) {
            maxVal = depthHistArr[i];
        }
    }

    for (int i=0; i<depthHistogramSize; i++) {
        depthHistogramData[i] = depthHistArr[i] / (float)maxVal;
    }
}
コード例 #30
0
ファイル: VisualTracker.C プロジェクト: ulyssesrr/carmen_lcad
// ######################################################################
void VisualTracker::setTargets(const Image<byte>& grey, const Image<byte>& target)
{
#ifdef HAVE_OPENCV
  if (itsInitTracker)
    initTracker(grey.getDims());

  IplImage* currentImg = img2ipl(grey);

  cvCalcHist( &currentImg, itsObjectHist );

  float max_val = 0.f;
  cvGetMinMaxHistValue(itsObjectHist, 0, &max_val, 0, 0 );
  cvConvertScale( itsObjectHist->bins, itsObjectHist->bins, max_val ? 255. / max_val : 0., 0 );



  itsTargetTempl = target;
#endif
}