int main()
{
#define  MAX_SEC_CNT 100
	int pitch=10;
	IplImage *src = 0;
	if((src = cvLoadImage("lena.jpg",CV_LOAD_IMAGE_GRAYSCALE))==NULL)printf("load erro\n");
	printf("src height:%d,src channel:%d,depth:%d\n",src->height,src->nChannels,src->depth);
	IplImage* src2 = cvCloneImage(src);
    formatImg(src2,pitch);
	IMG_SHOW(src2,src2);


	Section_t sec[MAX_SEC_CNT];
	memset(sec,0,sizeof(Section_t));
	FILE* logfile = fopen("log.txt","w");
	double cmp;
	CvRect rect = cvRect(0,0,src->width/10,3); 
	for (int i=0;i<MAX_SEC_CNT;i++)
	{
		
		cal_one_pen_sect(src,rect,&sec[i]);
		 cmp = cvCompareHist(sec[0].hist,sec[i].hist,CV_COMP_CHISQR);
		fprintf(logfile,"rect.y:%d,mean:%d,sdv:%d,CMP:%.2f\n",rect.y,sec[i].mean,sec[i].std_dev,cmp);
		rect.y+=pitch;
		if(rect.y>src->height)break;
	}

	fclose(logfile);
	
	

		cvWaitKey();
	//getchar();
	return 0;
}
Exemplo n.º 2
0
/*
 * @src Frame image
 * @contour  contour processing
 * @testImageHistogram histogram from model image
 * @h_bins number of hue bins
 * @s_bins number of sat bins 
 * @min minimum similarity to achieve when comparing histograms
 */
