【问题标题】:How to calculate percentage of green and the percentage of blue in the images, array already made, for loop reading the image file如何计算图像中绿色的百分比和蓝色的百分比,数组已经制作,循环读取图像文件
【发布时间】:2019-03-14 13:23:48
【问题描述】:

所以我已经能够读取文件并生成一个数组。我正在努力寻找找到图像中绿色和蓝色百分比的方法。

#Import Libraries 

import matplotlib.pyplot as plt
import matplotlib.image as mpimg
import numpy as np
from PIL import Image
import glob

#option comd c and then cmd v to paste path /Users/Gilly/Desktop/Comp 180/images
#2 Reads the image of a sunset into an array LOOP

list_files=glob.glob("/Users/Gilly/Desktop/Comp 180/images/*.jpg")

for i in list_files:
    img = mpimg.imread(i)
    print(img)

#Plots the image from the array data 

for i in list_files:
    imgplot = plt.imshow(img)
    plt.show()

#Calculate % of Green and Blue in the images 

【问题讨论】:

  • 纯代码编写请求在 Stack Overflow 上是题外话——我们希望这里的问题与特定编程问题有关——但我们很乐意帮助您自己编写!告诉我们what you've tried,以及您遇到的问题。这也将有助于我们更好地回答您的问题。
  • "Can Someone Help Me?" is not a valid SO question。这通常表明您需要的是与当地导师相处或完成教程,而不是 Stack Overflow。在这种情况下,您在识别颜色区域的研究中发现了什么?您的编码尝试在哪里?
  • 我明白了,谢谢你,我尝试了很多次,但我不知道我必须发布那个。

标签: python arrays image colors percentage


【解决方案1】:
emptyBlue = []
emptyGreen= []
for i in list_files:
    img = mpimg.imread(i)
    imgplot = plt.imshow(img)
    RGBtuple = np.array(img).mean(axis=(0,1))
    averageRed = RGBtuple[0]
    averageGreen = RGBtuple[1]
    averageBlue = RGBtuple[2]
    percentageGreen = averageGreen/(averageRed+averageGreen+averageBlue)
    percentageBlue = averageBlue/(averageRed+averageGreen+averageBlue)
    percentageRed = averageRed/(averageRed+averageGreen+averageBlue)
    emptyBlue+=[percentageBlue]
    emptyGreen+=[percentageGreen]
    print('Percent Blue',percentageBlue)
    print('Percent Green',percentageGreen)
print('Percentages of Blue',emptyBlue)
print('Percentages of Green',emptyGreen)

【讨论】:

    【解决方案2】:

    这里有一个提示,可以帮助您入门。

    要找到平均蓝色值,您必须遍历图像中的每个像素。您正在处理的图像(存储在 img 变量中)是一个行列表,每一行都是一个像素列表。

    嵌套的 for 循环可以让您遍历每个像素:

    for row in img:
        for pixel in row:
            red = pixel[0]
            green = pixel[1]
            blue = pixel[2]
            print(blue)
    

    如果遇到困难,请记住average = total / count

    【讨论】:

    • 谢谢我得到它,我最终用类似的东西做事
    猜你喜欢
    • 2023-04-08
    • 1970-01-01
    • 1970-01-01
    • 2014-02-23
    • 2021-06-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多