0

I am trying to extract image files from one sub folder "input" (10th line) and process it and then save it into another subfolder "output" (10th last line), but the processed files are not being saved. But if I take all the image files from the same folder where the code is saved (not writing input in the extraction command in 10th line), then when I save it in the subfolder "output", I'm successful. Just do remember the note; in both cases the processed files are being displayed and are the same, just not being saved in one case.

import glob
import cv2
import numpy as np
import matplotlib.pyplot as plt

# initialize data and target as lists
data = []
target = []
# find all bmp files in the current folder
Files = glob.glob("input/*.bmp")

# go through all files and fit contour
for f in Files:
    img = 255-cv2.imread(f,0) #0 for grayscale
    white=cv2.imread("white.bmp")
    # add image to the list
    data.append(img)

    # make lines thicker
    kernel = np.ones((5,5),np.uint8)   
    img = cv2.dilate(img,kernel,iterations = 1)

    # contour application    
    ret,thresh = cv2.threshold(img,127,255,0)
    im2,contours,hierarchy = cv2.findContours(thresh, 1, 2)

    # sort contours by area
    areas = [cv2.contourArea(cnt) for cnt in contours]
    idx = np.argsort(areas)[::-1]
    contours = np.asarray(contours)[idx]

    for cnt in contours:
        (x,y),radius = cv2.minEnclosingCircle(cnt)
        if radius < img.shape[0]-10 and radius > 20:
            cv2.circle(white,(int(x),int(y)),int(radius),0,4)
            break

    plt.imshow(white)
    #save the files to output folder with the name "Image_x"
    filename = "output/Image_%s" %f
    plt.colorbar()
    # live image display
    plt.draw()
    # need to add pause command, otherwise it does not work
    plt.pause(.01)
    # clear the figure to avoid memory issues
    plt.clf()
    #save these contours (outputs) as bmps with same file names
    cv2.imwrite(filename,white)
Areeb Muzaffar
  • 197
  • 2
  • 15
  • Did you try printing the output filename, so you see where you're actually trying to write it? I wouldn't be surprised if it looked something like "output/Image_input/foo.bmp", and the whole thing failing silently trying to write into a non-existent directory (as you don't bother to test the result of `imwrite`). – Dan Mašek Oct 25 '17 at 14:13

2 Answers2

1

Let's do a small experiment.

>>> import os, glob
>>> os.system("tree ./matcher")
matcher
├── matcher2.py
├── matcher.cpp
├── matcher.py
└── question.md

0 directories, 4 files
0
>>> glob.glob("matcher/*.py")
['matcher/matcher2.py', 'matcher/matcher.py']

As you can see, the results of glob.glob("matcher/*.py") contain the root dir "matcher/". That so say, it's wrong to write filename = "output/Image_%s" %f. Change it to filename = "output/" + f.split("/")[-1] or so.

## source bmps in "input/"
## processed bmps to "output"
for f in glob.glob("input/*.bmp"):
    pass
    filename = "output/" + f.split("/")[-1]
    cv2.imwrite(filename, xxx)
Kinght 金
  • 17,681
  • 4
  • 60
  • 74
1

It seems that we need to add the matcher2.py and matcher.py at first. Otherwise, it has the following error.

>>> import os, glob
>>> os.system("tree ./matcher")

sh: 1: tree: not found

32512

Mike Chen
  • 377
  • 3
  • 5