Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
92 views
in Technique[技术] by (71.8m points)

python - binarization an image grayscale

i am new in python , my probleme it's about edit some changes in an image grayscale , i wanna make a binarization for this image , the values of pixels bigger then 100 take the value 1 (white), and the values low than 100 takes the value 0 (black) so any suggestion plz (sorry for my bad english)

my code :

`import numpy as np import cv2

image = cv2.imread('Image3.png', 0)




dimension = image.shape
height = dimension[0]
width = dimension[1]

#finalimage = np.zeros((height, width))
for i in  range(height) :
    for j in  range(width):
        
        if (image[i, j] > 100):
            image[i][j] = [1]  
        else:
            image[i][j] = [0]

cv2.imshow('binarizedImage',image)
cv2.waitKey(0)
cv2.destroyAllWindows()
question from:https://stackoverflow.com/questions/65927513/binarization-an-image-grayscale

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I think you just want to use np.where():

import numpy as np
image = np.array([[200, 50, 200],[50, 50 ,50],[10, 255, 10]]) #this will be your image instead
In [11]: image
Out[11]: 
array([[200,  50, 200],
       [ 50,  50,  50],
       [ 10, 255,  10]])

In [12]: np.where(image > 100, 1, 0)
Out[12]: 
array([[1, 0, 1],
       [0, 0, 0],
       [0, 1, 0]])

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...