Пример #1
0
	void GCApplication::showImage() const
	{
		if (image->empty() || winName->empty())
			return;

		Mat res;
		Mat binMask;
		if (!isInitialized)
			image->copyTo(res);
		else
		{
			getBinMask(mask, binMask);
			
			image->copyTo(res, binMask);
		}

		vector<Point>::const_iterator it;
		for (it = bgdPxls.begin(); it != bgdPxls.end(); ++it)
			circle(res, *it, radius, BLUE, thickness);
		for (it = fgdPxls.begin(); it != fgdPxls.end(); ++it)
			circle(res, *it, radius, RED, thickness);
		for (it = prBgdPxls.begin(); it != prBgdPxls.end(); ++it)
			circle(res, *it, radius, LIGHTBLUE, thickness);
		for (it = prFgdPxls.begin(); it != prFgdPxls.end(); ++it)
			circle(res, *it, radius, PINK, thickness);

		if (rectState == IN_PROCESS || rectState == SET)
			rectangle(res, Point(rect.x, rect.y), Point(rect.x + rect.width, rect.y + rect.height), GREEN, 2);

		imshow(*winName, res);
	}
Пример #2
0
Mat GrabCut::getFG()
{
    Mat fg = cv::Mat::zeros(_src.size(), _src.type());
    Mat mask = getBinMask();
    _src.copyTo(fg, mask);
    return fg;
}
Пример #3
0
void GrabCut::show()
{
    Scalar fg_color(255,0,0);
    Scalar bg_color(0,255,0);
    cv::Mat scribbled_src = _src.clone();
    const float alpha = 0.7f;
    
    for(int y=0; y < _gcut.rows; y++){
        for(int x=0; x < _gcut.cols; x++){
            if(_gcut.at<uchar>(y, x) == cv::GC_FGD) {
                cv::circle(scribbled_src, cv::Point(x, y), 2, fg_color, -1);
            } 
            else if(_gcut.at<uchar>(y, x) == cv::GC_BGD) {
                cv::circle(scribbled_src, cv::Point(x, y), 2, bg_color, -1);
            } 
            else if(_gcut.at<uchar>(y, x) == cv::GC_PR_BGD) {
                cv::Vec3b& pix = scribbled_src.at<cv::Vec3b>(y, x);
                pix[0] = (uchar)(pix[0] * alpha + bg_color[0] * (1-alpha));
                pix[1] = (uchar)(pix[1] * alpha + bg_color[1] * (1-alpha));
                pix[2] = (uchar)(pix[2] * alpha + bg_color[2] * (1-alpha));
            } 
            else if(_gcut.at<uchar>(y, x) == cv::GC_PR_FGD) {
                cv::Vec3b& pix = scribbled_src.at<cv::Vec3b>(y, x);
                pix[0] = (uchar)(pix[0] * alpha + fg_color[0] * (1-alpha));
                pix[1] = (uchar)(pix[1] * alpha + fg_color[1] * (1-alpha));
                pix[2] = (uchar)(pix[2] * alpha + fg_color[2] * (1-alpha));
            }
        }
    }
    Rect roi;
    Mat scrb;
    roi = Rect(_src.cols,0,_src.cols,_src.rows);
    scribbled_src.copyTo(_dsp(roi));
    
    Mat msk = getBinMask();
    cv::Mat send = msk.clone();
    cv::Mat sendFilter;
    cv::Mat resultMat = filterMaskAndMergeMat(send, _cutResultMask, _gcutBuffer);
    Mat fg = getFGByMask(resultMat);//getFG();
    roi = Rect(_src.cols,_src.rows,_src.cols,_src.rows);
    fg.copyTo(_dsp(roi));
  //  imshow("fg", fg);
    
   // imshow("mergeResultMat", resultMat);
    _cutResultMask = resultMat;
    //Rect    tmp = getMaskRct(_gcutBuffer);
    cvtColor(msk,msk,COLOR_GRAY2BGR);
    roi = Rect(0,_src.rows,_src.cols,_src.rows);
    msk.copyTo(_dsp(roi));
    //imshow("msk", msk);
    //imshow("_mask", _mask);
    imshow(_name,_dsp);
    waitKey(1);
}
Пример #4
0
void showCompImage(Mat& compImg)
{
    Mat binMask;
    getBinMask(sepAPP.mask, binMask);
    int newR = foreImg.rows * alpha;
    int newC = foreImg.cols * alpha;

    Mat selfFore = foreImg.clone();
    resize(binMask, binMask, Size(newC, newR));
    resize(selfFore, selfFore, Size(newC, newR));
    Mat parentFore;
    selfFore.copyTo(parentFore, binMask);
    backImg.copyTo(compImg);
    copySubsMat(selfFore, compImg, x, y, binMask);
}
void Grabcut_app::showImage(int number, bool full)
{
    if( p_image->empty() || p_win_name->empty() )
        return;

    if (number >= 0)
        p_number = number;

    cv::Mat res;
    cv::Mat binMask;
    if( !p_is_initialized )
        p_image->copyTo( res );
    else
    {
        if (m_overlay)
            res = (*p_image).clone();
        else
            res = 0.3*(*p_image);

        getBinMask( p_mask, binMask, full);
        p_image->copyTo( res, binMask );
    }

    std::vector<cv::Point>::const_iterator it;
    cv::Mat circle_overlay = res.clone();
    for( it = p_bgdPxls.begin(); it != p_bgdPxls.end(); ++it )
        cv::circle( circle_overlay, *it, p_radius, P_BLUE, p_thickness );
    for( it = p_fgdPxls.begin(); it != p_fgdPxls.end(); ++it )
        cv::circle( circle_overlay, *it, p_radius, P_RED, p_thickness );
    for( it = p_prBgdPxls.begin(); it != p_prBgdPxls.end(); ++it )
        cv::circle( circle_overlay, *it, p_radius, P_LIGHTBLUE, p_thickness );
    for( it = p_prFgdPxls.begin(); it != p_prFgdPxls.end(); ++it )
        cv::circle( circle_overlay, *it, p_radius, P_PINK, p_thickness );

    if( (p_rect_state == IN_PROCESS || p_rect_state == SET) && !m_validation ) {
        cv::rectangle(circle_overlay, cv::Point(p_rect.x, p_rect.y), cv::Point(p_rect.x + p_rect.width, p_rect.y + p_rect.height), P_GREEN, 1);
        //cv::rectangle(circle_overlay, cv::Point(p_roi_rect.x, p_roi_rect.y), cv::Point(p_roi_rect.x + p_roi_rect.width, p_roi_rect.y + p_roi_rect.height), P_RED, 1);
    }

    res = 0.5*res + 0.5*circle_overlay;


    if (m_show_enclosing_rest){
        cv::Point2f vertices[4];
        m_enclosing_rect.points(vertices);
        for (int i = 0; i < 3; i++)
            line(res, vertices[i], vertices[(i+1)], P_BLUE);
        line(res, vertices[3], vertices[0], P_BLUE);
    }

    if (m_validation) {
        cv::Mat b(res.size(), CV_8UC1), g(res.size(), CV_8UC1), r(res.size(), CV_8UC1);
        std::vector<cv::Mat> mat_arr = {b, g, r};
        cv::split(res, mat_arr);
        double ratio = m_valid_ratio;
        if (!m_overlay)
            ratio = 0.5*ratio;

        cv::max(g, p_mask_valid_fg*ratio, g);
        cv::max(r, p_mask_valid_bg*ratio, r);
        cv::merge(mat_arr, res);
    }

    if (p_number >= 0) {
        std::stringstream s; std::string num;
        s << "#" << p_number;
        s >> num;
        cv::putText(res, num, cvPoint(5,15), cv::FONT_HERSHEY_COMPLEX_SMALL, 0.8, cvScalar(218,218,40), 1, CV_AA);
    }