OpenCV(Version 3.2.0) - Extraction of ROI

Viewed 23

I have been working on extracting the ROI from the image given below. Fluroscopic Image

This code is supposed to work for images of all resolutions. My approach is to:

  1. Find the largest contour and store it's ID.
  2. Use minEnclosingCircle() function to find the minimum bounding circle.

But, this circle is being offset to the top-left corner.

Here is my code:

#include<iostream>
    #include<opencv2/opencv.hpp>

    using namespace std;
    using namespace cv;

    int largestContourId(vector<vector<Point>> contourVec); //Function to find ID of largest contour

    int main(int argc, char **argv )
    {
    if ((argv[1] != 0) && (argv[2] != 0))
    {

        String path = argv[1]; //Path of image to be given here
        
        Mat img = imread(path); // Initialize and read image
        Mat img_copy = img.clone(); //Make a copy of the orignal image
        Mat img_gray;   //Initialize grey image matrix
        Mat thresh;     //Initialize threshold mask
        Mat result;     //Initialize resulting image matrix

        int maxAreaContourId; //This stores ID of the largest contour
        Point2f center; //MEC center
        float radius;   //MEC radius

        vector<vector<Point>> contours; // Defining contour vector
        vector<Vec4i> hierarchy; //Defining contour hierarchy vector

        cout << "Resolution of image: " << img.size() << endl;

        cvtColor(img,img_gray,COLOR_BGR2GRAY); // Convert from colored to grayscale image
        threshold(img_gray,thresh,5,255,THRESH_BINARY); //Apply threshold mask and convert to a binary image

        imshow("Binary img",thresh); //Display binary image in B&W

        findContours(thresh,contours,hierarchy,RETR_TREE,CHAIN_APPROX_NONE); //Find contours and hierarchy within using the Simple Approximation method 

        maxAreaContourId = largestContourId(contours); //Store largest contour ID
        
        minEnclosingCircle(contours[maxAreaContourId],center,radius);
        circle(img_copy,center,radius-20.0,Scalar(255,255,255),-1);
        
        bitwise_not(img_copy,img_copy);
        bitwise_xor(img,img_copy,result);

        
        imshow("Orignal Image",img); //Show orignal image
        imshow("Result",result); //Show the result of the plot
        waitKey(0); //Wait for input

        imwrite(argv[2],result); //Write the result to a file
        destroyAllWindows();
        
        return 0;
    }
    else
    {
        cout << "Please enter CLA in the format: *input_file* *output_file*" << endl;
        return 0;
    }
    }

    int largestContourId(vector<vector<Point>> contourVec) //Function to find ID of largest contour
{
        double maxArea = 0; //Intialize area contour
    int maxAreaContourId = -1; //Intialize largest contour ID

    for(int j = 0; j < contourVec.size(); j++) //Cycle through contour vector
    {
        double newArea = contourArea(contourVec.at(j)); //Variable for comparing area
        if (newArea > maxArea) //Comparison of areas
        {
            maxArea = newArea;
            maxAreaContourId = j;
        }
    }
    return maxAreaContourId; //Largest contour ID to be returned
}

This is my output: Output Fluroscopic Image

I would like this code to work for all sorts of images even if the text is written on th e ROI itself. Please let me know if my current approach is feasible or not.

Suggestions are always welcome.

0 Answers
Related