0

i am declaring an array that should print both strings and float types in my output of featuring matrix but it's only displaying 1st letter of each What should i change to display my full words output i don't want to have only one words in the output as it can be seen like i want proper table with strings displayed in their respective columns here's my code:

import cv2
import skimage.feature
import numpy as np
import pandas as pd

Image1 = cv2.imread("lab12.tif", 0)
GLCM_Image1 = skimage.feature.greycomatrix(Image1, [1], [0])
# w, h = 4, 7
# feature_matrix = [[0 for x in range(w)] for y in range(h)]
feature_matrix = np.zeros([4, 7],dtype = str)
feature_matrix[0,0] = ""
feature_matrix[0,1] = 'contrast'
feature_matrix[0,2] = 'dissimilarity'
feature_matrix[0,3] = 'homogeneity'
feature_matrix[0,4] = 'energy'
feature_matrix[0,5] = 'correlation'
feature_matrix[0,6] = 'ASM'
feature_matrix[1,0] = 'Image 1'
feature_matrix[2,0] = 'Image 2'
feature_matrix[3,0] = 'Image 3'


print("IMAGE 1 PROPERTIES:")
contrast1 = skimage.feature.greycoprops(GLCM_Image1, prop='contrast')[0][0]
print("Image 1 contrast", contrast1)
feature_matrix[1,1] = str(contrast1)
dissimilarity1 = skimage.feature.greycoprops(GLCM_Image1, prop='dissimilarity')[0][0]
print("Image 1 dissimilarity", dissimilarity1)
feature_matrix[1,2] = str(dissimilarity1)
homogeneity1 = skimage.feature.greycoprops(GLCM_Image1, prop='homogeneity')[0][0]
print("Image 1 homogeneity", homogeneity1)
feature_matrix[1,3] = str(homogeneity1)
energy1 = skimage.feature.greycoprops(GLCM_Image1, "energy")[0][0]
print("Image 1 energy", energy1)
feature_matrix[1,4] = str(energy1)
correlation1 = skimage.feature.greycoprops(GLCM_Image1, "correlation")[0][0]
print("Image 1 correlation", correlation1)
feature_matrix[1,5] = str(correlation1)
ASM1 = skimage.feature.greycoprops(GLCM_Image1, "ASM")[0][0]
print("Image 1 ASM", ASM1)
feature_matrix[1,6] = str(ASM1)

Image2 = cv2.imread("lab12a.tif", 0)
GLCM_Image2 = skimage.feature.greycomatrix(Image2, [1], [0])

print("\nIMAGE 2 PROPERTIES:")
contrast2 = skimage.feature.greycoprops(GLCM_Image2, "contrast")[0][0]
print("Image 2 contrast", contrast2)
feature_matrix[2,1] = str(contrast2)
dissimilarity2 = skimage.feature.greycoprops(GLCM_Image2, "dissimilarity")[0][0]
print("Image 2 dissimilarity", dissimilarity2)
feature_matrix[2,2] = str(dissimilarity2)
homogeneity2 = skimage.feature.greycoprops(GLCM_Image2, "homogeneity")[0][0]
print("Image 2 homogeneity", homogeneity2)
feature_matrix[2,3] = str(homogeneity2)
energy2 = skimage.feature.greycoprops(GLCM_Image2, "energy")[0][0]
print("Image 2 energy", energy2)
feature_matrix[2,4]= str(energy2)
correlation2 = skimage.feature.greycoprops(GLCM_Image2, "correlation")[0][0]
print("Image 2 correlation", correlation2)
feature_matrix[2,5] = str(correlation2)
ASM2 = skimage.feature.greycoprops(GLCM_Image2, "ASM")[0][0]
print("Image 2 ASM", ASM2)
feature_matrix[2,6] = str(ASM2)

Image3 = cv2.imread("lab12(b).tif", 0)
GLCM_Image3 = skimage.feature.greycomatrix(Image3, [1], [0])

print("\nIMAGE 3 PROPERTIES:")
contrast3 = skimage.feature.greycoprops(GLCM_Image3, "contrast")[0][0]
print("Image 3 contrast", contrast3)
feature_matrix[3,1] = str(contrast3)
dissimilarity3 = skimage.feature.greycoprops(GLCM_Image3, "dissimilarity")[0][0]
print("Image 3 dissimilarity", dissimilarity3)
feature_matrix[3,2] = str(dissimilarity3)
homogeneity3 = skimage.feature.greycoprops(GLCM_Image3, "homogeneity")[0][0]
print("Image 3 homogeneity", homogeneity3)
feature_matrix[3,3] = str(homogeneity3)
energy3 = skimage.feature.greycoprops(GLCM_Image3, "energy")[0][0]
print("Image 3 energy", energy3)
feature_matrix[3,4] = str(energy3)
correlation3 = skimage.feature.greycoprops(GLCM_Image3, "correlation")[0][0]
print("Image 3 correlation", correlation3)
feature_matrix[3,5] = str(correlation3)
ASM3 = skimage.feature.greycoprops(GLCM_Image3, "ASM")[0][0]
print("Image 3 ASM", ASM3)
feature_matrix[3,6] = str(ASM3)

print("\nFeature Matrix:")
pd.options.display.float_format = "{:,.2f}".format
print(pd.DataFrame(data=feature_matrix[1:,1:],
                  index=feature_matrix[1:,0],
                  columns=feature_matrix[0,1:]))

here's my output: output

mawall
  • 171
  • 3
  • 10
Nabia Salman
  • 552
  • 1
  • 8
  • 29
  • I think you need to use `dtype = object` instead of `dtype = str` at creating the numpy array. For more details, have a look at: https://stackoverflow.com/questions/14639496/how-to-create-a-numpy-array-of-arbitrary-length-strings. So it will be `feature_matrix = np.zeros([4, 7],dtype = object)` – KlopDesign May 19 '20 at 19:39
  • Please have a look at this answer: https://stackoverflow.com/questions/11309739/store-different-datatypes-in-one-numpy-array – mawall May 21 '20 at 09:48

1 Answers1

1

Numpy is a package for scientific computing and most useful to manipulate matrices. If you create the feature array only to print to the console, it would be much easier to use a pandas dataframe or python lists.

That being said: Your numpy array has a dtype of <U1. This is a Unicode string of length one. So it is effectively a character array, which is why it will only store the first character of every string you assign.

Numpy structured arrays are intended to hold values of different datatypes. But you could also use dtype object to store both floats and arbitrary long strings in the same matrix:

np.full((4,7), 0, dtype=np.object)

Alternatively you can specify the maximum length string you will need: dtype='<U256' specifies that strings of up to 256 characters can be stored for example.

Your code example is very long and most lines are not immediately relevant to the problem. It is better to only show the minimum of code necessary to reproduce the problem. This will also help you understand the problem and narrow down where the bug is.

mawall
  • 171
  • 3
  • 10