William Falcon
William Falcon

Reputation: 9823

Most prevalent color on a background by changing color space

I have a sheet of paper on another surface. I want to figure out when the paper ends and the surface begins.

I'm using this approach: 1. Convert image to pixel array 2. Pick 3 random 20x20 squares and frequency count the colors 3. The highest frequency is the background

However, the problem is that I get over 100 colors every time I do this on an actual image taken by the camera.

I think I can fix it by putting the image in 16 colors palette. Is there a way to do this change on a UIImage or CGImage?

Thanks

Upvotes: 0

Views: 84

Answers (1)

user189804
user189804

Reputation:

Your colours are probably very close together. How about calculating the distance (the cumulative absolute difference between red, green and blue values) from each sampled colour to a reference colour - just use the first one you sample as reference. If the distance is large, you have a different colour. If the distance is small, you have the same colour with minor variations in lighting or other camera artefacts.

Basically this is applying a filter in a very simple manner. It is up to you to decide how big the difference has to be for the colours to be considered different, but you could decide that by looking at the median difference of all the colours and grouping them into over/under samples.

You might also get good results from applying a Core Image filter to the sample images, such as CIColorClamp (CISpotColor looks better but is OS X only). if you can find a suitable filter there is a good chance it will be simpler and faster than doing it yourself.

Upvotes: 1

Related Questions