Jan Rabe
Jan Rabe

Reputation: 398

How to get rectangles around the objects in the image?

what would be the easiest way to programmatically get the rectangles around the objects in the image? looking for a solution in c#

enter image description here

i'm not entirely sure how to approach the search for this. any hints are highly welcome.

*edit: As Bobby guessed correctly, I'm trying to find the surrounding rectangles around the blobs. The spots vary in size and shape and position. They could be star shaped and round for instance. As TaW stated I would need to figure out what pixels are connected to each other. How would you deal with holes?

best wishes

Upvotes: 3

Views: 1248

Answers (2)

zeFrenchy
zeFrenchy

Reputation: 6591

A simple approach could be

  1. scan the image for a red pixel
  2. perform a FloodFill keeping track of the min/max x and y for successful colour replacements ... when the fill finishes you have one bounding box. Plus you have eliminated all red pixels for that blob
  3. goto 1 and resume scanning

Upvotes: 3

Margus
Margus

Reputation: 20068

This is simplified question of Find Waldo assuming you can call Wolfram language in C#.net. I do not have Wolfram on this computer, but it should be something like:

img = Import["https://i.sstatic.net/qlVlM.png"];
objectshape = SelectComponents[DeleteBorderComponents[Binarize[img, {0, .7}]], "Area"}, 10 < #1 < 1000 && #2 > 0 &];
shapes = ComponentMeasurements[ImageMultiply[img, objectshape], {"BoundingBoxArea"}][[All, 2]];
Show[img, Graphics[{Red, Thick, Rectangle @@ # & /@ shapes}]]

Very similar result, what i based my answer on : segmentation analysis

Upvotes: 1

Related Questions