【问题标题】:Pixel intensity of RGB images and how to multiply it with integers to view shades of grayRGB 图像的像素强度以及如何将其与整数相乘以查看灰度阴影
【发布时间】:2019-10-27 10:25:48
【问题描述】:

我有一个 RGB 图像,它有 4 种不同的颜色黑色(0,0,0)作为背景,绿色(106,136,93)蓝色(64,224,208)和棕色(168,124,85)。当我将图像读取为灰度并使用 np.unique() 它返回一个巨大的像素强度列表。 但实际上,只有 4 种强度,即 [0,1,2,3] 黑色、绿色、蓝色和棕色。

import cv2
import numpy as np

test = cv2.imread("test-BlackBG.png",0) #test image 

results = np.unique(test)     #returns [0,1,2,3,4,5,6,7,8...........132]
print(test.shape)             #returns (480, 640)
print(results)
cv2.imshow("image",test)
cv2.waitKey()
cv2.destroyAllWindows()

预期结果:当我将图像乘以 85 时,它应该以不同的灰度显示所有 3 种强度。

【问题讨论】:

  • 您是否要最终尝试实现灰度图像?如果是这样,请尝试gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
  • 如果您在问题中包含的图像是起始图像,您应该知道它有 790 种独特的颜色...
  • 也许开始另一个关于您创建此图像的方式的问题......它是否在其生命的某个阶段被保存为 JPEG?为什么它有一个未使用的 Alpha 通道?
  • 这似乎不太可能。你数过np.unique()的灰色数了吗?

标签: python numpy opencv image-processing


【解决方案1】:

我认为边缘正在把它扔掉。尝试编写一个函数,将构成形状边缘的像素设置为该形状的确切颜色。

