-
Apr 5th, 2015, 10:13 AM
#1
Thread Starter
Hyperactive Member
How to detect similar colors in a picture?
I was just trying to detect similar colors in a picture.
First I load a picture in a picturebox and then I select some hundreds of similar colors in the picture using the mouse and getpixel api.
In another picturebox (it's empty, so no picture has been loaded), the program sets the pixel to blue if that color has been selected from the original picture.
The problem is I've to select tons of different pixels from the original picture to get a good result. In case that I only select a few ones, then I will see some small blue pixels in the second picturebox.
I remember that I've use a software where you can select just a pixel and the program detects all the similar colors.
How can I do it?
-
Apr 5th, 2015, 10:22 AM
#2
Re: How to detect similar colors in a picture?
I guess it depends on how you define similar colors. There are probably several techniques and a couple of them may be:
-- Take a color and add/subtract a percentage (threshold) from each of the R,G,B components. Any color that falls within that range is 'similar'
-- Similar to #1 above, but using properties of the color: just focusing on the R, G, or B component, not all, or maybe using Hue vs RGB
When messing with colors, there are usually many ways to address the problem. Every solution has pros and cons
-
Apr 6th, 2015, 01:11 AM
#3
Thread Starter
Hyperactive Member
Re: How to detect similar colors in a picture?
Originally Posted by LaVolpe
I guess it depends on how you define similar colors. There are probably several techniques and a couple of them may be:
-- Take a color and add/subtract a percentage (threshold) from each of the R,G,B components. Any color that falls within that range is 'similar'
-- Similar to #1 above, but using properties of the color: just focusing on the R, G, or B component, not all, or maybe using Hue vs RGB
When messing with colors, there are usually many ways to address the problem. Every solution has pros and cons
I don't fully understand what you're trying to explain to me.
So, you suggest that I've to change (add or substract) a percentaje from each of the R,G,B components... so if I've a color, for example 45,123,31. I've to add (or substract) a percentaje... so
45,123,31 is:
And I add 45 units to each of the R,G,B values:
90,168,76
For me both are green colors and I can think both are similars.
So I don't fully understand what you mean.
-
Apr 6th, 2015, 09:00 AM
#4
Re: How to detect similar colors in a picture?
So in your example, any color that falls in that range is 'similar'
Code:
' Values below are the range +/- from the selected color
If R => 45 And R <= 90 Then
If G => 123 And G <= 168 Then
If B => 31 And B <= 76 Then
' similar color to the selected color
End If
End If
End If
If you are already using logic like this, then I don't think I fully understand the question or the logic is too generic and won't work in this case?
Note: If using this logic, I would suggest adding AND subtracting a percentage from the selected color. That way a 'similar' color would match if it was a bit lighter or a bit darker than the selected color
Last edited by LaVolpe; Apr 6th, 2015 at 09:34 AM.
-
Apr 6th, 2015, 09:46 AM
#5
Re: How to detect similar colors in a picture?
I think I have an algorithm somewhere that I used to convert a 32 bit colour image to an 8 bit coloured image. I had to use a nearest match scheme to achieve this as 8 bit images only has 256 colours to choose from as opposed to millions of colours. Would you like me to get that for you?
-
Apr 6th, 2015, 10:15 PM
#6
Re: How to detect similar colors in a picture?
For RGB colors, the easiest way to compare colors is to treat them as three-dimensional vectors. So to calculate similarity, you use a standard distance formula, e.g.:
Code:
Similarity = Sqr( (R2 - R1) ^ 2 + (G2 - G1) ^ 2 + (B2 - B1) ^ 2)
If you only need relative distances, you can omit the Sqr() for a performance boost. Detecting similar colors is very simple:
Code:
If Similarity < Threshold Then
(colors are similar)
Else
(colors are not similar)
End If
The best Threshold value will depend greatly on the source images. Low-quality JPEGs will need a higher threshold than lossless formats like BMP or PNG. It will probably take some experimentation to find the best number. I'd start at 10 (if not using Sqr()) and work your way up from there.
<Begin potentially pedantic comments>
One of the problems with the RGB color space is that color distance is not uniformly perceptual. This is a fancy way of saying that color distance is not a very meaningful measurement for RGB colors.
For example, using the formula above, two dark colors with a similarity of 10 could look pretty much identical. However, two bright colors with a similarity of 10 may look very different.
Scientists have developed a number of color models that are perceptually uniform. These color models are the result of thousands of tests, where participants of all ages and genders would try to match differing colors, and scientists would use the resulting data to figure out exactly how humans perceive color. (Pretty mind-numbing work!) These experiments made it possible to develop color models that correlate very well with human perception.
CIELAB is probably the most famous perceptually uniform color space. If you need very accurate results, I'd recommend using it instead of RGB. Converting between RGB and CIELAB is complicated, but you can find well-known formulas here.
<End pedantry>
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|