Menu

#231 Make color spaces more explicit, refactor pixel methods etc

2.0
open
None
2012-03-02
2012-02-08
No

I am trying to convert RGB colors to HSV and back to RGB.

I tried both OpenCV's way and Python's way but both seem to be messed up.

OK, lets have a look at RGB color (123,234,45).
Photoshop reports this as (95 degree, 81%, 92%). Because it uses 360 degrees, it would be (95/2, 0.81255, 0.92255) = (48, 206, 234) in OpenCV's HSV.

Here is the diagnostic Python script:
from SimpleCV import *

c1 = (123,234,45)
print "Source color in RGB: " + str(c1)
print

print "Python's way (from SimpleCV's source)"
c2 = colorsys.rgb_to_hsv( c1 )
print (c2[0]
180,c2[1]*255,c2[2])
print "Something totally broken, hue and saturation totally off!!! How could saturation be 0?"
print

print "SimpleCV's way"
px = Image((1,1),colorSpace=ColorSpace.RGB)
print "px colorspace: " + str(px.getColorSpace())
px[0,0] = c1
print px[0,0]
print "Shouldn't it be in RGB space, if the image is in RGB? It prints in BGR space."
print

print "OK, doesn't matter, just convert it to HSV"
px2 = px.toHSV()
print "px2 colorspace: " + str(px2.getColorSpace())
print px2[0,0]
print "Almost good, but why H = 72? It should be 48!"
print

print "OK, try again:"
px3 = Image((1,1),colorSpace=ColorSpace.BGR)
print "px3 colorspace: " + str(px3.getColorSpace())
px3[0,0] = c1
print px3[0,0]
print "now it got it right"
px4 = px3.toHSV()
print "px4 colorspace: " + str(px2.getColorSpace())
print px4[0,0]
print "and the conversion too!"

print "trying converting back to original"
px5 = px4.toRGB()
print px5[0,0]
print "works"
print

print "a way to fix SimpleCV's .hsv function"

This line is missing from SimpleCV!!!

c3 = ( c1[0]/255., c1[1]/255., c1[2]/255. )
c4 = colorsys.rgb_to_hsv( c3 )
c5 = int(round(c4[0]
180)),int(round(c4[1]255)),int(round(c4[2]255))
print c5

So what happens is that:
1. SimpleCV's .hsv function is broken. I think the test must be broken too, as I don't know why it passes. The last part is my way of fixing it.
2. SimpleCV's color model and px[0,0] = tuple behaves very strangely.

Discussion

Anonymous
Anonymous

Add attachments
Cancel