Пример #1
0
main( int argc, char* argv[] ) {

    // Choose a negative floating point number.  Take its absolute value,
    // round it, and then take its ceiling and floor.
    double a = -1.23;
    printf( "CV_IABS(a) = %d\n", CV_IABS(a) );
    printf( "cvRound(a) = %d\n", cvRound(a) );
    printf( "cvCeil(a) = %d\n", cvCeil(a) );
    printf( "cvFloor(a) = %d\n", cvFloor(a) );


    // Generate some random numbers.
    CvRNG rngState = cvRNG(-1);
    for (int i = 0; i < 10; i++) {
        printf( "%u %f\n", cvRandInt( &rngState ),
                           cvRandReal( &rngState ) );
    }

    // Create a floating point CvPoint2D32f and convert it to an integer
    // CvPoint.
    CvPoint2D32f point_float1 = cvPoint2D32f(1.0, 2.0);
    CvPoint point_int1 = cvPointFrom32f( point_float1 );

    // Convert a CvPoint to a CvPoint2D32f.
    CvPoint point_int2 = cvPoint(3, 4);
    CvPoint2D32f point_float2 = cvPointTo32f( point_int2 );

}
Пример #2
0
const CvRect &ProcessSelection()
{
	currentPos.x = GetMouseX();
	currentPos.y = GetMouseY();
	if (IsLMouseDown()) {
		if (!isSelectionStarted) {
			//start picking
			origin = currentPos;
			selection.x = origin.x;
			selection.y = origin.y;
			selection.width = origin.x;
			selection.height = origin.y;

			printf("staret picking\n");
		}
		isSelectionStarted = true;
		selection.x = MIN(currentPos.x,origin.x);
		selection.y = MIN(currentPos.y,origin.y);
		selection.width = selection.x + CV_IABS(currentPos.x - origin.x);
		selection.height = selection.y + CV_IABS(currentPos.y - origin.y);

		selection.x = MAX( selection.x, 0 );
		selection.y = MAX( selection.y, 0 );
		selection.width = MIN( selection.width, 640);
		selection.height = MIN( selection.height, 480 );
		selection.width -= selection.x;
		selection.height -= selection.y;

	} else {
		if (isSelectionStarted) {
			printf("end picking\n");
			//end picking
			isSelectionStarted = false;
			hasSelection = true;
			/*selection.width = selection.x - currentPos.x;
			selection.height =selection.y -  currentPos.y;*/

		}
	}
	return selection;
}
Пример #3
0
void Kinect::on_mouse(int event, int x, int y, int flags, void* param)
{
	if (!KinectColorImg)
		return;
	if (KinectColorImg->origin)
	{
		y = KinectColorImg->height - y;
	}
	//마우스 클릭 후 포인터 이동이 있을 때
	if (select_object)
	{
		selection.x = MIN(x, origin.x);
		selection.y = MIN(y, origin.y);
		selection.width = selection.x + CV_IABS(x - origin.x);
		selection.height = selection.y + CV_IABS(y - origin.y);

		selection.width = MIN(selection.width, KinectColorImg->width);
		selection.height = MIN(selection.height, KinectColorImg->height);
		selection.width -= selection.x;
		selection.height -= selection.y;
	}
	switch (event)
	{
		//마우스 왼쪽 버튼 클릭
	case CV_EVENT_LBUTTONDOWN:
		origin = cvPoint(x, y);
		selection = cvRect(x, y, 0, 0);
		select_object = 1;
		break;
		//마우스 왼쪽버튼 클릭후 release
	case CV_EVENT_LBUTTONUP:
		select_object = 0;
		//ROI 지정되었을때
		if (selection.width > 0 && selection.height > 0)
		{
			trackObject = -1;
		}
		break;
	}
}
Пример #4
0
void on_mouse(int event, int x, int y, int flags, void *param) {
	Tracker *t = (Tracker *)param;
	
	if (!t->img)
		return;
	
	if(t->img->origin)
		y = t->img->height - y;
	
	if(t->select_object) {
		t->selection.x = MIN(x, t->origin.x);
		t->selection.y = MIN(y, t->origin.y);
		t->selection.width = t->selection.x + CV_IABS(x - t->origin.x);
		t->selection.height = t->selection.y + CV_IABS(y - t->origin.y);
		
		t->selection.x = MAX(t->selection.x, 0);
		t->selection.y = MAX(t->selection.y, 0);
		t->selection.width = MIN(t->selection.width, t->img->width);
		t->selection.height = MIN(t->selection.height, t->img->height);
		t->selection.width -= t->selection.x;
		t->selection.height -= t->selection.y;
	}
	
	switch (event) {
		case CV_EVENT_LBUTTONDOWN:
			t->origin = cvPoint(x, y);
			t->selection = cvRect(x, y, 0, 0);
			t->select_object = 1;
			break;
		case CV_EVENT_LBUTTONUP:
			t->select_object = 0;
			if (t->selection.width > 0 && t->selection.height > 0) {
				t->track_object = -1;
			}
			break;
			
		default:
			break;
	}
}
Пример #5
0
//=========================================
void setFaceCoords(int type, int x, int y, camshift_kalman_tracker& camKalTrk) {
//=========================================
	if (!camKalTrk.image)
		return;

	if (camKalTrk.image->origin)
		y = camKalTrk.image->height - y;

	if (camKalTrk.selectObject) {
		camKalTrk.selection.x = MIN(x,camKalTrk.origin.x);
		camKalTrk.selection.y = MIN(y,camKalTrk.origin.y);
		camKalTrk.selection.width = camKalTrk.selection.x + CV_IABS(x - camKalTrk.origin.x);
		camKalTrk.selection.height = camKalTrk.selection.y + CV_IABS(y - camKalTrk.origin.y);

		camKalTrk.selection.x = MAX( camKalTrk.selection.x, 0 );
		camKalTrk.selection.y = MAX( camKalTrk.selection.y, 0 );
		camKalTrk.selection.width = MIN( camKalTrk.selection.width, camKalTrk.image->width );
		camKalTrk.selection.height = MIN( camKalTrk.selection.height, camKalTrk.image->height );
		camKalTrk.selection.width -= camKalTrk.selection.x;
		camKalTrk.selection.height -= camKalTrk.selection.y;
	}

	switch (type) {
	case 0:
		camKalTrk.origin = cvPoint(x, y);
		camKalTrk.selection = cvRect(x, y, 0, 0);
		camKalTrk.selectObject = 1;
		break;
	case 1:
		camKalTrk.selectObject = 0;
		if (camKalTrk.selection.width > 0 && camKalTrk.selection.height > 0)
			camKalTrk.trackObject = -1;
		camKalTrk.originBox = camKalTrk.selection;

		break;
	}
}
void AdaptiveHistogramCamshift::OnMouse(int event, int x, int y, int /*flags*/, void* param)
{
  // Update rect while mouse is down.
  if (g_selectObject)
  {
    g_selRect.x = MIN(x,g_selOrigin.x);
    g_selRect.y = MIN(y,g_selOrigin.y);
    g_selRect.width = g_selRect.x + CV_IABS(x - g_selOrigin.x);
    g_selRect.height = g_selRect.y + CV_IABS(y - g_selOrigin.y);

    g_selRect.x = MAX( g_selRect.x, 0 );
    g_selRect.y = MAX( g_selRect.y, 0 );
    g_selRect.width -= g_selRect.x;
    g_selRect.height -= g_selRect.y;
  }

  // Store or update rect.
  switch (event)
  {
    case CV_EVENT_LBUTTONDOWN:
      // Set which window is doing the selection.
      g_selId = *static_cast<int*>(param);
      // Init selection rect.
      g_selRect = cvRect(x, y, 0, 0);
      g_selOrigin = cvPoint(x, y);
      g_selectObject = true;
      break;
    case CV_EVENT_LBUTTONUP:
      // Set flag for initialization.
      g_selectObject = false;
      if ((g_selRect.width > 0) && (g_selRect.height > 0))
      {
        g_initTracking = true;
      }
      break;
  }
}
Пример #7
0
void on_mouse( int event, int x, int y, int flags, void* param )
{
    if( !image )
        return;

    if( image->origin )
        y = image->height - y;

    if( select_object )
    {
        selection.x = MIN(x,origin.x);
        selection.y = MIN(y,origin.y);
        selection.width = selection.x + CV_IABS(x - origin.x);
        selection.height = selection.y + CV_IABS(y - origin.y);

        selection.x = MAX( selection.x, 0 );
        selection.y = MAX( selection.y, 0 );
        selection.width = MIN( selection.width, image->width );
        selection.height = MIN( selection.height, image->height );
        selection.width -= selection.x;
        selection.height -= selection.y;
    }

    switch( event )
    {
    case CV_EVENT_LBUTTONDOWN:
        origin = cvPoint(x,y);
        selection = cvRect(x,y,0,0);
        select_object = 1;
        break;
    case CV_EVENT_LBUTTONUP:
        select_object = 0;
        if( selection.width > 0 && selection.height > 0 )
            track_object = -1;
        break;
    }
}
Пример #8
0
void ChromacityShadRem::extractShadows(const cv::Mat& hsvFrame, const ConnCompGroup& darkPixels,
		const cv::Mat& hsvBg, ConnCompGroup& shadows) {
	cv::Mat mask(hsvFrame.size(), CV_8U, cv::Scalar(0));

	for (int cc = 0; cc < (int) darkPixels.comps.size(); ++cc) {
		const ConnComp& object = darkPixels.comps[cc];

		for (int p = 0; p < (int) object.pixels.size(); ++p) {
			int x = object.pixels[p].x;
			int y = object.pixels[p].y;

			int hDiffSum = 0;
			int sDiffSum = 0;
			int winArea = 0;
			int minY = std::max(y - params.winSize, 0);
			int maxY = std::min(y + params.winSize, hsvFrame.rows - 1);
			int minX = std::max(x - params.winSize, 0);
			int maxX = std::min(x + params.winSize, hsvFrame.cols - 1);
			for (int i = minY; i <= maxY; ++i) {
				const uchar* hsvFramePtr = hsvFrame.ptr(i);
				const uchar* hsvBgPtr = hsvBg.ptr(i);

				for (int j = minX; j <= maxX; ++j) {
					int hDiff = CV_IABS(hsvFramePtr[j * 3] - hsvBgPtr[j * 3]);
					if (hDiff > 90) {
						hDiff = 180 - hDiff;
					}
					hDiffSum += hDiff;

					int sDiff = hsvFramePtr[j * 3 + 1] - hsvBgPtr[j * 3 + 1];
					sDiffSum += sDiff;

					++winArea;
				}
			}

			bool hIsShadow = (hDiffSum / winArea < params.hThresh);
			bool sIsShadow = (sDiffSum / winArea < params.sThresh);

			if (hIsShadow && sIsShadow) {
				uchar* maskPtr = mask.ptr(y);
				maskPtr[x] = 255;
			}
		}
	}

	shadows.update(mask);
}
Пример #9
0
/*F///////////////////////////////////////////////////////////////////////////////////////
//    Name: icvCalcPGH
//    Purpose:
//      Calculates PGH(pairwise geometric histogram) for contour given.
//    Context:
//    Parameters:
//      contour  - pointer to input contour object.
//      pgh      - output histogram
//      ang_dim  - number of angle bins (vertical size of histogram)
//      dist_dim - number of distance bins (horizontal size of histogram)
//    Returns:
//      CV_OK or error code
//    Notes:
//F*/
static CvStatus
icvCalcPGH( const CvSeq * contour, float *pgh, int angle_dim, int dist_dim )
{
    char local_buffer[(1 << 14) + 32];
    float *local_buffer_ptr = (float *)cvAlignPtr(local_buffer,32);
    float *buffer = local_buffer_ptr;
    double angle_scale = (angle_dim - 0.51) / icv_acos_table[0];
    double dist_scale = DBL_EPSILON;
    int buffer_size;
    int i, count, pass;
    int *pghi = (int *) pgh;
    int hist_size = angle_dim * dist_dim;
    CvSeqReader reader1, reader2;       /* external and internal readers */

    if( !contour || !pgh )
        return CV_NULLPTR_ERR;

    if( angle_dim <= 0 || angle_dim > 180 || dist_dim <= 0 )
        return CV_BADRANGE_ERR;

    if( !CV_IS_SEQ_POINT_SET( contour ))
        return CV_BADFLAG_ERR;

    memset( pgh, 0, hist_size * sizeof( pgh[0] ));

    count = contour->total;

    /* allocate buffer for distances */
    buffer_size = count * sizeof( float );

    if( buffer_size > (int)sizeof(local_buffer) - 32 )
    {
        buffer = (float *) cvAlloc( buffer_size );
        if( !buffer )
            return CV_OUTOFMEM_ERR;
    }

    cvStartReadSeq( contour, &reader1, 0 );
    cvStartReadSeq( contour, &reader2, 0 );

    /* calc & store squared edge lengths, calculate maximal distance between edges */
    for( i = 0; i < count; i++ )
    {
        CvPoint pt1, pt2;
        double dx, dy;

        CV_READ_EDGE( pt1, pt2, reader1 );

        dx = pt2.x - pt1.x;
        dy = pt2.y - pt1.y;
        buffer[i] = (float)(1./sqrt(dx * dx + dy * dy));
    }

    /* 
       do 2 passes. 
       First calculates maximal distance.
       Second calculates histogram itself.
     */
    for( pass = 1; pass <= 2; pass++ )
    {
        double dist_coeff = 0, angle_coeff = 0;

        /* run external loop */
        for( i = 0; i < count; i++ )
        {
            CvPoint pt1, pt2;
            int dx, dy;
            int dist = 0;

            CV_READ_EDGE( pt1, pt2, reader1 );

            dx = pt2.x - pt1.x;
            dy = pt2.y - pt1.y;

            if( (dx | dy) != 0 )
            {
                int j;

                if( pass == 2 )
                {
                    dist_coeff = buffer[i] * dist_scale;
                    angle_coeff = buffer[i] * (_CV_ACOS_TABLE_SIZE / 2);
                }

                /* run internal loop (for current edge) */
                for( j = 0; j < count; j++ )
                {
                    CvPoint pt3, pt4;

                    CV_READ_EDGE( pt3, pt4, reader2 );

                    if( i != j )        /* process edge pair */
                    {
                        int d1 = (pt3.y - pt1.y) * dx - (pt3.x - pt1.x) * dy;
                        int d2 = (pt4.y - pt1.y) * dx - (pt2.x - pt1.x) * dy;
                        int cross_flag;
                        int *hist_row = 0;

                        if( pass == 2 )
                        {
                            int dp = (pt4.x - pt3.x) * dx + (pt4.y - pt3.y) * dy;

                            dp = cvRound( dp * angle_coeff * buffer[j] ) +
                                (_CV_ACOS_TABLE_SIZE / 2);
                            dp = MAX( dp, 0 );
                            dp = MIN( dp, _CV_ACOS_TABLE_SIZE - 1 );
                            hist_row = pghi + dist_dim *
                                cvRound( icv_acos_table[dp] * angle_scale );

                            d1 = cvRound( d1 * dist_coeff );
                            d2 = cvRound( d2 * dist_coeff );
                        }

                        cross_flag = (d1 ^ d2) < 0;

                        d1 = CV_IABS( d1 );
                        d2 = CV_IABS( d2 );

                        if( pass == 2 )
                        {
                            if( d1 >= dist_dim )
                                d1 = dist_dim - 1;
                            if( d2 >= dist_dim )
                                d2 = dist_dim - 1;

                            if( !cross_flag )
                            {
                                if( d1 > d2 )   /* make d1 <= d2 */
                                {
                                    d1 ^= d2;
                                    d2 ^= d1;
                                    d1 ^= d2;
                                }

                                for( ; d1 <= d2; d1++ )
                                    hist_row[d1]++;
                            }
                            else
                            {
                                for( ; d1 >= 0; d1-- )
                                    hist_row[d1]++;
                                for( ; d2 >= 0; d2-- )
                                    hist_row[d2]++;
                            }
                        }
                        else    /* 1st pass */
                        {
                            d1 = CV_IMAX( d1, d2 );
                            dist = CV_IMAX( dist, d1 );
                        }
                    }           /* end of processing of edge pair */

                }               /* end of internal loop */

                if( pass == 1 )
                {
                    double scale = dist * buffer[i];

                    dist_scale = MAX( dist_scale, scale );
                }
            }
        }                       /* end of external loop */

        if( pass == 1 )
        {
            dist_scale = (dist_dim - 0.51) / dist_scale;
        }

    }                           /* end of pass on loops */


    /* convert hist to floats */
    for( i = 0; i < hist_size; i++ )
    {
        ((float *) pghi)[i] = (float) pghi[i];
    }

    if( buffer != local_buffer_ptr )
        cvFree( &buffer );

    return CV_OK;
}
Пример #10
0
// chain function; this function does the actual processing
static GstFlowReturn
gst_haar_adjust_chain(GstPad *pad, GstBuffer *buf)
{
    GstHaarAdjust *filter;

    // sanity checks
    g_return_val_if_fail(pad != NULL, GST_FLOW_ERROR);
    g_return_val_if_fail(buf != NULL, GST_FLOW_ERROR);

    filter = GST_HAARADJUST(GST_OBJECT_PARENT(pad));

    filter->image->imageData = (char*) GST_BUFFER_DATA(buf);

    if ((filter->rect_timestamp == GST_BUFFER_TIMESTAMP(buf)) &&
        (filter->rect_array != NULL) &&
        (filter->rect_array->len > 0)) {
        guint i;

        for (i = 0; i < filter->rect_array->len; ++i) {
            CvRect        rect;
            GstEvent     *event;
            GstMessage   *message;
            GstStructure *structure;
            gint          complement_height_top_bg, complement_height_bottom_bg,
                          complement_height_top_projected, complement_height_bottom_projected;

            rect = g_array_index(filter->rect_array, CvRect, i);

            complement_height_top_bg = complement_height_bottom_bg = -1;

            // Calculation of the 'height' complement of projected value
            complement_height_bottom_projected = complement_height_top_projected = (rect.height * filter->height_adjustment) - rect.height;

            // Calculation of the 'height' complement of haar rect and bg rect
            if ((filter->rect_bg_timestamp == GST_BUFFER_TIMESTAMP(buf)) &&
                    (filter->rect_bg_array != NULL) &&
                    (filter->rect_bg_array->len > 0)) {

                guint i;

                for (i = 0; i < filter->rect_bg_array->len; ++i) {
                    CvRect rect_bg_temp;
                    rect_bg_temp = g_array_index(filter->rect_bg_array, CvRect, i);

                    if (rectIntercept(&rect, &rect_bg_temp) == 1) {
                        complement_height_bottom_bg = rect_bg_temp.height - rect.height - (rect.y - rect_bg_temp.y);
                        complement_height_top_bg = rect_bg_temp.height - rect.height - ((rect_bg_temp.y + rect_bg_temp.height)-(rect.y + rect.height));
                        break;
                    }
                }
            }

            // adjust ROIs
            if (g_strcasecmp(filter->object_type, OBJECT_TYPE_UPPER_BODY) == 0) {

                if (CV_IABS(complement_height_bottom_projected - complement_height_bottom_bg) < (rect.height * MAX_PERC_DESVIATION_TO_FOLLOW_BG))
                    rect.height += complement_height_bottom_bg;
                else
                    rect.height += complement_height_bottom_projected;

            } else if (g_strcasecmp(filter->object_type, OBJECT_TYPE_LOWER_BODY) == 0) {

                if (CV_IABS(complement_height_bottom_projected - complement_height_bottom_bg) < (rect.height * MAX_PERC_DESVIATION_TO_FOLLOW_BG)) {
                    rect.height += complement_height_bottom_bg;
                    rect.y -= complement_height_bottom_bg;
                } else {
                    rect.height += complement_height_bottom_projected;
                    rect.y -= complement_height_bottom_projected;
                }

            } else {
                GST_ERROR("invalid object type: '%s'", filter->object_type);
                break;
            }

            // if greater than the image margins, set new limits
            //if (rect.x < 0) rect.x = 0;
            //if (rect.y < 0) rect.y = 0;
            //if (rect.x + rect.width  > filter->image->width ) rect.width  = filter->image->width  - rect.x;
            //if (rect.y + rect.height > filter->image->height) rect.height = filter->image->height - rect.y;

            if (filter->verbose)
                GST_INFO("[rect] x: %d, y: %d, width: %d, height: %d",
                         rect.x, rect.y, rect.width, rect.height);

            if (filter->display) {
                cvRectangle(filter->image,
                            cvPoint(rect.x, rect.y),
                            cvPoint(rect.x + rect.width, rect.y + rect.height),
                            CV_RGB(255, 0, 255), 1, 8, 0);
            }

            // send downstream event and bus message with the rect info
            structure = gst_structure_new("haar-adjust-roi",
                                          "x",         G_TYPE_UINT,   rect.x,
                                          "y",         G_TYPE_UINT,   rect.y,
                                          "width",     G_TYPE_UINT,   rect.width,
                                          "height",    G_TYPE_UINT,   rect.height,
                                          "timestamp", G_TYPE_UINT64, GST_BUFFER_TIMESTAMP(buf),
                                          NULL);

            message = gst_message_new_element(GST_OBJECT(filter), gst_structure_copy(structure));
            gst_element_post_message(GST_ELEMENT(filter), message);

            event   = gst_event_new_custom(GST_EVENT_CUSTOM_DOWNSTREAM, structure);
            gst_pad_push_event(filter->srcpad, event);
        }
    }

    gst_buffer_set_data(buf, (guint8*) filter->image->imageData, (guint) filter->image->imageSize);
    return gst_pad_push(filter->srcpad, buf);
}