简体   繁体   中英

OpenCV resize error(-215:Assertion Failed)

This is my code for building a linear classifier for images and cv2.resize() is throwing error while converting the image from original size to (32,32)

import numpy as np
import cv2

labels = ["dog","cat","panda"]
np.random.seed(1)

W = np.random.randn(3,3072)
b = np.random.randn(3)

orig = cv2.imread("panda_00001.png")
image = cv2.resize(orig,(32,32)).flatten()

scores = W.dot(image) + b

for (label,score) in zip(labels,scores):
    print("[INFO] {}:{:.2f}".format(label,score))

cv2.putText(orig,"Label:{}".format(labels[np.argmax(scores)]),(10,30),cv2.FONT_HERSHEY_SIMPLEX,0.9,(0,255,0),2)

cv2.imshow("Image",orig)
cv2.waitkey(0)

Getting this error on execution

Traceback (most recent call last):
  File "linearclassifier.py", line 11, in <module>
    image = cv2.resize(orig,(32,32)).flatten()
cv2.error: OpenCV(4.3.0) C:\projects\opencv-python\opencv\modules\imgproc\src\resize.cpp:3929: error: (-215:Assertion failed) !ssize.empty() in function 'cv::resize'

Your picture path seems to be wrong or invalid.

It's always good practice to integrate a null check to the read picture in your script:

eg:

import cv2
... 
img = cv2.imread("myImage.jpg")

if (img.size == 0):
   # error handling or throw exception here!

# do stuff with image here 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM