pythonopencv图片粘贴,使用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) );

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],:]

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)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值