我试图总结灰度图像的灰度像素值" 1.jpg"对于图像中每个垂直的像素列。
以下是代码:
import cv2
img = cv2.imread("1.jpg", 0)
for i in range (img.shape[0]):
aaa = img[i]
print aaa
这样做我得到像
这样的东西aaa = [1,2,3] [2,3,4] [3,4,5] [4,5,6] .....
现在我如何总结数组得到:
bbb = [6, 9, 12, 15, ....]
如果我这样做:
import cv2
img = cv2.imread("1.jpg", 0)
for i in range (img.shape[0]):
aaa = img[i]
bbb = sum(aaa, 0)
print bbb
我明白了:
6
9
12
15
.
..
...
但那不是我需要的东西!!!
更新:已解决
终于能够做到这一点了:
import cv2
bbb = []
img = cv2.imread("1.jpg", 0)
for i in range (img.shape[0]):
bbb.append(sum(img[i]))
答案 0 :(得分:0)
you should put aaa in a container, like a list or set
aaa = []
for i in range (img.shape[0]):
aaa.append(img[i])
print aaa
或
aaa = [ i for i in range (img.shape[0])]
aaa = [[1,2,3],[2,3,4],[3,4,5],[4,5,6]]
[sum(a)for a in aaa]
出:
[6, 9, 12, 15]
答案 1 :(得分:0)
这应该做(未经测试)。请注意,img.shape[1]
给出了图像列的数量。
import cv2
results = []
img = cv2.imread('1.jpg', cv2.IMREAD_GRAYSCALE)
for col in range(img.shape[1]):
results.append(sum(img[col]))
答案 2 :(得分:0)
也许在所有答案中最快的方式,尝试并排使用numpy包。如下:
import cv2
import numpy as np
img = cv2.imread('your_image_file')
sum_cols = np.sum(img,axis=1)
sum_cols = np.sum(img,axis=1) #if the image is a color image.
这是有效的,并且沿着第二轴求和,即每行和每个颜色通道的列总和。
答案 3 :(得分:0)
如果您得到如下数组:
[1,2,3]
[2,3,4]
[3,4,5]
[4,5,6]
然后总结数组并将其存储到不同的列表中,尝试以下代码:
import cv2
img = cv2.imread("1.jpg", 0)
final_list = []
for i in range (img.shape[0]):
final_list.append(reduce(lambda x,y: x+y, img[i]))
print final_list