1

I'm trying to figure out the best way to approach this. I'm looking to take an UIImage, detect if there are any shapes/blobs of a specific RGB color, find their frame and crop them into their own image. I've seen a few posts of people recommending OpenCV as well as other links similar to this - Link

Here are 2 screenshot's of what I'm looking to do. So in Example 1 there is the light blue rectangle with some text inside it. I need to detect the blue background and crop the image along the black lines. Same for the red image below it. This is just showing that it doesn't matter what's inside of the color blob. Example 2 shows the actual images that will be cropped once the 2 color blobs are found and cropped. All image will always be on a white background.

Example 1 Example 2

Community
  • 1
  • 1

1 Answers1

1

This question goes way beyond a simple answer. What you will need to do is access the raw data on that image based on the color then create a frame to crop. I would find the upper, left,right, lower frame of all matches of that specific color then make a frame out of it to crop the image.

Access the color Get Pixel color of UIImage

Crop the image Cropping an UIImage

Community
  • 1
  • 1
Mark McCorkle
  • 9,349
  • 2
  • 32
  • 42
  • How do you handle if there are 2 separate shapes that are the same color though? – user3420761 Nov 12 '14 at 22:05
  • The shapes are irrelevant since what you would be doing is testing every pixel and logging the max left,right,top,bottom pixel based on color(or threshold of color such as less than .01R.01G.01B, mostly black) then creating a CGRect(frame) from it which you'll use to crop the original image. I know it sounds confusing but the question does not have an extremely simple answer from what I'm aware of. – Mark McCorkle Nov 12 '14 at 22:08
  • Yeah, I understand but what happens if there is is a shape at (0,0,100,100) and another at (0, 150,100,100) that are both the same color. If I do it the way you're suggesting it will get the frame (0, 0, 250, 100) with 50 px of white space in the middle of the cropped image. Unless I'm misunderstanding you? – user3420761 Nov 12 '14 at 22:16
  • You would store the furthest extremes regardless of shapes or match counts to create your frame. This would naturally always give you a left,right,top,bottom to crop with. It's a matrix of pixels you're wishing to match to. – Mark McCorkle Nov 12 '14 at 22:23