0

How to draw inscribed rectangle in fish eye corrected image using opencv?

This is the fish eye corrected image: https://plus.google.com/u/0/+AnandMuglikar/posts/iP6WKBb3whz?pid=6031031942035796338&oid=110347380425323822802

This is what I need to crop the image: Either the 4 corner points of the rectangle or the midpoints of the sides.

https://plus.google.com/u/0/+AnandMuglikar/posts/iP6WKBb3whz?pid=6031031940808141666&oid=110347380425323822802

Final manually corrected image: lh3.googleusercontent.com/-KAylBUEtYO0/U7KJhDEhVWI/AAAAAAAAQDY/UA1NRjCRYNk/w742-h434-no/rect.jpg

muglikar
  • 126
  • 1
  • 15
  • maybe you can try my answer from: http://stackoverflow.com/questions/21410449/how-do-i-crop-to-largest-interior-bounding-box-in-opencv/21479072#21479072 – Micka Jul 01 '14 at 11:55
  • Hi @Micka, could you please elaborate on the sortX and sortY functions? And what is the interiorBBExtraction.png ? – muglikar Jul 02 '14 at 07:59
  • `interiorBBExtraction.png` was the image of the original poster in the question. It's not used in the algorithm since it's overwritten 1 line later (shouldn be there anyway because it doesnt compile that way - was commented out in my code and didnt copy the `//`). Added `sortX` and `sortY`. – Micka Jul 02 '14 at 08:18
  • Hi @Micka, could you please elaborate on the sortX and sortY functions? And what is the interiorBBExtraction.png ? Why do we see input redefined? – muglikar Jul 02 '14 at 08:25
  • have a look at my answer from http://stackoverflow.com/questions/21410449/how-do-i-crop-to-largest-interior-bounding-box-in-opencv/21479072#21479072 I updated my code there. – Micka Jul 02 '14 at 08:27
  • Hi @Micka, I get an error which says "vector subscript out of range" on the 2nd iteration of the while loop, even when trying your given lena image on this line: cv::Point max(cSortedX[maxXId].x, cSortedY[maxYId].y); – muglikar Jul 02 '14 at 09:46
  • Hi @Micka, It was my mistake in the last comment. But I'm not getting the true value for 'finished' variable for long and the end result is like this: https://lh3.googleusercontent.com/-okptxDqDXe4/U7PXtNb411I/AAAAAAAAQE0/ubsR6WJxQUA/s506/interiorBoundingBoxResult.png That is no largest inscribed rectangle is being shown. – muglikar Jul 02 '14 at 09:57

1 Answers1

1

using my own code from

How do I crop to largest interior bounding box in OpenCV?

int main()
{

    //cv::Mat input = cv::imread("interiorBBExtraction.png");
    //cv::Mat input = cv::imread("LenaWithBG.png");
    cv::Mat input = cv::imread("fisheye_interior.png");

    cv::Mat gray;
    cv::cvtColor(input,gray,CV_BGR2GRAY);

    cv::imshow("gray", gray);

    cv::Mat mask = gray>0;
    cv::imshow("mask", mask);


    std::vector<std::vector<cv::Point> > contours;
    std::vector<cv::Vec4i> hierarchy;

    cv::findContours(mask,contours,hierarchy, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_NONE, cv::Point(0,0));

    std::cout << "found contours: " << contours.size() << std::endl;


    cv::Mat contourImage = cv::Mat::zeros( input.size(), CV_8UC3 );;

    //find contour with max elements
    // remark: in theory there should be only one single outer contour surrounded by black regions!!

    unsigned int maxSize = 0;
    unsigned int id = 0;
    for(unsigned int i=0; i<contours.size(); ++i)
    {
        if(contours.at(i).size() > maxSize)
        {
            maxSize = contours.at(i).size();
            id = i;
        }
    }

    std::cout << "chosen id: " << id << std::endl;
    std::cout << "max size: " << maxSize << std::endl;

    /// Draw contour
    cv::Scalar color = cv::Scalar( 255, 255,255);
    //cv::drawContours( contourImage, contours, id, color, 1, 8, hierarchy, 0, cv::Point() );
    cv::drawContours( contourImage, contours, id, color, 1, 8, hierarchy, 0, cv::Point() );

    cv::Mat contourMask = cv::Mat::zeros( input.size(), CV_8UC1 );
    cv::drawContours( contourMask, contours, id, cv::Scalar(255), -1, 8, hierarchy, 0, cv::Point() );
    cv::imshow("contour mask", contourMask);

    cv::imshow("contours",contourImage);

    std::vector<cv::Point> usedContour = contours.at(id);

    std::vector<cv::Point> cSortedX = contours.at(id);
    //std::cout << cSortedX.size() << std::endl;
    std::sort(cSortedX.begin(), cSortedX.end(), sortX);
    std::cout << cSortedX.front().x << ",...,  " << cSortedX.back().x << std::endl;

    std::vector<cv::Point> cSortedY = contours.at(id);
    //std::cout << cSortedY.size() << std::endl;
    std::sort(cSortedY.begin(), cSortedY.end(), sortY);
    std::cout << cSortedY.front().y << ",...,  " << cSortedY.back().y << std::endl;


    unsigned int minXId = 0;
    unsigned int maxXId = cSortedX.size()-1;

    unsigned int minYId = 0;
    unsigned int maxYId = cSortedY.size()-1;

    cv::Rect interiorBB;



    while( (minXId<maxXId)&&(minYId<maxYId) )
    {
        cv::Point min(cSortedX[minXId].x, cSortedY[minYId].y);
        cv::Point max(cSortedX[maxXId].x, cSortedY[maxYId].y);
        //std::cout << min << " ... " << max << std::endl;
        interiorBB = cv::Rect(min.x,min.y, max.x-min.x, max.y-min.y);

        //std::cout << interiorBB << std::endl;

        int ocTop = 0;
        int ocBottom = 0;
        int ocLeft = 0;
        int ocRight = 0;

        bool finished = checkInteriorExterior(contourMask, interiorBB, ocTop, ocBottom,ocLeft, ocRight);
        if(finished)
        {
            break;
        }


        if(ocLeft)++minXId;
        if(ocRight) --maxXId;

        if(ocTop) ++minYId;
        if(ocBottom)--maxYId;


    }

    std::cout <<  "done! : " << interiorBB << std::endl;

    cv::Mat mask2 = cv::Mat::zeros(input.rows, input.cols, CV_8UC1);
    cv::rectangle(mask2,interiorBB, cv::Scalar(255),-1);

    cv::Mat maskedImage;
    input.copyTo(maskedImage);
    for(unsigned int y=0; y<maskedImage.rows; ++y)
        for(unsigned int x=0; x<maskedImage.cols; ++x)
        {
            maskedImage.at<cv::Vec3b>(y,x)[2] = 255;
        }
    input.copyTo(maskedImage,mask2);

    cv::imshow("masked image", maskedImage);
    cv::imwrite("interiorBoundingBoxFisheyeResult.png", maskedImage);



    cv::waitKey(-1);
    return 0;
}

