首页 > 解决方案 > 使用索引将图像的一部分从一个复制到另一个时出现openCV错误

问题描述

import cv2
import os
from PIL import Image
img = Image.open('/home/niti/Downloads/ironman.jpeg').convert('RGB')

img=cv2.imread('/home/niti/Downloads/ironman.jpeg')

def click(event,x,y,flag,param):# checking coordiantes
    if event==cv2.EVENT_LBUTTONDOWN:
        strp=str(x)+','+str(y)
        font=cv2.FONT_HERSHEY_COMPLEX
        cv2.putText(img,strp,(x,y),font,1,(0,255,255),2)
        cv2.imshow('image',img)

b,g,r=cv2.split(img)
img=cv2.merge((b,g,r))
print(img.shape)        
print(img.size)
print(img.dtype)

hand=img[928:233, 1103:378]

img[71:121, 187:216]=hand 

#cv.imshow('image',img)   

cv2.imshow('image',img)
cv2.setMouseCallback('image',click)
cv2.waitKey(0) 
cv2.destroyAllWindows()

我收到以下错误

(774, 1280) 990720 uint8 -------------------------------------------------------- -------------------------------- ValueError Traceback (last last call last) in 22 hand=img[928:233, 1103:378] 23 ---> 24 img[71:121, 187:216]=hand 25 26 #cv.imshow('image',img)

ValueError:无法将输入数组从形状(0,0,3)广播到形状(50,29,3)

标签: python-3.xopencvindexingjupyter-notebookubuntu-18.04

解决方案


推荐阅读