簡體   English   中英

使用PNG圖像作為GrabCut的掩碼

[英]Using a PNG image as mask for GrabCut

我有一個綠色和紅色線條和透明背景的png圖像,我需要使用它作為執行GrabCut的掩碼。 但我得到意想不到的結果。 這是我的代碼:

//find the mask
Mat mask;
mask.create( image.size(), CV_8UC1);
mask.setTo(Scalar::all(GC_BGD));
Mat maskImg = imread("messi5.png");

for(int i=0; i<maskImg.cols; i++)    
    for(int j=0; j<maskImg.rows; j++)    
    {               
        //if it's red, make it white
        if ((int)maskImg.at<cv::Vec3b>(j,i)[0]==0 && (int)maskImg.at<cv::Vec3b>(j,i)[1] == 0 && (int)maskImg.at<cv::Vec3b>(j,i)[2] == 255) {

            mask.at<cv::Vec3b>(j,i)[0]= GC_BGD;
            mask.at<cv::Vec3b>(j,i)[1] = GC_BGD;
            mask.at<cv::Vec3b>(j,i)[2] = GC_BGD;
        }           

        //if it's green, make it black
        if ((int)maskImg.at<cv::Vec3b>(j,i)[0]==0 && (int)maskImg.at<cv::Vec3b>(j,i)[1] == 255 && (int)maskImg.at<cv::Vec3b>(j,i)[2] == 0) {    
            mask.at<cv::Vec3b>(j,i)[0] = GC_FGD;
            mask.at<cv::Vec3b>(j,i)[1] = GC_FGD;
            mask.at<cv::Vec3b>(j,i)[2] = GC_FGD;
        }
    }

 ...

這是輸出: http//prntscr.com/40kt4e 我想這就像沒有矩形,它只能看到GC_FGD像素,其他一切都被認為是BG。 它看起來有點縮放,但我不知道如何解決它。

我試着說

OpenCV(C ++)中PNG文件中的GrabCut讀取掩碼

你使用3通道訪問器的1通道圖像。 這會弄亂一些東西,使用1通道版本的1通道掩碼:

    Mat image;
    image= cv::imread(file);

    //everything outside this box will be set to def. 
    //background GC_BGD, clearly from the image you can see that the players legs are outside the box, 
    //so this will cause problems. you need to either change the box, 
    //such that everything is outside the box is the background, or use your mask to scribble on the players legs in green.  
    cv::Rect rectangle(startX, startY, width, height);  
    cv::Mat bgModel,fgModel;

    //find the mask
    Mat mask;
    mask.create( image.size(), CV_8UC1);  //CV_8UC1 is single channel
    mask.setTo(Scalar::all(GC_BGD));  //you have set it to all def. background 

    Mat maskImg = imread("messi5.png");


    for(int i=0; i<maskImg.cols; i++)    
        for(int j=0; j<maskImg.rows; j++)    
        {               
            //if it's red, make it black
            if ((int)maskImg.at<cv::Vec3b>(j,i)[0]==0 && (int)maskImg.at<cv::Vec3b>(j,i)[1] == 0 && (int)maskImg.at<cv::Vec3b>(j,i)[2] == 255) {

                //the whole mask is black so this is redundant
                mask.at<uchar>(j,i)= GC_BGD;  //GC_BGD := 0 := black 

            }           

            //if it's green, make it white
            if ((int)maskImg.at<cv::Vec3b>(j,i)[0]==0 && (int)maskImg.at<cv::Vec3b>(j,i)[1] == 255 && (int)maskImg.at<cv::Vec3b>(j,i)[2] == 0) {    

                    mask.at<uchar>(j,i) = GC_FGD; //GC_FGD:= 1 := white 

            }
        }

有關循環圖像的更有效代碼,請參閱: http//docs.opencv.org/doc/tutorials/core/how_to_scan_images/how_to_scan_images.html

LUT函數我在這里訣竅: http//docs.opencv.org/doc/tutorials/core/how_to_scan_images/how_to_scan_images.html#the-core-function

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM