0

I already tried to search about openCV ROI function, but All of them used rectangle roi function.

I want to get roi using by inclined line that get from hough transform function.

My situation is next :

I have multiple vertical lines(little inclined) that output from hough transform function.

i want to get image(Matrix) between vertical lines. enter image description here

i want to get divided matrix in my image (For example, A image, B image, C image etc.. )

Is there ROI function that used line in openCV? or any another method?

amatuer
  • 3
  • 4
  • How do you propose to use a single line (or perhaps line segment) to define a rectangular subregion of the original image (that's what ROI means here)? What is the expected outcome? Please illustrate. – Dan Mašek Apr 24 '18 at 15:18

1 Answers1

0

I think you need to use contours to define your roi. If it is not a perfect square you can not use the ROI function, because this is always a perfect square (not even a rotated square)

int main()
{
    enum hierIdx { H_NEXT = 0, H_PREVIOUS, H_FIRST_CHILD, H_PARENT };
    cv::Mat img = cv::imread("example_image.jpg", cv::IMREAD_UNCHANGED);
    // convert RGB to gray scale image
    cv::Mat imgGrs;
    cv::cvtColor(img, imgGrs, cv::COLOR_RGB2GRAY);
    // because it was a .jpg the grey values are messed up
    // we fix it by thresholding at 128
    cv::threshold(imgGrs, imgGrs, 128, 255, cv::THRESH_BINARY);
    imgGrs = ~imgGrs;
    // now create contours (we need the hierarchy to find the inner shapes)
    std::vector<std::vector<cv::Point> > contours;
    std::vector<cv::Vec4i> hierarchy;
    cv::findContours(imgGrs.clone(), contours, hierarchy, CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE);
    //cv::drawContours(img, contours, -1, cv::Scalar(255, 0, 0), 1);

    int iLen = (int)hierarchy.size();
    int idxChild = -1;
    // find first child of master
    for (int i = 0; i < iLen; i++){
        if (hierarchy[i][H_PARENT] < 0) {
            idxChild = hierarchy[i][H_FIRST_CHILD];
            break;
        }
    }

    // used for erosion of mask
    cv::Mat element = cv::getStructuringElement(cv::MORPH_ELLIPSE, cv::Size(3, 3));

    while (idxChild >= 0)
    {
        // create image to use as mask for section
        cv::Mat mask = cv::Mat::zeros(imgGrs.size(), CV_8U);
        cv::drawContours(mask, contours, idxChild, cv::Scalar(255), CV_FILLED);
        // make masker 1 pixel smaller so we wont see the outer contours
        cv::erode(mask, mask, element);
        // ok nu we create a singled out part we want
        cv::Mat part = imgGrs & mask;
        // Crop it to the AOI rectangle
        cv::Rect aoi = cv::boundingRect(contours[idxChild]);
        part = part(aoi);
        // part is now the aoi image you asked for

        // proceed to next AOI
        idxChild = hierarchy[idxChild][H_NEXT];
    }
    return 0;
}
Benno Geels
  • 118
  • 7
  • thanks you for answer. but, there isn't function that get image from contour. just draw and then end – amatuer Apr 26 '18 at 00:59