/* Mutator for rectangle. */
void GrabCut3DObjectSegmenter::rectIs(const Rect &_r)
{
    if (_r == DEFAULT_RECT)
        rect_ = Rect(image_.cols/(DEFAULT_RECT_SCALE_FACTOR*2),
                     image_.rows/(DEFAULT_RECT_SCALE_FACTOR*2),
                     image_.cols/DEFAULT_RECT_SCALE_FACTOR,
                     image_.rows/DEFAULT_RECT_SCALE_FACTOR);
    else
        rect_=_r;

    rect_state_ = SET;

    ROS_INFO_STREAM("rect area is " << rect_.area());
    if (rect_.area()==0)
        rect_state_=EMPTY;

    setRectInMask();
    assert( bgd_pxls_.empty()
            && fgd_pxls_.empty()
            && pr_bgd_pxls_.empty()
            && pr_fgd_pxls_.empty() );
    updateDisplayImage();

}
コード例 #2
0
ファイル: GCApplication.cpp プロジェクト: islam56naser/3Dizer
	void GCApplication::mouseClick(int event, int x, int y, int flags, void*)
	{
		// TODO add bad args check
		switch (event)
		{
		case EVENT_LBUTTONDOWN: // set rect or GC_BGD(GC_FGD) labels
		{
			bool isb = (flags & BGD_KEY) != 0,
				isf = (flags & FGD_KEY) != 0;
			if (rectState == NOT_SET && !isb && !isf)
			{
				rectState = IN_PROCESS;
				rect = Rect(x, y, 1, 1);
			}
			if ((isb || isf) && rectState == SET)
				lblsState = IN_PROCESS;
		}
		break;
		case EVENT_RBUTTONDOWN: // set GC_PR_BGD(GC_PR_FGD) labels
		{
			bool isb = (flags & BGD_KEY) != 0,
				isf = (flags & FGD_KEY) != 0;
			if ((isb || isf) && rectState == SET)
				prLblsState = IN_PROCESS;
		}
		break;
		case EVENT_LBUTTONUP:
			if (rectState == IN_PROCESS)
			{
				rect = Rect(Point(rect.x, rect.y), Point(x, y));
				rectState = SET;
				setRectInMask();
				CV_Assert(bgdPxls.empty() && fgdPxls.empty() && prBgdPxls.empty() && prFgdPxls.empty());
				showImage();
			}
			if (lblsState == IN_PROCESS)
			{
				setLblsInMask(flags, Point(x, y), false);
				lblsState = SET;
				showImage();
			}
			break;
		case EVENT_RBUTTONUP:
			if (prLblsState == IN_PROCESS)
			{
				setLblsInMask(flags, Point(x, y), true);
				prLblsState = SET;
				showImage();
			}
			break;
		case EVENT_MOUSEMOVE:
			if (rectState == IN_PROCESS)
			{
				rect = Rect(Point(rect.x, rect.y), Point(x, y));
				CV_Assert(bgdPxls.empty() && fgdPxls.empty() && prBgdPxls.empty() && prFgdPxls.empty());
				showImage();
			}
			else if (lblsState == IN_PROCESS)
			{
				setLblsInMask(flags, Point(x, y), false);
				showImage();
			}
			else if (prLblsState == IN_PROCESS)
			{
				setLblsInMask(flags, Point(x, y), true);
				showImage();
			}
			break;
		}
	}
/* Mouse callback function passed to OpenCV window. Handles marking the ROI
 * rectangle and foreground and background pixel hints. */
void GrabCut3DObjectSegmenter::mouseClick( MouseEvent event, int x, int y, bool control_down, bool shift_down)
{
    // TODO add bad args check
    switch( event )
    {
    case LEFT_BUTTON_DOWN: // set rect or GC_BGD(GC_FGD) labels
    {
        bool isb = control_down;
        bool isf = shift_down;
        if( rect_state_ == NOT_SET && !isb && !isf )
        {
            rect_state_ = IN_PROCESS;
            rect_ = Rect( x, y, 1, 1 );
        }
        if ( (isb || isf) && rect_state_ == SET )
            lbls_state_ = IN_PROCESS;
    }
    break;

    case RIGHT_BUTTON_DOWN: // set GC_PR_BGD(GC_PR_FGD) labels
    {
        bool isb = control_down;
        bool isf = shift_down;
        if ( (isb || isf) && rect_state_ == SET )
            pr_lbls_state_ = IN_PROCESS;
    }
    break;

    case LEFT_BUTTON_UP:
        if( rect_state_ == IN_PROCESS )
        {
            rect_ = Rect( Point(rect_.x, rect_.y), Point(x,y) );
            rect_state_ = SET;

            ROS_INFO_STREAM("rect area is " << rect_.area());
            if (rect_.area()==0)
                rect_state_=EMPTY;

            setRectInMask();
            assert( bgd_pxls_.empty()
                    && fgd_pxls_.empty()
                    && pr_bgd_pxls_.empty()
                    && pr_fgd_pxls_.empty() );
            updateDisplayImage();
        }
        if( lbls_state_ == IN_PROCESS )
        {
            setLblsInMask(control_down, shift_down, Point(x,y), false);
            lbls_state_ = SET;
            updateDisplayImage();
        }
        break;

    case RIGHT_BUTTON_UP:
        if( pr_lbls_state_ == IN_PROCESS )
        {
            setLblsInMask(control_down, shift_down, Point(x,y), true);
            pr_lbls_state_ = SET;
            updateDisplayImage();
        }
        break;

    case MOUSE_MOVE:
        if( rect_state_ == IN_PROCESS )
        {
            rect_ = Rect( Point(rect_.x, rect_.y), Point(x,y) );
            assert( bgd_pxls_.empty()
                    && fgd_pxls_.empty()
                    && pr_bgd_pxls_.empty()
                    && pr_fgd_pxls_.empty() );
            updateDisplayImage();
        }
        else if( lbls_state_ == IN_PROCESS )
        {
            setLblsInMask(control_down, shift_down, Point(x,y), false);
            updateDisplayImage();
        }
        else if( pr_lbls_state_ == IN_PROCESS )
        {
            setLblsInMask(control_down, shift_down, Point(x,y), true);
            updateDisplayImage();
        }
        break;
    }
}