How to convert a csv dataset to a numpy array readable by opencv?

Question:

I have a csv dataset namely sign language mnist dataset. It contains information of 28×28 images with its pixel information. I would like to convert this dataset into a numpy array that can be read by opencv3. A numpy array where i can manipulate through opencv. I would like to apply histogram of oriented gradients into this dataset through opencv.

I have already successfully converted it into a numpy array and was able to isolate a row and reshape it into a 28×28 array. The row has a label at the beginning and i have also split it with only the 28×28 pixel data. I have used matplotlib to successfully plot it however I can’t seem to use cv2.imshow() on the numpy array. I also know that opencv can only read certain datatypes and i have tried converting my data numpy array into both int32 and float but it still didnt work.

Here is how the CSV file looks like:
The CSV file showing the first 4 rows

Here is the file:
Sign Language CSV dataset

Site Where I got the Dataset[Kaggle.com – Sign Language Mnist]

The 1st column is the label for the image and the rest are the pixels. It goes up to the pixel 784 column.

Here is my code:

import cv2

data = np.genfromtxt('sign_mnist_test.csv', delimiter=',', skip_header=1)

labels = data[1:, 0].astype(np.float)
value1 = data[0, 1:].astype(np.float) #the 1st row of the dataset

reshaped = np.reshape(value1, (28, 28))

cv2.imshow('image', reshaped)
cv2.waitKey(0)
cv2.destroyAllWindows()

Here is how my numpy array for the 1st row looks like:

Numpy array of the 1st row

Here is the output:

Output Image

I expect it to show a 28×28 training image(a hand image) however it only shows a plain white 28×28 image with no features.

plt.imshow(reshaped, cmap="Greys")
plt.show()

Output using matplotlib

I am using PyCharm as IDE.

I am also looking for alternative options so that i can use my dataset for openCV if there is any solution that is better.

Asked By: blinkThrice

||

Answers:

The problem is that you have made it a float on this line:

value1 = data[0, 1:].astype(np.float)

You need to preferably pass an np.uint8 to cv2.imshow().

Answered By: Mark Setchell

I used resize instead of reshape

data = pd.read_csv("sign_mnist_train/sign_mnist_train.csv") # images of 28x28 
labels = data.label
data = data.drop(columns=["label"])
value1 = data.iloc[0].astype(np.uint8) 
reshaped = np.resize(value1, (28, 28))
cv2.imshow('image', reshaped)
cv2.waitKey(0)
cv2.destroyAllWindows()