sample-projects icon indicating copy to clipboard operation
sample-projects copied to clipboard

stitcher doesn't remove the black area

Open indra365 opened this issue 6 years ago • 3 comments

panorama stitcher doesn't crop the result image to remove that black area

indra365 avatar Mar 04 '19 09:03 indra365

Need to convert this code to Java, since javacv doesn't have an easy way to access the pixel ..

//find largest roi
bool cropLargestPossibleROI(const cv::Mat& gray, cv::Mat& pano, cv::Rect startROI)
{
    // evaluate start-ROI
    Mat possibleROI = gray(startROI);
    bool topOk = checkRow(possibleROI, 0);
    bool leftOk = checkColumn(possibleROI, 0);
    bool bottomOk = checkRow(possibleROI, possibleROI.rows-1);
    bool rightOk = checkColumn(possibleROI, possibleROI.cols-1);
    if(topOk && leftOk && bottomOk && rightOk)
    {
        // Found!!
        LOGE("cropLargestPossibleROI success");
        pano = pano(startROI);
        return true;
    }
    // If not, scale ROI down
    Rect newROI(startROI.x, startROI.y, startROI.width, startROI.height);
    // if x is increased, width has to be decreased to compensate
    if(!leftOk)
    {
        newROI.x++;
        newROI.width--;
    }
    // same is valid for y
    if(!topOk)
    {
        newROI.y++;
        newROI.height--;
    }
    if(!rightOk)
    {
        newROI.width--;
    }
    if(!bottomOk)
    {
        newROI.height--;
    }
    if(newROI.x + startROI.width < 0 || newROI.y + newROI.height < 0)
    {
        //sorry...
        LOGE("cropLargestPossibleROI failed");
        return false;
    }
    return cropLargestPossibleROI(gray,pano,newROI);
}

/check row
bool checkRow(const cv::Mat& roi, int y)
{
    int zeroCount = 0;
    for(int x=0; x<roi.cols; x++)
    {
        if(roi.at<uchar>(y, x) == 0)
        {
            zeroCount++;
        }
    }
    if((zeroCount/(float)roi.cols)>cutBlackThreshold)
    {
        return false;
    }
    return true;
}

//check col
bool checkColumn(const cv::Mat& roi, int x)
{
    int zeroCount = 0;
    for(int y=0; y<roi.rows; y++)
    {
        if(roi.at<uchar>(y, x) == 0)
        {
            zeroCount++;
        }
    }
    if((zeroCount/(float)roi.rows)>cutBlackThreshold)
    {
        return false;
    }
    return true;
}

indra365 avatar Mar 08 '19 07:03 indra365

Yes we can easily access the pixel values using indexers: http://bytedeco.org/news/2014/12/23/third-release/

saudet avatar Mar 08 '19 08:03 saudet

Hi @indra365 Are you done with this things, Can you share the code crop the black area,.. Thanks

begiflow avatar Nov 18 '22 08:11 begiflow