使用python的opencv-将roi复制到新的较小图像
在使用python的opencv中- 如何创建一个新图像,它只是另一个图像的roi副本? 当然也有类似的东西使用python的opencv-将roi复制到新的较小图像,python,opencv,Python,Opencv,在使用python的opencv中- 如何创建一个新图像,它只是另一个图像的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.rectang
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.矩形是(x,y)和img形状(y,x),它比(g,r,b)更讨厌(r,g,b)……< /p> btw。上面的C++没有实际复制。也许你的意思是
matroi=img(Rect(x,y,w,h)).clone()
您是否尝试过Mat roi=image[x:y,x+w:y+h]
?假设图像为灰度,则此方法有效。如果我们有颜色(即roi=img[x:x+w,y:y+h,:]
它可以在Python 2.7.6中为我的彩色图像添加或不添加其他访问器。奇怪的是…我出错了。是时候更新我的发行版了。谢谢你的消息!仍然应该是:img[y:y+h,x:x+w]
(这是一个简单的numpy roi,顺便说一句)@berak,你是对的,我编辑了我的答案以澄清和更正。
import os
import cv2
img = cv2.imread(filename)
roi = img[row:row+height,column:column+width]
cv2.imshow('ROI',roi)
cv2.waitKey(0)