Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/image/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/ssh/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何循环图像中的各个像素并输出每行中RGB值最高的像素?_Python_Image_Image Processing_Rgb - Fatal编程技术网

Python 如何循环图像中的各个像素并输出每行中RGB值最高的像素?

Python 如何循环图像中的各个像素并输出每行中RGB值最高的像素?,python,image,image-processing,rgb,Python,Image,Image Processing,Rgb,我正在尝试导入一个图像文件,如file.bmp,读取图像中每个像素的RGB值,然后将每行RGB值最高的像素(最亮的像素)输出到屏幕上。有关于如何使用Python的建议吗?好吧,您可以使用scipy.misc.imread读取图像并像这样操作它: import scipy.misc file_array = scipy.misc.imread("file.bmp") def get_brightness(pixel_tuple): return sum([component*compone

我正在尝试导入一个图像文件,如file.bmp,读取图像中每个像素的RGB值,然后将每行RGB值最高的像素(最亮的像素)输出到屏幕上。有关于如何使用Python的建议吗?

好吧,您可以使用
scipy.misc.imread
读取图像并像这样操作它:

import scipy.misc
file_array = scipy.misc.imread("file.bmp")

def get_brightness(pixel_tuple):
   return sum([component*component for component in pixel_tuple])**.5 # distance from (0, 0, 0)

row_maxima = {}
height, width = len(file_array), len(file_array[0])
for y in range(height):
  for x in range(width):
    pixel = tuple(file_array[y][x]) # casting it to a tuple so it can be stored in the dict
    if y in row_maxima and get_brightness(pixel) > row_maxima[y]:
      row_maxima[y] = pixel
    if y not in row_maxima:
      row_maxima[y] = pixel
print row_maxima

你可以在这里充分利用numpy的力量。请注意,下面的代码输出范围为[0255]的“亮度”

#!/bin/env python

import numpy as np
from scipy.misc import imread

#Read in the image
img = imread('/users/solbrig/smooth_test5.png')

#Sum the colors to get brightness
brightness = img.sum(axis=2) / img.shape[2]

#Find the maximum brightness in each row
row_max = np.amax(brightness, axis=1)
print row_max
如果您认为图像可能有alpha层,可以执行以下操作:

#!/bin/env python

import numpy as np
from scipy.misc import imread

#Read in the image
img = imread('/users/solbrig/smooth_test5.png')

#Pull off alpha layer
if img.shape[2] == 4:
    alph = img[:,:,3]/255.0
    img = img[:,:,0:3]
else:
    alph = np.ones(img.shape[0:1])

#Sum the colors to get brightness
brightness = img.sum(axis=2) / img.shape[2]
brightness *= alph

#Find the maximum brightness in each row
row_max = np.amax(brightness, axis=1)
print row_max

当我试着运行这段代码时,它说“image”没有定义:第11行,in pixel=tuple(image[y][x])#将其转换为一个tuple,以便可以存储在dict NameError中:name“image”没有定义您将image定义为什么?对不起,我是python新手。哦,对不起,这是我的错别字<代码>图像应该是文件数组。我已经更新了。我还没有看到计算亮度的rgb距离(0,0,0)方法。我认为一个标准方法将使用30%的红色值+60%的绿色值+10%的蓝色值来匹配人类的视觉灵敏度。看,我不知道numpy中sum()的axis参数。很好的解决方案。大多数应用程序都允许使用
关键字。它非常有用。