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

jer*_*man 2 python opencv

在使用python的opencv中-如何创建新图像,该图像只是其他图像中roi的副本?肯定有一些类似的东西

Mat roi = img( Rect(x,y,w,h) );
Run Code Online (Sandbox Code Playgroud)

用于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],:]
Run Code Online (Sandbox Code Playgroud)

顺便说一句,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)....

mcd*_*fee 5

import os
import cv2

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

  • 仍然应该是:`img [y:y + h,x:x + w]`(那是普通的numpy roi,顺便说一句) (2认同)