gpt4 book ai didi

python - 使用 python 的 opencv - 将 roi 复制到新的较小图像

转载 作者:太空宇宙 更新时间:2023-11-03 21:19:29 33 4
gpt4 key购买 nike

在 opencv 中使用 python -我怎样才能创建一个新图像,它只是另一个图像的 roi 的副本?肯定有一些类似

的东西
Mat roi = img( Rect(x,y,w,h) );

对于 python,或任何比蛮力更优雅的东西

rect=[x,y,w,h]
img = cv2.imread(subst)
roi= np.zeros((rect[3],rect[2],3),np.uint8) #is this really reversed? who ordered that?
cv2.rectangle(img,(x,y),(w+x,h+y),[255,0,0],thickness=1)
cv2.imshow('img',img)
cv2.waitKey()
#cv.Copy(cv.fromarray(img),cv.fromarray(roi),cv.fromarray(mask)) #can't make it work...
for x in range(rect[2]):
for y in range(rect[3]):
roi[y,x,:]=img[y+rect[1],x+rect[0],:]

顺便说一句,x,y 坐标顺序如何 - 是 [x,y,c] 还是 [y,x,c] 来指定 x(水平)和 y(垂直)处的点?它看起来像它的 [y,x,c], iiuc,而 cv2.rectangle 是 (x,y) 而 img.shape 是 (y,x) 这比 (g,r,b) 更烦人的(r,g,b)....

最佳答案

import os
import cv2

img = cv2.imread(filename)
roi = img[row:row+height,column:column+width]
cv2.imshow('ROI',roi)
cv2.waitKey(0)

关于python - 使用 python 的 opencv - 将 roi 复制到新的较小图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25734545/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com