Cropping out an image from an existing image

12,030

If the text at the top and at the bottom are the regions that you want to crop out, if they are always at the same location the solution is easy: just set a ROI that ignores those areas:

#include <cv.h>
#include <highgui.h>

int main(int argc, char* argv[])
{
    cv::Mat img = cv::imread(argv[1]);
    if (img.empty())
    {
        std::cout << "!!! imread() failed to open target image" << std::endl;
        return -1;        
    }

    /* Set Region of Interest */

    int offset_x = 129;
    int offset_y = 129;

    cv::Rect roi;
    roi.x = offset_x;
    roi.y = offset_y;
    roi.width = img.size().width - (offset_x*2);
    roi.height = img.size().height - (offset_y*2);

    /* Crop the original image to the defined ROI */

    cv::Mat crop = img(roi);
    cv::imshow("crop", crop);
    cv::waitKey(0);

    cv::imwrite("noises_cropped.png", crop);

    return 0;
}

Output image:

If the position of the black rectangle, which is your area of interest, is not present on a fixed location then you might want to check out another approach: use the rectangle detection technique:

On the output above, the area you are interested will be 2nd largest rectangle in the image.

On a side note, if you plan to isolate the text later, a simple cv::erode() could remove all the noises in that image so you are left with the white box & text. Another technique to remove noises is to use cv::medianBlur().You can also explore cv::morphologyEx() to do that trick:

cv::Mat kernel = cv::getStructuringElement(cv::MORPH_ELLIPSE, cv::Size(7, 7), cv::Point(3, 3));
cv::morphologyEx(src, src, cv::MORPH_ELLIPSE, kernel);    

A proper solution might even be a combination of these 3. I've demonstrated a little bit of that on Extract hand bones from X-ray image.

Share:
12,030
birdy
Author by

birdy

Updated on June 04, 2022

Comments

  • birdy
    birdy almost 2 years

    I would like to crop out an image from an existing image. I've taken an image and applied monochrome on it with threshold 98% using imagemagick (is this doable in openCV?)

    The resulting image is this:

    enter image description here

    Now from this Image I would like to crop out another image so that the final image looks like this:

    enter image description here

    Question How can I do this in OpenCV? Note, the only reason I want to crop the image is so that I can use this answer to get the part of the text. If there is no need to crop out a new image and instead just concentrate on black part of the image to begin with, that would be great.