【讨论】:

    【解决方案2】:

    我不完全确定您在这里要求什么,但要确定图像的 RGB 像素强度,您可以隔离每个 RGB 通道,同时将其他通道设置为 0 .

    原图

    import cv2
    
    image = cv2.imread('pikachu_smile.png')
    
    blue = image.copy()
    # Set green and red channels to 0
    blue[:, :, 1] = 0
    blue[:, :, 2] = 0
    
    green = image.copy() 
    # Set blue and red channels to 0
    green[:, :, 0] = 0
    green[:, :, 2] = 0
    
    red = image.copy()
    # Set blue and green channels to 0
    red[:, :, 0] = 0
    red[:, :, 1] = 0
    
    cv2.imshow('blue', blue)
    cv2.imshow('green', green)
    cv2.imshow('red', red)
    
    cv2.waitKey(0)
    

    孤立的蓝色(左)、绿色(中)和红色(右)通道

    要增加特定通道的强度,您可以为整个通道添加一个固定值。例如,使用绿色通道

    green[:, :, 1] += 40
    

    【讨论】:

      【解决方案3】:

      您的问题和假设有许多问题。


      您无法使用np.unique(image)

      您无法使用np.unique(im)的图像中的颜色。让我们来看看为什么通过仅具有4个强度的随机图像:0,1,2和3。

      import numpy as np
      import cv2
      
      # Ensure repeatable, deterministic randomness!
      np.random.seed(42)
      
      # Make a random image
      im = np.random.randint(0,4,(480,640,3), dtype=np.uint8)
      

      看起来像这样,每行是一个像素的RGB三联网:

      array([[[2, 2, 3],
          [3, 2, 1],
          [2, 2, 0],
          ...,
          [3, 3, 2],
          [0, 0, 1],
          [1, 1, 1]],
          ...,
          [3, 3, 1],
          [2, 3, 0],
          [0, 1, 3]]], dtype=uint8)
      
      现在,如果你尝试获得这样的唯一颜色,它就不起作用,因为每个颜色是组合的3强度:
      np.unique(im)    # prints: array([0, 1, 2, 3], dtype=uint8)
      

      ,如果您希望唯一颜色的数量,则需要查找三个RGB / BGR值的唯一组合数:

      np.unique(im.reshape(-1, im.shape[2]), axis=0)
      

      给出图像中唯一的RGB / BGR三联网的向量 - 每行都是唯一的颜色组合:

      array([[0, 0, 0],
             [0, 0, 1],
             [0, 0, 2],
             [0, 0, 3],
             [0, 1, 0],
             [0, 1, 1],
             [0, 1, 2],
             [0, 1, 3],
             [0, 2, 0],
             [0, 2, 1],
             [0, 2, 2],
             [0, 2, 3],
             [0, 3, 0],
             [0, 3, 1],
             [0, 3, 2],
             [0, 3, 3],
             [1, 0, 0],
             [1, 0, 1],
             [1, 0, 2],
             [1, 0, 3],
             [1, 1, 0],
             [1, 1, 1],
             [1, 1, 2],
             [1, 1, 3],
             [1, 2, 0],
             [1, 2, 1],
             [1, 2, 2],
             [1, 2, 3],
             [1, 3, 0],
             [1, 3, 1],
             [1, 3, 2],
             [1, 3, 3],
             [2, 0, 0],
             [2, 0, 1],
             [2, 0, 2],
             [2, 0, 3],
             [2, 1, 0],
             [2, 1, 1],
             [2, 1, 2],
             [2, 1, 3],
             [2, 2, 0],
             [2, 2, 1],
             [2, 2, 2],
             [2, 2, 3],
             [2, 3, 0],
             [2, 3, 1],
             [2, 3, 2],
             [2, 3, 3],
             [3, 0, 0],
             [3, 0, 1],
             [3, 0, 2],
             [3, 0, 3],
             [3, 1, 0],
             [3, 1, 1],
             [3, 1, 2],
             [3, 1, 3],
             [3, 2, 0],
             [3, 2, 1],
             [3, 2, 2],
             [3, 2, 3],
             [3, 3, 0],
             [3, 3, 1],
             [3, 3, 2],
             [3, 3, 3]], dtype=uint8)
      

      或,作为简单的唯一颜色:

      len(np.unique(im.reshape(-1, im.shape[2]), axis=0))    # prints 64
      

      所以,对于您的图像:

      # Open image
      im = cv2.imread('image.png',cv2.IMREAD_UNCHANGED)
      
      # Count unique colours
      len(np.unique(im.reshape(-1, im.shape[2]), axis=0)    # prints 790
      

      有比预期的颜色更多 h1>

      为什么我有比我期望更多的颜色?两个最常见的原因是:

      • 图像被保存为JPEG Li>
      • 存在抗锯齿的文本或绘制的形状

      让我们看看jpeg如何拯救你!

      # Load image and count colours
      im = cv2.imread('image.png',cv2.IMREAD_UNCHANGED)
      len(np.unique(im.reshape(-1, im.shape[2]), axis=0))    # prints 790
      
      # Save as JPEG
      cv2.imwrite('temp.jpg',im)
      
      # Reload and recount just the same
      im = cv2.imread('temp.jpg',cv2.IMREAD_UNCHANGED)
      len(np.unique(im.reshape(-1, im.shape[2]), axis=0))    # prints 4666 !!!
      

      如何将图像调用图像 - (将颜色缩小为固定调色板)? h1>

      如果要将图像映射到您自己的特定调色板,首先需要在BGR订单中指定您的调色板(@ 987654333)以匹配OpenCV的订购:

      palette = np.array([
         [0,0,0],                # Black
         [93,136,106],           # Green
         [208,224,64],           # Blue
         [85,124,168]],          # Brown
         dtype=np.uint8)
      

      然后读取您的图像丢弃完全无意义的alpha通道:

      test = cv2.imread("image.png",cv2.IMREAD_COLOR)
      

      然后从每个像素计算到每个调色板条目的距离:

      distance = np.linalg.norm(test[:,:,None] - palette[None,None,:], axis=3)
      
      然后选择每个像素最接近的一个调色板颜色:
      palettised = np.argmin(distance, axis=2).astype(np.uint8)
      

      您的图像现在处于阵列palettised并存储在每个像素位置是您的调色板中最接近的颜色的索引 - 所以,当您的调色板有4个条目(0..3),所有图像的元素是0,1,2或3。

      所以,现在您可以将85乘以:

      result = palettised * 85
      

      【讨论】:

      • 你让我得到了结果,但我对它有不同的方法。我使用了CV2.InRange()来使用RGB(1,1,1),棕色,带RGB(2,2,2)和RGB(3,3,3)的蓝色交换绿色。这给了我一个黑色图像,当我用85乘以85时,我得到了一个类似的图像作为输出。我喜欢你的解决方案,也会尝试一下。 span>
      猜你喜欢
      • 2020-05-04
      • 2017-02-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-08-20
      • 2021-11-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多