# How to generate black white image based on HSV

Hi, everybody. In my project I need to generate a black and white picture from a picture based on its HSV values. What I do right now is convert the image to HSV then loop through each pixel to check the pixel's HSV. If it's in the desired range, then the pixel will be white in the result image. Otherwise it will be black. Here is my current code:

 from SimpleCV import Image, Camera

WHITE = (255.0, 255.0, 255.0)
BLACK = (0.0, 0.0, 0.0)

cam = Camera(0, {'width' : 160, 'height' : 120})

img = cam.getImage()
hsvImg = img.toHSV()

hMin, hMax, sMin, sMax, vMin, vMax = 0, 3, 0, 3, 229, 235

for i in range(img.height):
for j in range(img.width):
h, s, v = hsvImg[j,i]
if (hMin &lt;= h &lt;= hMax) and
(sMin &lt;= s &lt;= sMax) and
(vMin &lt;= v &lt;= vMax) :
img[j,i] = WHITE
else :
img[j,i] = BLACK


The above code works but it takes too long, around 2 minutes, for a 160x120 image and it eats up 100% CPU power (I use raspberry pi). Is there a faster way to do this? Thanks before.

edit retag close merge delete

Sort by ยป oldest newest most voted

Hey, this code takes about 1.5 s to run on a modern laptop and I believe the code is as concise as possible.

However, there are definitely ways to speed up the process, IF there are properties about the image that you know about.

For e.g., if your background has the same color, then you can decide before, if you want the background to be black or white. This will save computer time, because it reduces the number of instruction from 6 to 1.

Or for e.g., if you know that your image will have a higher number of blacks than white, use the conditional statement in a way that can short circuit with the 6 conditions.

--> instead of: if (a == b AND a == c).... , --> do: if (a != b || ...

more

Hi, thanks for your reply. I already modified my code based on your suggestion but right now the Raspberry Pi is with my friend. I will write a comment here regarding the result once I've tried it using Raspberry Pi.

( 2014-04-02 10:54:00 -0500 )edit

It took around 50% less that using improvement you suggested. Thanks :)

( 2014-04-04 09:13:13 -0500 )edit

Here is a much simpler and faster way to do it using numpy for the heavy lifting, it ran at 12ms on my machine.

from SimpleCV import Image

def bin(img):
numpyImg = img.getNumpyCv2()
hsvValues = img.toHSV().getNumpyCv2()
hValues = hsvValues[:,:,0] # slice off Hue values
hueThreshold = 50 #If above this value make it black
black = (0,0,0) #RGB black, this could be whatever color you want
numpyImg[hValues &lt; hueThreshold] = black   #if value less than threshold set to black
img = Image(numpyImg)
return img

image = Image('http://upload.wikimedia.org/wikipedia/commons/5/51/DSHwiki.jpg') #picture of dog
img = bin(image)
img.show()

more

Thank you for your reply. I've tried your code in my laptop and what it does is convert any pixel with h value less than treshold to black, right? What I want to do is make any pixel with H 0-3, S 0-3, and V 229-235 white, otherwise it will be black.

( 2014-04-02 10:30:46 -0500 )edit