c++ - Cropping out an image from an existing image -
i crop out image existing image. i've taken image , applied monochrome on threshold 98%
using imagemagick (is doable in opencv?)
the resulting image this:
now image crop out image final image looks this:
question how can in opencv? note, reason want crop image can use this answer part of text. if there no need crop out new image , instead concentrate on black part of image begin with, great.
if text @ top , @ bottom regions want crop out, if @ same location solution easy: just set roi ignores 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 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 original image 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 position of black rectangle, area of interest, not present on fixed location might want check out another approach: use rectangle detection technique:
on output above, area interested 2nd largest rectangle in image.
on side note, if plan isolate text later, simple cv::erode() remove noises in image left white box & text. technique remove noises use cv::medianblur()
.you can explore cv::morphologyex()
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 combination of these 3. i've demonstrated little bit of on extract hand bones x-ray image.
Comments
Post a Comment