Ask Your Question

How to generate black white image based on HSV

asked 2014-04-01 01:02:47 -0500

andra gravatar image

updated 2014-04-02 09:21:59 -0500

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 <= h <= hMax) and
            (sMin <= s <= sMax) and
            (vMin <= v <= 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 flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted

answered 2014-04-01 21:49:49 -0500

jxieeducation gravatar image

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 || ...

edit flag offensive delete link 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.

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

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

andra gravatar imageandra ( 2014-04-04 09:13:13 -0500 )edit

answered 2014-04-02 09:53:10 -0500

xamox gravatar image

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 < hueThreshold] = black   #if value less than threshold set to black
    img = Image(numpyImg)
    return img

image = Image('') #picture of dog
img = bin(image)
edit flag offensive delete link 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.

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

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower


Asked: 2014-04-01 01:02:47 -0500

Seen: 572 times

Last updated: Apr 02 '14