python - saving the bounding box image
问题描述
Instead of trying to draw the bounding box over the image, i am trying to save it as a new image.
When i was getting [ymin, xmax, ymax, xmin] points, i was doing this.
import cv2
import numpy as np
image = cv2.imread('ballet_106_0.jpg')
image = np.array(image)
boxes = [21, 511, 41, 420 ]
ymin, xmax , ymax ,xmin = boxes
im2 = image[ymin:ymax,xmin:xmax,:]
cv2.imwrite('bboximage.jpg',im2)
But if i only get the x
and y
points along with the height
and width
. I'm not sure how i could index the numpy array.
Any suggestions would be really helpful ,Thanks in advance.
解决方案
您的代码看起来不错,尽管这一行:
image = np.array(image)
不是必需的,好像一切顺利cv2.imread
产生np.array
,但是如果cv2.imread
失败它返回None
,这可能是您的问题的根源,请在您的下面添加以下行cv2.imread
:
print(type(image))
如果它打印None
,则很可能意味着ballet_106_0.jpg
您的目录中没有图像。
编辑:要转换x,y,height,width
为x/y-min/max
值,只需执行
ymin = y
ymax = y+height
xmin = x
xmax = x+width
推荐阅读
- javascript - 如何在 React-Native 上分析网络?
- c++ - C ++:获取内存分配错误的SIGCONT错误
- validation - 验证集的指标很奇怪
- javascript - 欧拉计划 #32:为什么我的答案是错误的?
- html - 带有正则表达式的 html+input 类型文本不带下划线(_)和特殊字符
- python - matplotlib 中的重叠图
- visual-studio-code - 将 graphql.config.js 和 apollo.config.js 与 ApolloClient 一起使用
- reactjs - 我可以将 FieldArray 与 useFormik 挂钩一起使用吗?
- visual-studio - 如何在 Visual Studio 中检查将代码上传到 GitHub 的进度?
- javascript - fetchPage 返回空