Ask Your Question
0

TypeError: img is not a numpy array, neither a scalar

asked 2018-01-26 04:38:00 -0600

clara gravatar image

I have data similar to the following :

10-0 = [
    [1915, 387, 1933, 402],
    [3350, 387, 3407, 391],
    [842, 505, 863, 521],
 ]

10-0 being the credentials of an image(to find the path) and the values inside are coordinates of a box or rectangle each, the formula i'm working on is cv2.rectangle(img, (x1, y1), (x2, y2), (255,0,0), 2) , note that 10-0 is the key of one element of a dictionary.

My code :

import cv2
    for key in my_dict:
        folder_blueprint = re.findall(r'\d+', key)
        img = PATH_TO_SOURCE+str(folder_blueprint[0])+'-'+str(folder_blueprint[1])+'.png'
        for line in key:
            line_number = 0
            cv2.rectangle(img,( my_dict[key][line_number][0],my_dict[key][line_number][1]),(my_dict[key][line_number][2],my_dict[key][line_number][3]),(255,0,0),2)
            # cv2.imread(img)
            line_number = line_number + 1
        cv2.imwrite(FULL_PATH_TO_DESTINATION, img)
        cv2.imshow(FULL_PATH_TO_DESTINATION, img)
        k = cv2.waitKey(0)  # 0==wait forever

What i finally want is the image with red boxes around the regions of interest in a new destination folder leaving the original image intact. And i have referred to similar questions on here with the same error message but they weren't helpful to my case.

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
1

answered 2018-01-26 04:42:26 -0600

berak gravatar image

this here:

 img = PATH_TO_SOURCE+str(folder_blueprint[0])+'-'+str(folder_blueprint[1])+'.png'

is the PATH to the image on disk, not an image (in memory) !

try like:

img_path = PATH_TO_SOURCE+str(folder_blueprint[0])+'-'+str(folder_blueprint[1])+'.png'
img = cv2.imread(img_path) # actually read it from disk
edit flag offensive delete link more

Comments

Adjusted that part then got the following error error: (-2) could not find a writer for the specified extension in function imwrite_

clara gravatar imageclara ( 2018-01-26 04:52:09 -0600 )edit

you don't show, what FULL_PATH_TO_DESTINATION is, but it should end like .png or .jpg

(similar to the input)

berak gravatar imageberak ( 2018-01-26 04:56:37 -0600 )edit

Ok, i fixed that part then strangely for someone using opencv for the first time i got this error `OpenCV Error: Unspecified error (The function is not implemented. Rebuild the library with Windows, GTK+ 2.x or Carbon support. If you are on Ubuntu or Debian, install libgtk2.0-dev and pkg-config, then re-run cmake or configure script) in cvShowImage, file /io/opencv/modules/highgui/src/window.cpp, line 583 cv2.error: /io/opencv/modules/highgui/src/window.cpp:583: error: (-2) The function is not implemented. Rebuild the library with Windows, GTK+ 2.x or Carbon support. If you are on Ubuntu or Debian, install libgtk2.0-dev and pkg-config, then re-run cmake or configure script in function cvShowImage, i'll continue under

clara gravatar imageclara ( 2018-01-26 05:06:56 -0600 )edit

Then i kept only cv2.imwrite(FULL_PATH_TO_DESTINATION, img) deleted the 2 lines under, ran without errors but there was no image in destination file, could it be that
my problem is not about read and write, if i'm drawing a rectangle around a image, then i should give your rectangle function a numpy array.

clara gravatar imageclara ( 2018-01-26 05:08:01 -0600 )edit

"cv2.error: /io/opencv/modules/highgui/src/window.cpp:583: error: (-2)" -- yea, that's what you get, when you're lazily installing via pip or such.

to get any gui support on linux, you willl have to build cv2 from src (with gtk installed before)

(or use matplotlib, if you have that)

berak gravatar imageberak ( 2018-01-26 05:14:57 -0600 )edit

imwrite will also not create any directories, you have to do that manually

berak gravatar imageberak ( 2018-01-26 05:20:34 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2018-01-26 04:38:00 -0600

Seen: 16,448 times

Last updated: Jan 26 '18