1

I am working with binary images from CASIA database and opencv in a C++ project. I am looking for a way of extracting only the silhouette(the bounding box containing the silhouette). The original images are 240x320 and my goal is to get only the silhouette in a new image (let’s say 100x50 size). My first idea would be to get the minimum and maximum position of “white” pixels on rows and columns and get the pixels inside this rectangle in a new image, but I consider this not efficient at all. If you have any suggetion, I would be more than happy to hear it. On the left is the input and on the right is the output.

Input-Output

Theodor Badea
  • 465
  • 2
  • 9
  • Can you please attach some sample input images and expected output? – ZdaR Dec 04 '19 at 11:22
  • It's how computers work, why is it not efficient? The only quick option in my head is to scale the image down proportionaly (eg x4) and get a bounding box, then scale that box up x4 size and see if it "fits". Also show a sample image if possible. – VC.One Dec 04 '19 at 11:23
  • Check this out, it's already answered here https://stackoverflow.com/questions/13887863/extract-bounding-box-and-save-it-as-an-image – Ziri Dec 04 '19 at 12:08
  • Computers nowadays are very, very fast. You do not have to fear performance issues from this. Also unless you have done any precomputation on that image for some other reason within opencv (like contours extraction etc.), fear there is no other way than the trivial approach you mentioned. – AlexGeorg Dec 04 '19 at 13:33
  • 1
    An approach is to grayscale, threshold, get bounding box coordinates with `boundingRect`, crop the ROI, then resize to desired dimension – nathancy Dec 05 '19 at 00:35

1 Answers1

0

You can use the built-in OpenCV functionalities to find contours from your binary image:

e.g.

// using namespace cv;
vector<vector<Point> > contours;
vector<Vec4i> hierarchy;
findContours( your_binary_mat, contours, hierarchy, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_SIMPLE );

Note this will look for external contours (ignores inner contours which for the image above don't apply anyway) and retrieve a simplified approximation of the points.

Once you access the contour you can use either boundingRect() or minAreaRect() (wether you need the bounding box rotated or not).

George Profenza
  • 50,687
  • 19
  • 144
  • 218