简体   繁体   中英

Image Array "ValueError: setting an array element with a sequence"

I want to do some operation in numpy array. Actually I'm trying to zoom an image using the nearest neighbour rule. I have facing that above titled issue.

import cv2
import numpy as np
from numpy import ndarray

img = cv2.imread('abc.jpg')

rows = img.shape[0]*2
cols = img.shape[1]*2

zoomed = np.zeros((rows, cols), dtype=img.dtype)

for i in range(0, rows):
    for j in range(0, cols):
        zoomed[i][j] = img[int(i/2)][int(j/2)]

cv2.imshow('Input Image', img)
cv2.imshow('Zoomed Image', zoomed)
cv2.waitKey(0)

Try:

zoomed = np.zeros((rows, cols, 3), dtype=img.dtype)

The error you're getting is happening because img[int(i/2)][int(j/2)] is actually three RGB values and zoomed[i][j] can only hold integers. Creating zoomed to have shape (rows, cols, 3) allows zoomed to hold 3 integers at every row, column location.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM