Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

convert image (np.array) to binary image

Tags:

python

numpy

Thank you for reading my question.

I am new to python and became interested in scipy. I am trying to figure out how I can make the image of the Racoon (in scipy misc) to a binary one (black, white). This is not taught in the scipy-lecture tutorial.

This is so far my code:

%matplotlib inline
import matplotlib.pyplot as plt
import numpy as np 
from scipy import misc  #here is how you get the racoon image

face = misc.face()
image = misc.face(gray=True)
plt.imshow(image, cmap=plt.cm.gray)
print image.shape 

def binary_racoon(image, lowerthreshold, upperthreshold):
    img = image.copy()
    shape = np.shape(img)

    for i in range(shape[1]):
        for j in range(shape[0]): 
             if img[i,j] < lowerthreshold and img[i,j] > upperthreshold:
                 #then assign black to the pixel
             else:
                 #then assign white to the pixel

    return img

    convertedpicture = binary_racoon(image, 80, 100) 
    plt.imshow(convertedpicture, cmap=plt.cm.gist_gray)

I have seen other people using OpenCV to make a picture binary, but I am wondering how I can do it in this way by looping over the pixels? I have no idea what value to give to the upper and lower threshold, so I made a guess of 80 and 100. Is there also a way to determine this?

like image 488
User12049279432 Avatar asked Mar 10 '23 18:03

User12049279432


2 Answers

In case anyone else is looking for a quick minimal example to experiment with, here's what I used to binarize an image:

from scipy.misc import imread, imsave

# read in image as 8 bit grayscale
img = imread('cat.jpg', mode='L')

# specify a threshold 0-255
threshold = 150

# make all pixels < threshold black
binarized = 1.0 * (img > threshold)

# save the binarized image
imsave('binarized.jpg', binarized)

Input:

enter image description here

Output:

enter image description here

like image 50
duhaime Avatar answered Mar 21 '23 00:03

duhaime


You're overthinking this:

def to_binary(img, lower, upper):
    return (lower < img) & (img < upper)

In numpy, the comparison operators apply over the whole array elementwise. Note that you have to use & instead of and to combine the booleans, since python does not allow numpy to overload and

like image 25
Eric Avatar answered Mar 21 '23 01:03

Eric