Error reshaping an image | Valueerror: cannot reshape array of size 72000 into Shape (1.24000)

Asked

Viewed 1,847 times

2

I’m trying to use Python with Opencv to then make a classification. However, at the time I will make popular the object, I have the following error:

Valueerror: cannot reshape array of size 72000 into Shape (1,24000)

I understand it is an array size error, but what I am doing wrong when creating the array?

Follow the full code:

import sys
import numpy as np
import cv2
import os    

MIN_CONTOUR_AREA = 100    
RESIZED_IMAGE_WIDTH = 200
RESIZED_IMAGE_HEIGHT = 120

def main():

    npaFlattenedImages =  np.empty((0, RESIZED_IMAGE_WIDTH * RESIZED_IMAGE_HEIGHT))        
    intValidChars = [0,1,2,3]    

    for npaContour in intValidChars:

        name = "characters/MANDATOR-42-"+str(npaContour)+".png";
        print(name)            
        imgTrainingNumbers = cv2.imread(name)                
        imgROIResized = cv2.resize(imgTrainingNumbers, (RESIZED_IMAGE_WIDTH, RESIZED_IMAGE_HEIGHT))             
        x,y,z = imgTrainingNumbers.shape        
        print("Shape: " + str(x) +"|"+  str(y) + "|" + str(z)) 

        if(imgTrainingNumbers is None):
            print("Not Found")      

        cv2.imshow("Imagem", imgROIResized)    
        intChar = cv2.waitKey(0)            
        intClassifications = []            
        intClassifications.append(int(chr(intChar)))    
        npaFlattenedImage = imgROIResized.reshape((1,RESIZED_IMAGE_WIDTH * RESIZED_IMAGE_HEIGHT))
        npaFlattenedImages = np.append(npaFlattenedImages, npaFlattenedImage, 0)   

    # end for    

    fltClassifications = np.array(intClassifications, np.float32)                  
    npaClassifications = fltClassifications.reshape((fltClassifications.size, 1))       
    print ("training complete")    
    np.savetxt("classifications__.txt", npaClassifications)           
    np.savetxt("flattened_images__.txt", npaFlattenedImages)              
    cv2.destroyAllWindows()    

    return

###################################################################################################
if __name__ == "__main__":
    main()
# end if
  • Play the whole error message, so it is easier to try to help. Detail: you are mixing camelCase with snake_case, the convention in pyhthon is lower_snake_case. :-)

1 answer

2

Your image array has 72000 positions and you want to turn it into a 200x120 array, which totals 24000 positions. But why 72000 positions, being that its image is 200x120?

If your image has 200x120 pixels, you should not forget that it still has, for each pixel, 3 color channels, B (blue), G (green) and R (red). Each pixel is formed by 3 positions in its array. So, your array has to have the 200x120 and even more a dimension of 3, which are the color channels, ie: 200x120x3.

When you use cv2.imread()' you get a return with the following dimensions: height (in this case 200), width (in this case 120), channels (in this case 3).

So when you’re working with color images, don’t forget the color channels. If you want to use a grayscale image, which only occupies one color channel, you have to convert it first. For this, you can use:

imagem_cinza = cv2.cvtColor(image_colorida, cv2.COLOR_BGR2GRAY)

And so you will have an image of dimensions 200x120x1.

That being said, just tailor your code to work with the images considering the color channels.

  • Dude thanks, it was the same channel problem, I used the imagem_gray = cv2.cvtColor(colorimage_, cv2.COLOR_BGR2GRAY) and it worked thanks for the help

  • Mark the V of the answer to indicate that it has been resolved to help those who come here with the same problem as you in the future.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.