Search code examples
pythonopencvglob

Image serial is not maintaining in OpenCV


Using OpenCV I have read all the images of a folder then converted it into grayscale and then I have saved all the converted images to a new folder which name is "binary image". But when I again read all the images from the "binary image" folder to show histogram then the serial of images in "binary image" folder is not maintaining. Instead of image number 12th, the histogram is showing is 19th number image in the folder. But the total number of histograms are good as it is supposed to be. How can I maintain serial of images?

How i read images:

images1=[cv2.imread(file) for file in glob.glob(r"C:\Users\USER\Handcrafted dataset\binary_image/*.jpg")]

thats how i save histogram image:

 dir1=r"C:\Users\USER\Handcrafted dataset\histogram"
for i,img in enumerate(images1):
    plt.figure(figsize=(5,5))
    plt.hist(img.ravel(),256,[0,256],label=str(np.mean(img)))
    plt.legend(loc="upper right")
    plt.savefig(dir1+"\\"+str(i)+".png")
    print(i)

    plt.show()

to save gray scale image:

di=r"C:\Users\USER\Handcrafted dataset\binary_image"
for i,img in enumerate(images):
    img = rgb2gray(img)
    plt.figure(figsize=(5,5))
    print(i)
    plt.imshow(img ,cmap='gray')
    io.imsave(di+"\\"+str(i)+".jpg",img)

at first when I read images then the image name was random but after converting into grayscale I have saved the images in "binary image" folder as renaming from 0 to 51. That means 0.jpg, 1.jpg serially. After that to show histogram I have used the binay_image folder where images were sorted as I mentioned before. but histogram is showing randomly.


Solution

  • ok, here's a problem. When you use glob.glob() file names come in no specific order. That is, unless you sort them yourself or do something about the order, you cannot assume the file 001.jpg coming before 002.jpg.

    So, when you enumerate(images1) -- the results will be like you described. The numbers and file names won't match.

    What you can do, the simplest is to add sorted() to the file list:

    images1=[cv2.imread(file) for file in sorted(glob.glob(r"C:\Users\..."))]
    

    This however assumes your files are properly numbered without files missing in between, like 006 and then 010 with 007, 008 and 009 missing.


    UPDATE: once I saw the file names: 0.jpg, 1.jpg I immediately know what the problem is. File names are sorted lexicographically, that is, 1, 10, 11, 12, ... 2, 20, 21, 22 -- I hope you catch my drift...

    When you name your files, please, add leading zeros, they are there for a reason, to keep your files in the order, instead of:

    plt.savefig(dir1+"\\"+str(i)+".png")
    

    use:

    plt.savefig(os.path.join( dir1, "%06d.png" % i))