int Contours::histogramMatchingFilter(IplImage * src, CvHistogram * testImageHistogram,int h_bins,int s_bins, double min){
	CvRect box;
	CvMemStorage* mem = cvCreateMemStorage(0);
	
	double val;
	
	
	//get contour bounding box
	box=cvBoundingRect(this->c,0);
	
	//printf("box x:%d y:%d \n",box.x,box.y);
	
	IplImage * src_bbox=cvCreateImage(cvSize(box.width,box.height),src->depth,src->nChannels);
	
	//gets subimage bounded by box
    cvGetSubArr( src,(CvMat*)src_bbox, box );

	//gets subimage histogram
	utils::Histogram * h = new Histogram(h_bins,s_bins);
	CvHistogram* hist = h->getHShistogramFromRGB(src_bbox);
	//compares with object histogram
	val=cvCompareHist(hist,testImageHistogram,CV_COMP_BHATTACHARYYA);
	
	cvReleaseHist(&hist);
	cvReleaseImage(&src_bbox);
	cvReleaseMemStorage(&mem);
	delete h;
	
	return (val<min);
}
Exemplo n.º 3
0
void CV_CompareHistTest::run_func(void)
{
    int k;
    if( hist_type != CV_HIST_ARRAY && test_cpp )
    {
        cv::SparseMat h0((CvSparseMat*)hist[0]->bins);
        cv::SparseMat h1((CvSparseMat*)hist[1]->bins);
        for( k = 0; k < MAX_METHOD; k++ )
            result[k] = cv::compareHist(h0, h1, k);
    }
    else
        for( k = 0; k < MAX_METHOD; k++ )
            result[k] = cvCompareHist( hist[0], hist[1], k );
}
Exemplo n.º 4
0
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;
}
Exemplo n.º 5
0
static void
cvTsCalcBackProjectPatch( IplImage** images, IplImage* dst, CvSize patch_size,
                          CvHistogram* hist, int method,
                          double factor, int* channels )
{
    CvHistogram* model = 0;
    
    IplImage imgstub[CV_MAX_DIM], *img[CV_MAX_DIM];
    IplROI roi;
    int i, dims;
    int x, y;
    CvSize size = cvGetSize(dst);

    dims = cvGetDims( hist->bins );
    cvCopyHist( hist, &model );
    cvNormalizeHist( hist, factor );
    cvZero( dst );

    for( i = 0; i < dims; i++ )
    {
        CvMat stub, *mat;
        mat = cvGetMat( images[i], &stub, 0, 0 );
        img[i] = cvGetImage( mat, &imgstub[i] );
        img[i]->roi = &roi;
    }

    roi.coi = 0;

    for( y = 0; y < size.height; y++ )
    {
        for( x = 0; x < size.width; x++ )
        {
            double result;
            
            roi.xOffset = x;
            roi.yOffset = y;
            roi.width = patch_size.width;
            roi.height = patch_size.height;

            cvTsCalcHist( img, model, 0, channels );
            cvNormalizeHist( model, factor );
            result = cvCompareHist( model, hist, method );
            CV_IMAGE_ELEM( dst, float, y, x ) = (float)result;
        }
    }

    cvReleaseHist( &model );
}
Exemplo n.º 6
0
int CV_CalcHistTest::validate_test_results( int /*test_case_idx*/ )
{
    int code = CvTS::OK;
    double diff;
    cvTsCalcHist( images, hist[1], images[CV_MAX_DIM], channels );
    diff = cvCompareHist( hist[0], hist[1], CV_COMP_CHISQR );
    if( diff > DBL_EPSILON )
    {
        ts->printf( CvTS::LOG, "The histogram does not match to the reference one\n" );
        code = CvTS::FAIL_BAD_ACCURACY;
        
    }

    if( code < 0 )
        ts->set_failed_test_info( code );
    return code;
}
static GstFlowReturn
kms_pointer_detector_transform_frame_ip (GstVideoFilter * filter,
    GstVideoFrame * frame)
{
  KmsPointerDetector *pointerdetector = KMS_POINTER_DETECTOR (filter);
  GstMapInfo info;
  double min_Bhattacharyya = 1.0, bhattacharyya = 1, bhattacharyya2 =
      1, bhattacharyya3 = 1;
  int i = 0;

  pointerdetector->frameSize = cvSize (frame->info.width, frame->info.height);
  kms_pointer_detector_initialize_images (pointerdetector, frame);

  gst_buffer_map (frame->buffer, &info, GST_MAP_READ);
  pointerdetector->cvImage->imageData = (char *) info.data;

  if ((pointerdetector->iteration > FRAMES_TO_RESET)
      && (pointerdetector->state != CAPTURING_SECOND_HIST)) {
    get_histogram (pointerdetector->cvImage, pointerdetector->upCornerRect1,
        pointerdetector->trackinRectSize, pointerdetector->histSetUpRef);
    pointerdetector->histRefCapturesCounter = 0;
    pointerdetector->secondHistCapturesCounter = 0;
    pointerdetector->state = CAPTURING_REF_HIST;
    pointerdetector->colorRect1 = WHITE;
    pointerdetector->colorRect2 = WHITE;
    pointerdetector->iteration = 6;
  }
  if (pointerdetector->iteration == 5) {
    get_histogram (pointerdetector->cvImage, pointerdetector->upCornerRect1,
        pointerdetector->trackinRectSize, pointerdetector->histSetUpRef);
    pointerdetector->state = CAPTURING_REF_HIST;
    goto end;
  }

  if (pointerdetector->iteration < 6)
    goto end;

  get_histogram (pointerdetector->cvImage, pointerdetector->upCornerRect1,
      pointerdetector->trackinRectSize, pointerdetector->histSetUp1);
  bhattacharyya2 =
      cvCompareHist (pointerdetector->histSetUp1,
      pointerdetector->histSetUpRef, CV_COMP_BHATTACHARYYA);
  if ((bhattacharyya2 >= COMPARE_THRESH_SECOND_HIST)
      && (pointerdetector->state == CAPTURING_REF_HIST)) {
    pointerdetector->histRefCapturesCounter++;
    if (pointerdetector->histRefCapturesCounter > 20) {
      pointerdetector->histRefCapturesCounter = 0;
      pointerdetector->colorRect1 = CV_RGB (0, 255, 0);
      pointerdetector->state = CAPTURING_SECOND_HIST;
    }
  }
  if (pointerdetector->state == CAPTURING_SECOND_HIST) {
    get_histogram (pointerdetector->cvImage, pointerdetector->upCornerRect2,
        pointerdetector->trackinRectSize, pointerdetector->histSetUp2);
    bhattacharyya3 =
        cvCompareHist (pointerdetector->histSetUp1,
        pointerdetector->histSetUp2, CV_COMP_BHATTACHARYYA);
    if (bhattacharyya3 < COMPARE_THRESH_2_RECT) {
      pointerdetector->secondHistCapturesCounter++;
      if (pointerdetector->secondHistCapturesCounter > 15) {
        pointerdetector->secondHistCapturesCounter = 0;
        pointerdetector->state = BOTH_HIST_SIMILAR;
        pointerdetector->colorRect2 = CV_RGB (0, 255, 0);
        cvCopyHist (pointerdetector->histSetUp2, &pointerdetector->histModel);
        pointerdetector->upCornerFinalRect.x = 10;
        pointerdetector->upCornerFinalRect.y = 10;
        pointerdetector->histRefCapturesCounter = 0;
        pointerdetector->secondHistCapturesCounter = 0;
      }
    }
  }
  for (i = 0; i < pointerdetector->numOfRegions; i++) {
    int horizOffset =
        pointerdetector->upCornerFinalRect.x +
        pointerdetector->windowScale * (rand () %
        pointerdetector->trackinRectSize.width -
        pointerdetector->trackinRectSize.width / 2);
    int vertOffset =
        pointerdetector->upCornerFinalRect.y +
        pointerdetector->windowScale * (rand () %
        pointerdetector->trackinRectSize.height -
        pointerdetector->trackinRectSize.height / 2);
    pointerdetector->trackingPoint1Aux.x = horizOffset;
    pointerdetector->trackingPoint1Aux.y = vertOffset;
    pointerdetector->trackingPoint2Aux.x =
        horizOffset + pointerdetector->trackinRectSize.width;
    pointerdetector->trackingPoint2Aux.y =
        vertOffset + pointerdetector->trackinRectSize.height;
    if ((horizOffset > 0)
        && (pointerdetector->trackingPoint2Aux.x <
            pointerdetector->cvImage->width)
        && (vertOffset > 0)
        && (pointerdetector->trackingPoint2Aux.y <
            pointerdetector->cvImage->height)) {
      if (pointerdetector->show_debug_info)
        cvRectangle (pointerdetector->cvImage,
            pointerdetector->trackingPoint1Aux,
            pointerdetector->trackingPoint2Aux, CV_RGB (0, 255, 0), 1, 8, 0);
      cvSetImageROI (pointerdetector->cvImage,
          cvRect (pointerdetector->trackingPoint1Aux.x,
              pointerdetector->trackingPoint1Aux.y,
              pointerdetector->trackinRectSize.width,
              pointerdetector->trackinRectSize.height));
      cvCopy (pointerdetector->cvImage, pointerdetector->cvImageAux1, 0);
      cvResetImageROI (pointerdetector->cvImage);
      calc_histogram (pointerdetector->cvImageAux1,
          pointerdetector->histCompare);
      bhattacharyya =
          cvCompareHist (pointerdetector->histModel,
          pointerdetector->histCompare, CV_COMP_BHATTACHARYYA);
      if ((bhattacharyya < min_Bhattacharyya)
          && (bhattacharyya < COMPARE_THRESH_HIST_REF)) {
        min_Bhattacharyya = bhattacharyya;
        pointerdetector->trackingPoint1 = pointerdetector->trackingPoint1Aux;
        pointerdetector->trackingPoint2 = pointerdetector->trackingPoint2Aux;
      }
    }
  }
  cvRectangle (pointerdetector->cvImage, pointerdetector->upCornerRect1,
      pointerdetector->downCornerRect1, pointerdetector->colorRect1, 1, 8, 0);
  cvRectangle (pointerdetector->cvImage, pointerdetector->upCornerRect2,
      pointerdetector->downCornerRect2, pointerdetector->colorRect2, 1, 8, 0);
  if (min_Bhattacharyya < 0.95) {
    pointerdetector->windowScale = pointerdetector->windowScaleRef;
  } else {
    pointerdetector->windowScale = pointerdetector->cvImage->width / 8;
  }
  CvPoint finalPointerPositionAux;

  finalPointerPositionAux.x = pointerdetector->upCornerFinalRect.x +
      pointerdetector->trackinRectSize.width / 2;
  finalPointerPositionAux.y = pointerdetector->upCornerFinalRect.y +
      pointerdetector->trackinRectSize.height / 2;
  if (abs (pointerdetector->finalPointerPosition.x -
          finalPointerPositionAux.x) < 55 ||
      abs (pointerdetector->finalPointerPosition.y -
          finalPointerPositionAux.y) < 55) {
    finalPointerPositionAux.x =
        (finalPointerPositionAux.x +
        pointerdetector->finalPointerPosition.x) / 2;
    finalPointerPositionAux.y =
        (finalPointerPositionAux.y +
        pointerdetector->finalPointerPosition.y) / 2;
  }
  pointerdetector->upCornerFinalRect = pointerdetector->trackingPoint1;
  pointerdetector->downCornerFinalRect = pointerdetector->trackingPoint2;

  pointerdetector->finalPointerPosition.x = finalPointerPositionAux.x;
  pointerdetector->finalPointerPosition.y = finalPointerPositionAux.y;

  cvCircle (pointerdetector->cvImage, pointerdetector->finalPointerPosition,
      10.0, WHITE, -1, 8, 0);

  kms_pointer_detector_check_pointer_position (pointerdetector);

end:
  pointerdetector->iteration++;
  gst_buffer_unmap (frame->buffer, &info);
  return GST_FLOW_OK;
}
bool FeatureHistogram::eval(CvHistogram* hist, Rect ROI, float* result)
{
    *result = 0.0f;
    Point2D offset;
    offset = ROI;

    // define the minimum size
    Size minSize = Size(3,3);

    // printf("in eval %d = %d\n",curSize.width,ROI.width );

    if ( m_curSize.width != ROI.width || m_curSize.height != ROI.height )
    {
        m_curSize = ROI;
        if (!(m_initSize==m_curSize))
        {
            m_scaleFactorHeight = (float)m_curSize.height/m_initSize.height;
            m_scaleFactorWidth = (float)m_curSize.width/m_initSize.width;

            for (int curArea = 0; curArea < m_numAreas; curArea++)
            {
                m_scaleAreas[curArea].height = (int)floor((float)m_areas[curArea].height*m_scaleFactorHeight+0.5);
                m_scaleAreas[curArea].width = (int)floor((float)m_areas[curArea].width*m_scaleFactorWidth+0.5);

                if (m_scaleAreas[curArea].height < minSize.height || m_scaleAreas[curArea].width < minSize.width) {
                    m_scaleFactorWidth = 0.0f;
                    return false;
                }

                m_scaleAreas[curArea].left = (int)floor( (float)m_areas[curArea].left*m_scaleFactorWidth+0.5);
                m_scaleAreas[curArea].upper = (int)floor( (float)m_areas[curArea].upper*m_scaleFactorHeight+0.5);
                m_scaleWeights[curArea] = (float)m_weights[curArea] /
                                          (float)((m_scaleAreas[curArea].width)*(m_scaleAreas[curArea].height));
            }
        }
        else
        {
            m_scaleFactorWidth = m_scaleFactorHeight = 1.0f;
            for (int curArea = 0; curArea<m_numAreas; curArea++) {
                m_scaleAreas[curArea] = m_areas[curArea];
                m_scaleWeights[curArea] = (float)m_weights[curArea] /
                                          (float)((m_areas[curArea].width)*(m_areas[curArea].height));
            }
        }
    }

    if ( m_scaleFactorWidth == 0.0f )
        return false;

    for (int curArea = 0; curArea < m_numAreas; curArea++)
    {
        ///////////////
        if(initflag)//cap nhat lan dau
        {
            if(!hist)
            {
                *result  += -1.0f;
                initflag=false;
                return false;
            }
            float compute=(cvCompareHist(ref_histos,hist,CV_COMP_CORREL)-0.85)*255;

            //if(compute>maxcompute)maxcompute=compute;
            //if(compute<mincompute)mincompute=compute;
            //printf("\nmax compute:%f,min compute:%f",maxcompute,mincompute);
            float k=(compute)*m_scaleWeights[curArea];
            *result +=k;
            //if(k>maxcomputeresult)maxcomputeresult=k;
            //if(k<mincomputeresult)mincomputeresult=k;
            //printf("\nmax computerresult:%f,min computerresult:%f",maxcomputeresult,mincomputeresult);
        }
        else //lan dau la cap nhat hsv
        {
            if(!hist)
            {
                *result  += -1.0f;
                return false;
            }
            initflag=true;
            //khoi tao lai
            //*result +=m_scaleWeights[curArea]; //ket qua cao nhat
            ref_histos=copyhistogram3(hist);
            float thresh=((rand() % 10000)*0.2f)/10000;
            cvThreshHist(ref_histos,thresh);
            float compute=(cvCompareHist(ref_histos,hist,CV_COMP_CORREL)-0.85)*255;
            float k=(compute)*m_scaleWeights[curArea];
            *result +=k;
        }
        //////////////
        //float k=(float)image->getSum( m_scaleAreas[curArea]+offset )*
        //m_scaleWeights[curArea];
        //*result += k;
        //if(k>maxcomputegetsum)maxcomputegetsum=k;
        //if(k<mincomputegetsum)mincomputegetsum=k;
        //printf("\nmax computerget sum:%f,min computeget sum:%f",maxcomputegetsum,mincomputegetsum);
        //////////////
        //old function
        /*
        if(initflag)//cap nhat lan dau
        {
        	if(!image)
        	{
        		*result = -1.0f;
        		initflag=false;
        		return false;
        	}
        	*result=cvCompareHist(ref_histos,image,CV_COMP_CORREL)*m_scaleWeights[curArea]*1000;
        }
        else //lan dau la cap nhat hsv
        {
        	if(!image)
        	{
        		*result = -1.0f;
        		return false;
        	}
        	initflag=true;
        	//khoi tao lai
        	*result = m_scaleWeights[curArea]*1000; //ket qua cao nhat
        	ref_histos=copyhistogram(image);
        	float thresh=((rand() % 10000)*0.2f)/10000;
        	cvThreshHist(ref_histos,thresh);
        }
        //	*result += (float)image->getSum( m_scaleAreas[curArea]+offset )*
        //		m_scaleWeights[curArea];

        */
    }
    /*
    if (image->getUseVariance())
    {
    	float variance = (float) image->getVariance(ROI);
    	*result /=  variance;
    }
    */
    m_response = *result;

    return true;
}