函数imshow出错(Error in function imshow)

编程入门 行业动态 更新时间:2024-10-10 15:24:53
函数imshow出错(Error in function imshow)

我正在尝试使用HOG + SVM方法制作自定义对象检测器(椅子)。 我的行动计划是:

循环浏览我文件夹中的图像 阅读并调整它们的大小 使用hog.compute()函数将它们转换为向量 然后将它们存储到数据文件中

如果一切顺利,我的SVM分类器将处理剩下的事情。

理想情况下,它将能够识别通过我的网络摄像头显示的椅子。

这是我正在尝试使用的代码(仅初始位):

import cv2 import os import numpy as np def loadimg(): for i in range(0,19): image = cv2.imread(str(i)) cv2.imshow("img",image) (winW, winH) = (500, 500) r = 128.0 / image.shape[1] dim = (128, int(image.shape[0] * r)) img = cv2.resize(image, dim, interpolation = cv2.INTER_AREA) return img def hoggify(img): imeg = hog.compute(img) return imeg for fn in os.listdir('/Users/munirmalik/cvprojek/cod'): loadimg() hoggify(imeg) np.savetxt('data.txt')

这是我得到的错误:

cv2.error: /Users/munirmalik/opencv/modules/highgui/src/window.cpp:304: error: (-215) size.width>0 && size.height>0 in function imshow

我用google搜索了错误,它说imread()没有读取我的图像,我假设这与loadimg()函数中的for循环有关。

有人可以帮帮我吗? 此外,如果您能想到更好的方法,请告诉我:)

I'm trying to make a custom object detector (chairs) using the HOG+SVM method. My plan of action is to:

Loop through the images in my folder Read and resize them Convert them to vectors using the hog.compute() function And then store them into a data file

If all goes well, my SVM classifier will handle the rest.

Then ideally, it will be able to identify the chairs that are displayed through my webcam.

Here's the code I'm trying to use (only the initial bit):

import cv2 import os import numpy as np def loadimg(): for i in range(0,19): image = cv2.imread(str(i)) cv2.imshow("img",image) (winW, winH) = (500, 500) r = 128.0 / image.shape[1] dim = (128, int(image.shape[0] * r)) img = cv2.resize(image, dim, interpolation = cv2.INTER_AREA) return img def hoggify(img): imeg = hog.compute(img) return imeg for fn in os.listdir('/Users/munirmalik/cvprojek/cod'): loadimg() hoggify(imeg) np.savetxt('data.txt')

This is the error I'm getting:

cv2.error: /Users/munirmalik/opencv/modules/highgui/src/window.cpp:304: error: (-215) size.width>0 && size.height>0 in function imshow

I've googled the error and it says that imread() isn't reading my image, which I'm assuming has to do with my for loop in the loadimg() function.

Can someone please help me out? Also if you can think of a better way to go about doing this, please do let me know :)

最满意答案

这是您的代码中的错误

for i in range(0,19): image = cv2.imread(str(i))

这转换为cv2.imread("0") 。 cv2.imread()函数需要传递像“path / to / image.jpg”这样的图像地址。 我不确定你的图像是如何在文件结构中排列的,但是cv2.imread()的参数应该以JPG,PNG等文件扩展名结尾

根据OP的评论,代码应更改为:

for i in range(0,19): image = cv2.imread(r"/Users/munirmalik/cvprojek/cod/chairs/" + str(i)+ ".jpg")

This is the bug in your code

for i in range(0,19): image = cv2.imread(str(i))

This translates to cv2.imread("0"). The cv2.imread() functon needs to be passed an image address like "path/to/image.jpg". I'm not sure how your images are arranged in the file structure, but the argument to cv2.imread() should end in a file extension like JPG, PNG, etc

As per OP's comment, the code should be changed to:

for i in range(0,19): image = cv2.imread(r"/Users/munirmalik/cvprojek/cod/chairs/" + str(i)+ ".jpg")

更多推荐

本文发布于:2023-07-30 14:45:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1338829.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:函数   imshow   function   Error

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!