473,785 Members | 2,300 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Finding Nearest Colour



Hi

A problem with trying to find the nearest colour. I have an image
which I use GetPixel to return the colour of various points.
However .... not all colours returned will be a System/Known/Pre-
defined colour. So they wont have corresponding names - this is a
problem because my app needs to return the colour name of each pixel.
So I have looked at GetNearestColor but this just seems to return the
original color I passed it in the first place!?

Any pointers would be appreciated.

Thanks
Tad
Jan 9 '08 #1
5 2561
Jeremy Cowles wrote:
Dim dist As Integer

dist = Math.Abs(CInt(c .R) - known.R) _
+ Math.Abs(CInt(c .G) - known.G) _
+ Math.Abs(CInt(c .B) - known.B)
Wouldn't that be better measured as the root mean square distance?

<snip>
Dim known As Color
Dim dist As Double
For Each colorName As String In _
[Enum].GetNames(GetTy pe(KnownColor))
Color = Color.FromName( colorName)

dist=math.sqrt( (CInt(c.R) - known.R)^2 + (CInt(c.G) - known.G)^2 +
(CInt(c.B) - known.B)^2))

If best.Name Is Nothing OrElse dist < best.Distance Then
best.Color = known
best.Name = colorName
best.Distance = dist
End If
Next
<snip>

(OK, so not any need to take the square root; just use the square of the
distance. The comparisons remain valid.)

Andrew
Jan 9 '08 #2
On Jan 9, 9:18 am, "Andrew Morton" <a...@in-press.co.uk.inv alid>
wrote:
Jeremy Cowles wrote:
Dim dist As Integer
dist = Math.Abs(CInt(c .R) - known.R) _
+ Math.Abs(CInt(c .G) - known.G) _
+ Math.Abs(CInt(c .B) - known.B)

Wouldn't that be better measured as the root mean square distance?
Andrew
Yes, the RMS method is definitely a better match - good suggestion. I
ran several tests, and consistently about 160 out of 1000 samples
(~16%) are more accurate with RMS. So I guess it depends on weather
you want performance or accuracy, although, the performance gain is
probably going to be negligible.

Jeremy
Jan 10 '08 #3
On Jan 10, 12:49 pm, Jeremy Cowles <jeremy.cow...@ gmail.comwrote:
On Jan 9, 9:18 am, "Andrew Morton" <a...@in-press.co.uk.inv alid>
wrote:
Jeremy Cowles wrote:
Dim dist As Integer
dist = Math.Abs(CInt(c .R) - known.R) _
+ Math.Abs(CInt(c .G) - known.G) _
+ Math.Abs(CInt(c .B) - known.B)
Wouldn't that be better measured as the root mean square distance?
Andrew

Yes, the RMS method is definitely a better match - good suggestion. I
ran several tests, and consistently about 160 out of 1000 samples
(~16%) are more accurate with RMS. So I guess it depends on weather
you want performance or accuracy, although, the performance gain is
probably going to be negligible.

Jeremy
One thing, I think your RMS calculation is missing something:

dist=math.sqrt( (CInt(c.R) - known.R)^2 + (CInt(c.G) - known.G)^2 +
(CInt(c.B) - known.B)^2))
Shouldn't it be:

dist=math.sqrt(
((CInt(c.R) - known.R)^2 +
(CInt(c.G) - known.G)^2 +
(CInt(c.B) - known.B)^2))
/ 3
)
?
Jan 10 '08 #4
Jeremy Cowles wrote:
One thing, I think your RMS calculation is missing something:
Shouldn't it be:

dist=math.sqrt(
((CInt(c.R) - known.R)^2 +
(CInt(c.G) - known.G)^2 +
(CInt(c.B) - known.B)^2))
/ 3
)
Hey, it was only wrong by a factor of 1.7-ish :-)

Dim known As Color
Dim dist As Double ' temporary distance measure
For Each colorName As String In _
[Enum].GetNames(GetTy pe(KnownColor))
Color = Color.FromName( colorName)

dist=(CInt(c.R) - known.R)^2 + (CInt(c.G) - known.G)^2 + (CInt(c.B) -
known.B)^2)

If best.Name Is Nothing OrElse dist < best.Distance Then
best.Color = known
best.Name = colorName
best.Distance = Math.Sqrt(dist/3)
End If
Next

Also, the OP may want to consider the idea of a "nearest" colour in a
different colour-space, for example the hue may be more important than the
brightness.

Andrew
Jan 11 '08 #5
Also, the OP may want to consider the idea of a "nearest" colour in a
different colour-space, for example the hue may be more important than the
brightness.

Andrew
Actually, I was thinking the same thing. I created a test with various
combinations, Hue, RGB, Brightness, Saturation, and it didn't seem to
help much. I think it is because there is such large gaps between the
known colors that it can only get so accurate. Maybe if this were a
different application it would have a bigger impact (i.e. using more
colors than the "known" colors).

Jeremy
Jan 11 '08 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
11510
by: John Hunter | last post by:
I have a list of two tuples containing x and y coord (x0, y0) (x1, y1) ... (xn, yn) Given a new point x,y, I would like to find the point in the list closest to x,y. I have to do this a lot, in an inner loop, and then I add each new point x,y to the list. I know the range of x and y in
4
6797
by: tertius | last post by:
Hi, I'm trying to round my float total to the nearest .05 cents. 12.01 should produce 12.00 0.14 should produce 0.10 2.28 " 2.25 703.81 " 703.80 "%.02f"%100.0099 produces 100.01 (which I know is right)
2
2631
by: nkunapa | last post by:
Hi: Is there a way in XPATH to find the nearest node of the node in context with a certain attribute value. Here is my problem. I have the following XML and I am trying to add all the nodes with attribute value LNum=1 as child nodes of the nearest node above it with attribute LNum=0....and add all the nodes with attribute value LNum=2 as child nodes of the nearest node above it with attribute LNum=1 and so on. The LNum value can go...
6
5238
by: Louise | last post by:
Hi I have written an HTML pages which does not have any colour specifying tags as far I know. When I view this in an Microsoft internet explorer browser it appears with a white background and black text but when I change Windows start menu->settings->control panel ->display -> appearance and change scheme to 'High Contrast Black' the background in the browser changes to black and the text to white. I understand that the windows scheme...
8
2848
by: Gompie | last post by:
Why does the function DMin("Abs(!- #" & & "#)";"SomeTable") not work properly with datefields. It always finds the closest difference with a later date only, if an earlier date in the table is closer or even equal, it still comes up with the later date. It works fine with numeric fields. (of course without the number signs) Anybody knows why?
20
3170
by: Chor Lit | last post by:
Hi, I asked Bjarne Stroustrup about the idea of adding colour standard for C++, and he said that it is very difficult for compiler vendors to change their IDE. But do you think it is possible ? Note that the proposed colour standard is not just merely to ease the eye only as what presently is in C++ compilers, but to aid in syntax disambiguation and other advantages. Here are a few advantages that I can think of:
6
2710
Robbie
by: Robbie | last post by:
Hi. I've made 2 functions which play around with colours. They convert a 'colour number' (I don't know what the proper name for it is, so I call it this - the Long given back by RGB(), object.BackColor, etc, which represents a value of red, green and blue) into another 'colour number' but this is a greyscale version of it. There are 2 functions - the first one 'decodes' the 'colour number' into the separate R G and B values. The second one...
42
3831
by: aarklon | last post by:
Hi all, see:- http://mathforum.org/library/drmath/view/54456.html
4
1326
by: harryos | last post by:
hi i am reading thru a book on digitizing text lines using c code.They use a font data file containing unsigned char fonts with elements like 0x00,0x7e,0x81 etc to represent each character .After a text line is scanned 16 times the equivalent codes are stored in an unsigned char bitImage. Now the digitized line stored in bitImage is taken 1 character at a time(ie 8 bits) and color of each is to be determined whether black or white. The...
0
9489
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
10101
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9959
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8988
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7509
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5396
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5528
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4063
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2893
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.