3

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

Jan Rabe
  • 398
  • 2
  • 12
  • I can't see any rectangle around objects. What do you mean by *rectangles around the objects*? – Sriram Sakthivel Aug 21 '14 at 14:14
  • 1
    He wants to draw rectangles around the red blobs – Bobby Aug 21 '14 at 14:14
  • What are you using to make the drawings? – Chase Ernst Aug 21 '14 at 14:14
  • What is the input? Image? or you have some other representation as obejcts? – Sriram Sakthivel Aug 21 '14 at 14:15
  • 1
    How much work do you want to put into this? What do you know about the images and how they will vary, esp. in terms of the number of colors? Besides looking for a library you'll have to scan the raster of pixels to find a list of changes and then work throught those changes to identify those that are connected.. – TaW Aug 21 '14 at 14:26
  • Thanks for all the comments! Chase Ernst: I'm using c# with unity to generate a texture2d. Sriram Sakthivel The input image are basically two different images substracted from another to figure out the differences. But I want to basically work with the spots further on and try to figure out a way to get the frame around them. – Jan Rabe Aug 21 '14 at 14:39
  • 1
    As you stated, your first task is to determine your algorithm for defining a contiguous image (a blob). Once you have that, determine the size of margin (2px, 3px, etc) you want around the blob. Then the remaining question is what technology do you want to use? Are you using WinForm or WPF? Both have different drawing libraries and I would highly recommend against GDI for WinForms. You also have the option of drawing with OpenGL ...and doesn't Unity have a drawing library? – IAbstract Aug 21 '14 at 14:50

2 Answers2

3

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
zeFrenchy
  • 6,541
  • 1
  • 27
  • 36
  • this is what i end up with thanks :) for everyone else who is curious: https://github.com/kibotu/net.kibotu.sandbox.unity.spotthedifference/blob/master/unity/Assets/Sources/FloodFill.cs – Jan Rabe Aug 22 '14 at 12:47
1

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.stack.imgur.com/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

Community
  • 1
  • 1
Margus
  • 19,694
  • 14
  • 55
  • 103
  • This might be useful http://mathematica.stackexchange.com/questions/19516/load-a-mathematica-package-via-net-code – Margus Aug 21 '14 at 15:24
  • well i'm sure this works just fine however for this single task i don't think it's appriate to include the entire wolfram library into unity ^^' but thanks for recommending this though i might have further use for it in the future :) – Jan Rabe Aug 22 '14 at 12:46