简体   繁体   中英

Python + opencv :ValueError: setting an array element with a sequence

I am trying to write a code that correct the V channel of my images by taking my first images as reference. But I am getting this error while trying to pass my correction value to all the pixel to the images.

I am not sure how to do it. How should I rewrite my code for that part? I tried and error a lot of times but still didnt managed to resolve the issue.

>>> 
 RESTART: C:/Users/xxxx/AppData/Local/Programs/Python/Python36/avergae.py 
[0.0, 103.81328149045861, 102.25728890139274, 100.11808781708474, 102.70660218168091, 104.8367051139934, 99.823930500250071, 104.96426229104148, 101.85104381075587, 102.09709583116921, 99.400945032365726, 92.15991298604699, 101.19626441549323, 103.19529341359842, 101.34438951969196, 102.6448956449741, 94.161672541871852, 91.460941106879034, 101.18572887210487, 101.6783903260298, 90.000500755040008]
103.81328149
[0.0, 0.0, 1.5559925890658661, 3.6951936733738648, 1.1066793087777, -1.0234236235347964, 3.9893509902085356, -1.1509808005828717, 1.9622376797027385, 1.716185659289394, 4.4123364580928808, 11.653368504411617, 2.6170170749653749, 0.6179880768601862, 2.4688919707666486, 1.168385845484508, 9.6516089485867553, 12.352340383579573, 2.6275526183537323, 2.134891164428808]
[[ 38  38  38 ...,  37  37  36]
 [ 38  37  38 ...,  38  38  38]
 [ 39  39  39 ...,  38  38  38]
 ..., 
 [141 141 142 ..., 160 161 161]
 [142 142 144 ..., 164 160 159]
 [142 142 143 ..., 168 162 159]]
3648
5472
Traceback (most recent call last):
  File "C:/Users/Lian Ming/AppData/Local/Programs/Python/Python36/avergae.py", line 49, in <module>
    v[i,j] = v+int(deltaList[z])
ValueError: setting an array element with a sequence.

>>>

Code :

import cv2
import numpy as np

path = 'C:\\Users\\xxxx\\Desktop\\experiment\\aligned\\'
path1 = 'C:\\Users\\xxxx\Desktop\\experiment\\aligned\\New folder\\'

img1 = cv2.imread(path + 'aligned_IMG_1770.png')
img1 = cv2.cvtColor(img1, cv2.COLOR_BGR2HSV)
h_reference, s_reference, v_reference = cv2.split(img1)
Average_V_Reference = np.average(v_reference)  #get the average of V for my first images as a reference value to compare to the rest of images

def ValueMean(im_file):  #Create a function that do the average on the V channel for all the images
    im = cv2.imread(im_file)
    im = cv2.cvtColor(im, cv2.COLOR_BGR2HSV)
    h,s,v = cv2.split(im)
    a = np.average(v)
    return a             

myList = [0.0]
deltaList = [0.0] #store it in deltalist
num_images = 20

for i in range(1770, 1790):
    image_name = path + 'aligned_IMG_' + str(i) + '.png'
    myList.append(ValueMean(image_name)) #store all the images average into a list

print(myList)
print(Average_V_Reference)

for z in range(1, num_images):
    deltaList.append(z)
    deltaList[z] = Average_V_Reference - myList[z] #Data for all the difference in average value compared to reference images
print(deltaList)

z=1
for k in range(1770,1790):  #create a loop to recreate images based on the data i got
    a = 'aligned_IMG_' + str(k)
    image_name = path + a + '.png'
    img_file = cv2.imread(image_name)
    img_file = cv2.cvtColor(img_file, cv2.COLOR_BGR2HSV)
    h,s,v = cv2.split(img_file)
    print(v)
    print(img_file[:,:,2].shape[0])
    print(img_file[:,:,2].shape[1])
    for i in range(img_file[:,:,2].shape[0]): #passing correction value to each pixel on the V channel
        for j in range (img_file[:,:,2].shape[1]):
            v[i,j] = v+int(deltaList[z])
    z += 1
    img_file = cv2.merge((h,s,v))  #Merge back the HSV channel
    img_file = cv2.cvtColor(img_file, cv2.COLOR_HSV2BGR) #convert back to BGR and save
    new_image_name = path1 + 'BrightnessHSV_%d'%i + '.png'
    cv2.imwrite('new_image_name', new_image_name)

更改以下行即可解决

v[i,j] = v[i,j] +int(deltaList[z])

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