0
我試圖編寫一個代碼,以我的第一張圖像作爲參考來更正我的圖像的V通道。但是我在嘗試將所有像素的校正值傳遞給圖像時出現此錯誤。Python + opencv:ValueError:設置一個序列的數組元素
我不知道該怎麼做。我應該如何重寫我的代碼的那部分?我嘗試了很多次,但仍然無法解決問題。
>>>
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.
>>>
代碼:
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 + INT(deltaList [Z])'...在左邊,你有'v [i,j]' - 數組中的一個元素。在右邊,你有'v',一個完整的數組。將整個圖像分配給單個像素不太合理。問題是,爲什麼那些循環首先出現,當一個簡單的'v + = int(deltaList [z])'可以用更少的代碼實現同樣的事情並且更快。 –
你的意思是'v [i,j] = v [i,j] + int(deltaList [z])' –
@DanMašek謝謝你的寶貴意見和建議......非常感謝。你知道我應該添加什麼來防止溢出或下溢嗎? – SacreD