Ask Your Question
0

how to append list's element to an array - python opencv

asked 2014-02-24 04:20:19 -0600

varsha1901 gravatar image

updated 2014-02-24 04:22:39 -0600

berak gravatar image

i have to blue, green and red channels mean values in a separate list, and now i want to append all these values to a single list/ array. my code is like this:

import cv2
import numpy as np
import os,glob
resizelist = list()
B_mean = list()
G_mean = list()
R_mean = list()
path = 'C:\Users\HP\Desktop\dataset1'
for infile in glob.glob(os.path.join(path,'*.jpg')):
   imge = cv2.imread(infile)
   arr1 = np.array(imge)
   re_img = cv2.resize(imge,(200,200))
   resizelist.append(re_img)
   blue, green, red = cv2.split(re_img)
   total = re_img.size
   B = sum(blue) / total
   G = sum(green) / total
   R = sum(red) / total
   B_mean.append(B)
   G_mean.append(G)
   R_mean.append(R)
main_list = [[],[],[]]
main_list[0] = B_mean
main_list[1] = G_mean
main_list[2] = R_mean
print main_list

But the problem is, it is not printing the values, instead its displaying zeros only. So, how do i achieve this?

Thanks!

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
2

answered 2014-02-24 04:31:29 -0600

berak gravatar image

updated 2014-02-24 04:32:44 -0600

here's the culprit:

B = sum(blue) / total

all values are 0 due to integer division. you'll have to change the type to float to make it work:

import cv2
import numpy as np
import os,glob
resizelist = list()
B_mean = list()
G_mean = list()
R_mean = list()
path = 'C:/Users/HP/Desktop/dataset1'
for infile in glob.glob(os.path.join(path,'*.jpg')):
   imge = cv2.imread(infile)
   arr1 = np.array(imge)
   re_img = cv2.resize(imge,(200,200))
   resizelist.append(re_img)
   blue, green, red = cv2.split(re_img)
   total = re_img.size
   B = np.float32(sum(blue)) / total
   G = np.float32(sum(green)) / total
   R = np.float32(sum(red)) / total
   B_mean.append(B)
   G_mean.append(G)
   R_mean.append(R)
main_list = [B_mean,G_mean,R_mean]
print main_list
edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2014-02-24 04:20:19 -0600

Seen: 4,689 times

Last updated: Feb 24 '14