Cannot convert to binary image using otsu threshold

最后都变了- 提交于 2020-06-17 14:19:05

问题


I am trying to convert zebra crossing image into binary image through otsu method. At first I have counted threshold value of all the zebra crossing images in the folder and then append it to a list called th_li=[]

import cv2 as cv
from skimage.filters import threshold_otsu

dir3=r"C:\Users\USER\Handcrafted dataset\bw"
images3=[cv2.imread(file,cv2.IMREAD_GRAYSCALE) for file in sorted(glob.glob(r"C:\Users\USER\Handcrafted dataset\adaptive/*.jpg"))]
th_li=[]
for i,img in enumerate(images3):
    thresh = threshold_otsu(img)
    th_li.append(thresh)

After that using a two for-loop I have compared all the pixel value of the image with the corresponding threshold value. If the pixel value is less than the threshold value I have set it 0 otherwise 255. Then I have printed the image array, showing that the array contains only two value 0 or 255. Then saved the image to another folder. Instead of showing binary image image looks like yellowish. But In the folder saved image is binary. Another problem is when I again print the binary image it contains value except 0 and 255

images4=[cv2.imread(file,cv2.IMREAD_GRAYSCALE) for file in sorted(glob.glob(r"C:\Users\USER\Handcrafted dataset\adaptive/*.jpg"))]

for k,img in enumerate(images4):

    for i in range(0,int(img.shape[0])):
        for j in range(0,int(img.shape[1])):
            if img[i][j]<int(th_li[k]):
                img[i][j]=0
            else:
                img[i][j]=255
    print(img)
    plt.figure(figsize=(8,5))
    plt.imshow(img)
    cv2.imwrite(dir3+"\\"+ "%06d.jpg" % k,img)

output

After that when i read the image again it shows that array is not changed it all remains same.

来源:https://stackoverflow.com/questions/62161234/cannot-convert-to-binary-image-using-otsu-threshold

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!