with

bool sortX(cv::Point a, cv::Point b)
{
    bool ret = false;
    if(a.x == a.x)
        if(b.x==b.x)
            ret = a.x < b.x;

    return ret;
}

bool sortY(cv::Point a, cv::Point b)
{
    bool ret = false;
    if(a.y == a.y)
        if(b.y == b.y)
            ret = a.y < b.y;


    return ret;
}

bool checkInteriorExterior(const cv::Mat&mask, const cv::Rect&interiorBB, int&top, int&bottom, int&left, int&right)
{
    bool returnVal = true;

    //std::cout << interiorBB << std::endl;
    cv::Mat sub = mask(interiorBB);

    //cv::imshow("sub",sub); cv::waitKey(10);

    unsigned int max = 0;

    // top row:
    unsigned int x=0;
    unsigned int y=0;

    unsigned int cTop=0;
    unsigned int cBottom=0;
    unsigned int cLeft=0;
    unsigned int cRight=0;

    for(y=0, x=0 ; x<sub.cols; ++x)
    {
        // if there is an exterior part in the interior we have to move the top side of the rect a bit to the bottom
        if(sub.at<unsigned char>(y,x) == 0)
        {
            //          std::cout << "top: " << cv::Point(x,y) << std::endl;
            returnVal = false;
            ++cTop;
        }
    }

    for(y=sub.rows-1, x=0; x<sub.cols; ++x)
    {
        // if there is an exterior part in the interior we have to move the bottom side of the rect a bit to the top
        if(sub.at<unsigned char>(y,x) == 0)
        {
//          std::cout << "bottom: " << cv::Point(x,y) << std::endl;
            returnVal = false;
            ++cBottom;
        }
    }

    for(y=0, x=0 ; y<sub.rows; ++y)
    {
        // if there is an exterior part in the interior
        if(sub.at<unsigned char>(y,x) == 0)
        {
            //          std::cout << "left: " << cv::Point(x,y) << std::endl;
            returnVal = false;
            ++cLeft;
        }
    }

    for(x=sub.cols-1, y=0; y<sub.rows; ++y)
    {
        // if there is an exterior part in the interior
        if(sub.at<unsigned char>(y,x) == 0)
        {
            //          std::cout << "right: " << cv::Point(x,y) << std::endl;
            returnVal = false;
            ++cRight;
        }
    }

    if(cTop > cBottom)
    {
        if(cTop > cLeft)
            if(cTop > cRight)
                top = 1;
    }
    else
        if(cBottom > cLeft)
            if(cBottom > cRight)
                bottom = 1;

    if(cLeft >= cRight)
    {
        if(cLeft >= cBottom)
            if(cLeft >= cTop)
                left = 1;
    }
    else
        if(cRight >= cTop)
            if(cRight >= cBottom)
                right = 1;



    return returnVal;
}

and using this image as input:

enter image description here

i get this output:

enter image description here

not sure what happens if the green border disappears (in dont have such an image), since the original image contains some black parts too.

Community
  • 1
  • 1
Micka
  • 17,208
  • 4
  • 46
  • 68