Cropping Out An Image From An Existing Image
Solution 1:
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>intmain(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);
return0;
}
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.
Solution 2:
A simple solution: scan lines from top down, bottom up, left-right and right-left. Terminate when the number of dark pixels in the line exceeds 50% of the total amount of pixels in the line. This will give you the xmin, xmax, ymin, ymax coordinates to bound your cropping rectangle.
Post a Comment for "Cropping Out An Image From An Existing Image"