Ask Your Question
0

Reading in images, typeError: mat is not numpy nor scalar

asked 2018-02-23 17:08:09 -0600

AcrimoniousMirth gravatar image

updated 2018-02-23 17:28:00 -0600

So as part of a larger project here I've got this function meant to iterate through a directory of images and then perform some operations on them, mainly the function imageToPoints which is currently commented out for testing purposes but which basically runs a findContour, extracts the points and appends them to a list.

So as it currently stands the code is meant to read in one image at a time, print its name (which it does) then display the image (which it doesn't) suggesting the image isn't being read correctly. I figure I need to include cv.imread somehow but not sure exactly how.

def imagesToList():
# Iterates through directory and processes each image into array

   for img in os.listdir(DIRECTORY + 'TestPhotos/'):
    # Filters out any rogue files (e.g. hidden)
    if img.endswith(".png"):
        camNum, imgNum = os.path.splitext(img)[0].split('img')
        # print 'Camera is ', camNum
        # print 'Image is ', imgNum

        # Correction of values will be different for cams 1 & 2
        if camNum == 1:
            # Correction values here

            zVal = imgNum*numMoves
            #imageToPoints(img)
            print img
            cv.imshow('image',img)

        else: #camNum == 2:
            # Correction values here

            zVal = imgNum*numMoves
            #imageToPoints(img)
            print img
            cv.imshow('image',img)

        #else: print '''not of format [x]img[y]'''

Returns:

2img1.png
 Traceback (most recent call last):
   File "I2LTEST5.py", line 81, in <module>
    imagesToList()
  File "I2LTEST5.py", line 76, in imagesToList
    cv.imshow('image',img)
TypeError: mat is not a numpy array, neither a scalar

Which suggests to me that the image isn't being read in correctly.

Many thanks for your help and if you're willing to help a university student finish his honours project then your help at my GitHub would be really appreciated!

edit retag flag offensive close merge delete

1 answer

Sort by » oldest newest most voted
0

answered 2018-02-24 01:51:12 -0600

berak gravatar image

updated 2018-02-24 04:46:20 -0600

you don't have an image, all you have there is the name of an image. what's missing is:

image = cv.imread(imgpath)  # you have to read it from disc first !

if (image == None): 
      print(imgpath, "could not be read!")
      return # can't go on with an invalid image
cv.imshow("image", image)
cv.waitKey()
edit flag offensive delete link more

Comments

Hi @berak. Yes, I assumed so. I’m not sure which layer of loop to put it in to ensure it’s reading the correct image. Should it be after if img.endswith and be image = cv.imread(img)? What I think is happening here is that I’ll end up with 2 values for the image, one being img, the name of the image containing and image, the actual image.

AcrimoniousMirth gravatar imageAcrimoniousMirth ( 2018-02-24 02:47:55 -0600 )edit

"Should it be after if img.endswith and be image = cv.imread(img)" -- yes.

"What I think is happening here is that I’ll end up with 2 values" -- well, that's unfortunate naming, but again -- img is the path on disk, and image is the actual numpy array needed for processing / visualization

berak gravatar imageberak ( 2018-02-24 02:51:40 -0600 )edit

@berak okay, I gave that a shot, inserted imread into the loop and changed imshow to read image instead of img and ended up with this error: cv2.error: /home/pi/opencv-3.2.0/modules/highgui/src/window.cpp:304: error: (-215) size.width>0 && size.height>0 in function imshow

AcrimoniousMirth gravatar imageAcrimoniousMirth ( 2018-02-24 04:42:26 -0600 )edit

well, you obviously did NOT add the checking part...

berak gravatar imageberak ( 2018-02-24 04:45:45 -0600 )edit

@berak my bad, I didn't think I'd have to yet because the images are all of the same type, same name format and all have been read before in previous tests. I added as you suggested and got back ('/home/pi/Desktop/ScannerDev/TestPhotos/2img1.png', 'could not be read!').

AcrimoniousMirth gravatar imageAcrimoniousMirth ( 2018-02-24 06:32:57 -0600 )edit

"'/home/pi/Desktop/ScannerDev/TestPhotos/2img1.png"

_____________________________________^ slash missing here ? did you mean:

'/home/pi/Desktop/ScannerDev/TestPhotos/2/img1.png ?

berak gravatar imageberak ( 2018-02-24 06:37:52 -0600 )edit

@berak no, all the images are of form ximgy.png (or better: %iimg%i.png) and reside in TestPhotos! The readout is correct.

AcrimoniousMirth gravatar imageAcrimoniousMirth ( 2018-02-24 06:49:48 -0600 )edit

this works for me. Thank you! unfortunately I don't have >5 points to upvote

bluepint22 gravatar imagebluepint22 ( 2020-01-16 04:55:28 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2018-02-23 17:08:09 -0600

Seen: 1,919 times

Last updated: Feb 24 '18