使用python的opencv-将roi复制到新的较小图像 [英] opencv using python - copy roi to new smaller image
问题描述
在使用Python的opencv中- 如何创建一个新图像,该图像只是其他图像的roi副本? 肯定有一些与
In opencv using python - How can I create a new image that is simply a copy of an roi from another image? Surely there is something along the lines of
Mat roi = img( Rect(x,y,w,h) );
用于python或比蛮力更优雅的任何东西
for python, or anything more elegant than the brute force
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)....
and btw how do the x,y coordinates order - is it [x,y,c] or [y,x,c] to specify a point at x (horizontal) and y (vertical)? It seems like its [y,x,c], iiuc, while the cv2.rectangle is (x,y) and img.shape is (y,x) which would be even more annoying than (g,r,b) instead of (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的opencv-将roi复制到新的较小图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!