【问题标题】:Calculating the green colour percentage in an image计算图像中的绿色百分比
【发布时间】:2023-04-08 03:58:01
【问题描述】:

我想计算图像中的绿色百分比

我通过遍历图像中的每个像素并检查每个像素的颜色来计算。最后,我保留绿色像素的数量,并找到整个图像的总百分比。

def green_color_optimized(screenpath):
    start_time = time.time()
    img = image.load_img(screenpath,target_size=(300,300,3))
    x = image.img_to_array(img)
    print("Image size: ", x.shape)
    count_green = 0
    for i in range(0,x.shape[0]):
      for j in range(0,x.shape[1]):
        pixel = list(map(int, x[i,j].tolist()))
        if sum(pixel) != 0:
          green_pixel = 100*(pixel[1]/sum(pixel))
          blue_pixel = 100*(pixel[2]/sum(pixel))
          red_pixel = 100*(pixel[0]/sum(pixel))
          if green_pixel > red_pixel and green_pixel > blue_pixel:
            if green_pixel > 35:
              count_green += 1
    green_percent = round(100*(count_green/(x.shape[0]*x.shape[1])),2)

使用此代码,处理每个图像大约需要 200 毫秒;我想处理 100 万张图像。如何优化代码?

【问题讨论】:

    标签: python python-3.x image-processing


    【解决方案1】:

    假设 x 是一个 numpy 数组,您应该始终向量化您的矩阵运算。以下运行速度快约 200 倍:

    # Your original function, with the file i/o removed for timing comparison 
    def green_original(x):
        count_green = 0
        for i in range(0,x.shape[0]):
          for j in range(0,x.shape[1]):
            pixel = list(map(int, x[i,j].tolist()))
            if sum(pixel) != 0:
              green_pixel = 100*(pixel[1]/sum(pixel))
              blue_pixel = 100*(pixel[2]/sum(pixel))
              red_pixel = 100*(pixel[0]/sum(pixel))
              if green_pixel > red_pixel and green_pixel > blue_pixel:
                if green_pixel > 35:
                  count_green += 1
        green_percent = round(100*(count_green/(x.shape[0]*x.shape[1])),2)
        return green_percent
    
    
    def green_vectorized(x):
        mask = (img[:,:,1] > img[:,:,0]) & (img[:,:,1] > img[:,:,2]) & ((img[:,:,1]/np.sum(img, axis=2)) > .35)
        round(100 * np.sum(mask)/(x.shape[0]*x.shape[1]), 2)
    
    img = np.ones(shape=(300,300,3))
    img[0:150,0:150, 1] = 134
    %timeit green_original(img)
    %timeit green_vectorized(img)
    

    你的版本

    每个循环 81.7 毫秒 ± 6.24 毫秒(7 次运行的平均值 ± 标准偏差,每次 10 次循环)

    矢量化版本

    每个循环 461 µs ± 78.2 µs(7 次运行的平均值 ± 标准偏差,每次 1000 个循环)

    【讨论】:

    • 嗯,不错,不错。我已将该位更改为 (img[:,:,1]/np.sum(img, axis=2) > .35)
    • 不错!如果您使用np.uint8(而不是float64)和随机数据,即使用np.random.seed(42)img = np.random.randint(0,256,(300,300,3),dtype=np.uint8) 初始化您的数据,那么时序比较可能更具代表性
    • 如何在掩码语句中添加 if 条件?对于某些像素,np.sum 会给出 0,所以它会抛出除以零异常
    • @NiteshMenon 在控制台上得到这个RuntimeWarning: invalid value encountered in true_divide mask = (x[:,:,1] > x[:,:,0]) & (x[:,:,1] > x[:,:,2]) & ((x[:,:,1]/np.sum(x, axis=2)) > .35) D:/Summer_RA/Code/Screenshot_optimized_v2.py:31: RuntimeWarning: invalid value encountered in greater mask = (x[:,:,1] > x[:,:,0]) & (x[:,:,1] > x[:,:,2]) & ((x[:,:,1]/np.sum(x, axis=2)) > .35)
    猜你喜欢
    • 1970-01-01
    • 2019-03-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-28
    • 2014-02-23
    • 1970-01-01
    • 2021-06-15
    相关资源
    最近更新 更多