1

I trained an autoencoder model in Keras to generate denoised images given noisy images. The predicted images are stored in the "result" directory, with the individual filenames appended with the "denoised" tag. When I run the code, nothing is stored in the "result" folder. When I ran for an individual image, the final cv2.imwrite throws this error: "TypeError: Expected Ptr<cv::UMat> for argument 'img'". Is there a different way to write the predicted files to the destination folder without using openCV in this case?

import glob
from keras.preprocessing import image

test = glob.glob("data/test1/*.png") #all test data that needs to be denoised
test.sort()
result = "data/result" #the folder that contains predicted denoised images
#load model
model = load_model('autoencoder_model.h5')
model.summary()
#compile the model
model.compile(loss='mean_squared_error', optimizer = RMSprop())

for f in test:
    img = image.load_img(f,grayscale=True,target_size=(128,128),interpolation="nearest")
    img_name = f.split(os.sep)[-1]
    #preprocess the image
    x = image.img_to_array(img)
    x = np.expand_dims(x, axis=0)
    x /= 255 
    #predict on the image
    preds = model.predict(x)
    pred_norm = 255 * (preds - preds.min()) / (preds.max() - preds.min())
    pred_normed = np.array(pred_norm, np.int)    
    cv2.imwrite(os.path.join(result,f[:-4]+'_removed.png'), pred_normed)
shiva
  • 1,177
  • 2
  • 14
  • 31
  • First step to debug would be to verify that the line is even reached. Have you added print()s? – couka Jan 21 '21 at 19:47
  • @couka: I checked the variables, ran the code, the variables are getting stored at every point, except for the cv2.imwrite part. The code runs with no errors, I checked the path, it is good, but once the code runs, it stores nothing. – shiva Jan 21 '21 at 19:52
  • What you want is in this answer of: [Saving a Numpy array as an image](https://stackoverflow.com/questions/902761/saving-a-numpy-array-as-an-image) – Mr. For Example Jan 22 '21 at 02:03

0 Answers0