Reputation: 103
img = cv2.imread('img/dream.jpg')
(b,g,r)=cv2.split(img)
color = ('b','g','r')
b = cv2.calcHist([img],[0],None,[256],[0, 256])
g = cv2.calcHist([img],[1],None,[256],[0, 256])
r = cv2.calcHist([img],[2],None,[256],[0, 256])
for i, col in enumerate(color):
histr = cv2.calcHist([img],[i],None,[256],[0, 256])
plt.plot(histr, color = col)
plt.xlim([0, 256])
with open('output.csv', 'w', newline='') as csvfile:
writer = csv.writer(csvfile)
writer.writerow('BGR')
for row in rows:
writer.writerow(row)
I am trying to read in a picture and show its RGB counts of each pixel. It's all cool until I tried to write my data into a csv file. Here is what it look like:
B,G,R
[0.],[0.],[631788.]
[0.],[0.],[418355.]
[0.],[0.],[105815.]
All I need is to remove the '[' and ']' from my array.
I am new to python I am not sure whether the array is tuple dictionary or something else.
I have tried the replace() method however it appear an error message 'numpy.ndarray' object has no attribute 'replace'
Upvotes: 1
Views: 120
Reputation: 528
Every value in your array is within its own list. In python you access elements of a list using an index.
In your case, you can convert your array of lists (with each list having only one value i[0]
) using the following:
delisted_array = np.array([i[0] for i in your_array_of_lists])
This list comprehension says: "take the value from first index of each (list) found in this array and put those values in a list".
Eg.
In [70]: a = np.array([[1],[2]])
In [71]: a
Out[71]:
array([[1],
[2]])
In [72]: delisted_array = np.array([i[0] for i in a])
In [73]: delisted_array
Out[73]: array([1, 2])
Upvotes: 1
Reputation: 3317
What you need is to convert a list
to String
by
list_string = ''.join(list_name)
as well if you have a matrix
you will need
for item in list_outer:
item = ''.join(item)
list_outer = ''.join(list_outer)
Upvotes: 1