0

I have each character as a Mat object which are of different sizes. Some sample images are,

Sample Image 1 Sample Image 2

I am trying to convert them to an Image using PIL and then into a standard 12x12 matrix which will be flattened into 144 column 1D array. The code I am using is as below, after suggestion

#roi is a Mat Object
images = cv2.resize(roi,(12,12))
myImage = Image.fromarray(images)
withoutReshape = np.array(myImage.getdata()) #Corresponding output attached
print(withoutReshape)
withReshape = np.array(myImage.getdata()).reshape(myImage.size[0], myImage.size[1], 3) 
print(withReshape) #Corresponding output attached

unable to find the significance of using reshape. Also, how can I flatten the matrix into an array after using resize

Link to output files with and without reshape

Link for complete code and source image that I am using

Gladiator
  • 354
  • 3
  • 19
  • What is this shape `(2829, 3)`? It doesn't look like proportions of a regular single character. – Andriy Makukha Jun 15 '18 at 06:12
  • @AndriyMakukha. I have arrived at extracting each character from a larger image and code which was posted in the link, https://stackoverflow.com/questions/50713398/detecting-spaces-between-characters-in-ocr-opencv/50713765?noredirect=1#comment88439062_50713765 As an extension, I am converting each character into a 12x12 matrix. I still could not understand how the shape is `(2829 ,3)` – Gladiator Jun 15 '18 at 06:53

1 Answers1

1

You're confusing ndarray.resize with an image resizing function. That's not how it works. Numpy doesn't know your array is an image and will only resize the array without caring about the content.

You should use OpenCV resize function instead.

images = cv2.resize(images, (12, 12))

Also, you need to reshape your images array to the image dimensions after creating it from the PIL data. Take a look at this question to see how it's done.

Sunreef
  • 4,452
  • 21
  • 33
  • My code after update looks like, `images = cv2.resize(roi_2,(12,12)); myImage = Image.fromarray(images); pix = np.array(myImage.getdata()).flatten()` This gives me a flattened array of length, `12x12x3`. The code when updated as below, `myImage = Image.fromarray(images); pix = np.array(myImage.getdata()).reshape(myImage.size[0], myImage.size[1], 3)` also gives `12x12x3` matrix. What is the significance of `reshape`? – Gladiator Jun 15 '18 at 07:29
  • @BalakrishnanRamasamy Have you looked at the results with and without reshape ? My guess is that without reshape it will be completely wrong even though the shape is right. – Sunreef Jun 15 '18 at 07:34
  • I have updated the question and have attached output files – Gladiator Jun 15 '18 at 07:52
  • @BalakrishnanRamasamy So you're checking what your images look like by reading a text file with the pixel values ? Good luck... – Sunreef Jun 15 '18 at 08:03
  • I have a separate `H2O deep learning` model which is built in `R`. This model is trained to predict characters with a standard dimensioned `pixel values`. Please let me know if you have anything to comment on this if the approach is good? – Gladiator Jun 15 '18 at 08:14