Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to match cv2.imread to the keras image.img_load output

Tags:

I'm studying deep learning. Trained an image classification algorithm. The problem is, however, that to train images I used:

test_image = image.load_img('some.png', target_size = (64, 64))
test_image = image.img_to_array(test_image)

While for actual application I use:

test_image = cv2.imread('trick.png')
test_image = cv2.resize(test_image, (64, 64))

But I found that those give a different ndarray (different data):

Last entries from load_image:

  [ 64.  71.  66.]
  [ 64.  71.  66.]
  [ 62.  69.  67.]]]

Last entries from cv2.imread:

  [ 15  23  27]
  [ 16  24  28]
  [ 14  24  28]]]

, so the system is not working. Is there a way to match results of one to another?

like image 926
wasd Avatar asked Jun 07 '18 16:06

wasd


People also ask

What is cv2 Imread ()?

cv2. imread() method loads an image from the specified file. If the image cannot be read (because of missing file, improper permissions, unsupported or invalid format) then this method returns an empty matrix. Syntax: cv2.imread(path, flag)

What will the cv2 Imread () return if a wrong image name is passed as argument?

Typically, the cv2. imread function will return None if the path to the input image is invalid, so be sure to double-check and triple-check your input image paths!

Can cv2 read JPEG?

imread does not read jpg files.


1 Answers

OpenCV reads images in BGR format whereas in keras, it is represented in RGB. To get the OpenCV version to correspond to the order we expect (RGB), simply reverse the channels:

test_image = cv2.imread('trick.png')
test_image = cv2.resize(test_image, (64, 64))
test_image = test_image[...,::-1] # Added

The last line reverses the channels to be in RGB order. You can then feed this into your keras model.

Another point I'd like to add is that cv2.imread usually reads in images in uint8 precision. Examining the output of your keras loaded image, you can see that the data is in floating point precision so you may also want to convert to a floating-point representation, such as float32:

import numpy as np
# ...
# ...
test_image = test_image[...,::-1].astype(np.float32)

As a final point, depending on how you trained your model it's usually customary to normalize the image pixel values to a [0,1] range. If you did this with your keras model, make sure you divide your values by 255 in your image read in through OpenCV:

import numpy as np
# ...
# ...
test_image = (test_image[...,::-1].astype(np.float32)) / 255.0
like image 82
rayryeng Avatar answered Dec 02 '22 16:12

rayryeng