bytedeco / sample-projects

Sample project files for JavaCPP, JavaCPP Presets, and JavaCV
219 stars 174 forks source link

stitcher doesn't remove the black area #44

Open indra365 opened 5 years ago

indra365 commented 5 years ago

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

indra365 commented 5 years ago

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;
}
saudet commented 5 years ago

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

chubecode commented 2 years ago

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