Example #1
0
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 );
}
int cam() //calling main
{
    int hdims = 16;
    printf("I am main");
    CvCapture* capture = cvCreateCameraCapture(1); //determining usb camera
    CvHistogram *hist = 0;
    CvMemStorage* g_storage = NULL;
    Display *display=construct_display();
    int x,y, tmpx=0, tmpy=0, chk=0;
    IplImage* image=0;
    IplImage* lastimage1=0;
    IplImage* lastimage=0;
    IplImage* diffimage;
    IplImage* bitimage;
    IplImage* src=0,*hsv=0,*hue=0,*backproject=0;
    IplImage* hsv1=0,*hue1=0,*histimg=0,*frame=0,*edge=0;
    float* hranges;
    cvNamedWindow( "CA", CV_WINDOW_AUTOSIZE ); //display window 3
    //Calculation of Histogram//
    cvReleaseImage(&src);
    src= cvLoadImage("images/skin.jpg"); //taking patch
    while(1)
    {
        frame = cvQueryFrame( capture ); //taking frame by frame for image prcessing
        int j=0;
        float avgx=0;
        float avgy=0;
        if( !frame ) break;
        //#########################Background Substraction#########################//
        if(!image)
        {
            image=cvCreateImage(cvSize(frame->width,frame->height),frame->depth,1);
            bitimage=cvCreateImage(cvSize(frame->width,frame->height),frame->depth,1);
            diffimage=cvCreateImage(cvSize(frame->width,frame->height),frame->depth,1);
            lastimage=cvCreateImage(cvSize(frame->width,frame->height),frame->depth,1);
        }
        cvCvtColor(frame,image,CV_BGR2GRAY);
        if(!lastimage1)
        {
            lastimage1=cvLoadImage("images/img.jpg");
        }
        cvCvtColor(lastimage1,lastimage,CV_BGR2GRAY);
        cvAbsDiff(image,lastimage,diffimage);
        cvThreshold(diffimage,bitimage,65,225,CV_THRESH_BINARY);
        cvInRangeS(bitimage,cvScalar(0),cvScalar(30),bitimage);
        cvSet(frame,cvScalar(0,0,0),bitimage);
        cvReleaseImage(&hsv);
        hsv= cvCreateImage( cvGetSize(src), 8, 3 );
        cvReleaseImage(&hue);
        hue= cvCreateImage( cvGetSize(src), 8, 1);
        cvCvtColor(src,hsv,CV_BGR2HSV);
        cvSplit(hsv,hue,0,0,0);
        float hranges_arr[] = {0,180};
        hranges = hranges_arr;
        hist = cvCreateHist( 1, &hdims, CV_HIST_ARRAY, &hranges, 1 );
        cvCalcHist(&hue, hist, 0, 0 );
        cvThreshHist( hist, 100 );
        //#############################Display histogram##############################//
        cvReleaseImage(&histimg);
        histimg = cvCreateImage( cvSize(320,200), 8, 3 );
        cvZero( histimg );
        int bin_w = histimg->width / hdims;
        //#### Calculating the Probablity of Finding the skin with in-built method ###//
        if(0)
        {
            free (backproject);
            free (hsv1);
            free (hue1);
        }
        cvReleaseImage(&backproject);
        backproject= cvCreateImage( cvGetSize(frame), 8, 1 );
        cvReleaseImage(&hsv1);
        hsv1 = cvCreateImage( cvGetSize(frame), 8, 3);
        cvReleaseImage(&hue1);
        hue1 = cvCreateImage( cvGetSize(frame), 8, 1);
        cvCvtColor(frame,hsv1,CV_BGR2HSV);
        cvSplit(hsv1,hue1,0,0,0);
        cvCalcBackProject( &hue1, backproject, hist );
        cvSmooth(backproject,backproject,CV_GAUSSIAN);
        cvSmooth(backproject,backproject,CV_MEDIAN);
        if( g_storage == NULL )
        g_storage = cvCreateMemStorage(0);
        else
        cvClearMemStorage( g_storage );
        CvSeq* contours=0;
        CvSeq* result =0;
        cvFindContours(backproject, g_storage, &contours );
        if(contours)
        {
            result=cvApproxPoly(contours, sizeof(CvContour), g_storage,
            CV_POLY_APPROX_DP, 7, 1);
        }
        cvZero( backproject);
        for( ; result != 0; result = result->h_next )
        {
            double area = cvContourArea( result );
            cvDrawContours( backproject,result, CV_RGB(255,255, 255), CV_RGB(255,0, 255)
            , -1,CV_FILLED, 8 );
            for( int i=1; i<=result-> total; i++ )
            {
                if(i>=1 and abs(area)>300)
                {
                    CvPoint* p2 = CV_GET_SEQ_ELEM( CvPoint, result, i );
                    if(1)
                    {
                        avgx=avgx+p2->x;
                        avgy=avgy+p2->y;
                        j=j+1;
                        cvCircle(backproject,cvPoint(p2->x,p2->y ),10,
                        cvScalar(255,255,255));
                    }
                }
            }
        }
        cvCircle( backproject, cvPoint(avgx/j, avgy/j ), 40, cvScalar(255,255,255) );
        x = ( avgx/j );
        y = ( avgy/j );
        x=( (x*1240)/640 )-20;
        y=( (y*840)/480 )-20;
        if ( (abs(tmpx-x)>6 or abs(tmpy-y)>6 ) and j )
        {
            tmpx = x;
            tmpy = y;
            chk=0;
        }
        else chk++;
        mouse_move1( tmpx, tmpy, display );
        if ( chk==10 )
        {
            mouse_click( 5, 2, display );
            mouse_click( 5, 3, display );
        }
        cvSaveImage( "final.jpg", frame );
        cvSaveImage( "final1.jpg", backproject );
        cvShowImage( "CA", backproject );
        char c = cvWaitKey(33);
        if( c == 27 )
        break; //function break and destroying windows if press <escape> key
    }
    cvReleaseCapture( &capture );
    cvDestroyWindow( "CA" );
}
Example #3
0
void CV_ThreshHistTest::run_func(void)
{
    cvThreshHist( hist[0], threshold );
}
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;
}