“标准”RGB到灰度转换

问题描述 投票:11回答:6

我正在尝试编写一个转换器算法,该算法采用JPEG图像并返回其PGM(便携式灰度图)版本。问题是我无法理解“官方”JPG-> PGM转换器如何根据从经典RGB格式开始分配给最终像素(我猜,0-> 255)的值来工作。

一开始,我使用了这个公式(它与OpenCV的CV_RGB2GRAY转换使用的公式相同):

0.30 * R + 0.59 * G + 0.11 * B = val

我写了一个简单的代码来测试我的结果:它采用彩色图像及其PGM版本(已使用GIMP转换)。然后它使用前面的公式转换彩色图像。目标是使像素到像素的灰度图像等于PGM输入。

此时,它不会返回相同的值。你能帮助我吗?

image algorithm jpeg file-conversion pgm
6个回答
17
投票

问题是我无法理解“官方”JPG-> PGM转换器如何根据从经典RGB格式开始分配给最终像素(我猜,0-> 255)的值来工作。

这些“官方”工具正在使用的转换中可能存在伽玛调整。 也就是说,它不仅仅是线性变换。

有关详细信息,请参阅此维基百科部分:Converting color to grayscale

我相信你想使用Csrgb的公式。 试一试,看看它是否与您期望的结果相符。

基本上,你会这样做:

  1. R, G, B颜色(每个在[0,1]范围内) 如果它们在0..255的范围内,只需除以255.0
  2. 计算Clinear = 0.2126 R + 0.7152 G + 0.0722 B 这可能是您之前应用的线性变换
  3. 根据它的公式计算Csrgb,基于Clinear 这是您缺少的非线性伽玛校正件 看看this WolframAlpha plotCsrgb = 12.92 Clinear时,Clinear <= 0.0031308Csrgb = 1.055 Clinear1/2.4 - 0.055时,Clinear > 0.0031308

5
投票

关于“Y平面”的观点:标准颜色JPEG使用YCbCr颜色空间编码,其中Y是亮度分量(即亮度),Cb和Cr是蓝色差异和红色差异色度分量。因此,将彩色JPEG转换为灰度JPEG的一种方法是简单地删除Cb和Cr组件。

有一个名为jpegtran的实用程序可以使用-grayscale选项无损地执行此操作。 (无损部分真的唯一的问题,如果你想最终得到JPEG而不是PGM,以避免generation loss。)无论如何,这可能是进行这种转换的最快方法,因为它甚至不解码将图像转换为像素,更不用说每个像素。


1
投票

理论上,使用几个像素(在本例中为3),您可以确定他们的算法正在做什么。 Juste选择你的三个像素(p1,p2,p3),它们的RGB值和它们的PGM灰度值,你有:

RedConstant * p1.redValue + GreenConstant * p1.greenValue + BlueConstant * p1.blueValue = p1.grayValue

RedConstant * p2.redValue + GreenConstant * p2.greenValue + BlueConstant * p2.blueValue = p2.grayValue

RedConstant * p3.redValue + GreenConstant * p3.greenValue + BlueConstant * p3.blueValue = p3.grayValue。

然后解决这个问题(查找“方程求解器”或其他东西),看看他们使用的常数是什么。


1
投票

在OPENCV PYTHON中将RGB图像转换为灰度的简单算法!

我使用了注释,所以代码是不言自明的。但它工作得很快。

import cv2
import numpy as np
img1 = cv2.imread('opencvlogo.png')
row,col,ch = img1.shape
g = [ ]  #the list in which we will stuff single grayscale pixel value inplace of 3 RBG values
#this function converts each RGB pixel value into single Grayscale pixel value and appends that value to list 'g'
def rgb2gray(Img):
    global g
    row,col,CHANNEL = Img.shape
    for i in range(row) :
        for j in range(col):
        a =      (   Img[i,j,0]*0.07  +  Img[i,j,1]*0.72 +    Img[i,j,2] *0.21   ) #the algorithm i used id , G =  B*0.07 + G*0.72 + R* 0.21
                                                                                   #I found it online
        g.append(a)
rgb2gray(img1)  #convert the img1 into grayscale
gr = np.array(g)  #convert the list 'g' containing grayscale pixel values into numpy array
cv2.imwrite("test1.png" , gr.reshape(row,col)) #save the image file as test1.jpg

所以我用这个图像文件... enter image description here

我的程序生成以下Grayscale文件..

enter image description here


0
投票

将默认RGB ColorModel中的单个输入像素转换为单个灰色像素。

/* Convertation function 
 * @param x    the horizontal pixel coordinate
 * @param y    the vertical pixel coordinate
 * @param rgb  the integer pixel representation in the default RGB color model
 * @return a gray pixel in the default RGB color model.*/

    public int filterRGB(int x, int y, int rgb) {
    // Find the average of red, green, and blue.
    float avg = (((rgb >> 16) & 0xff) / 255f +
                 ((rgb >>  8) & 0xff) / 255f +
                  (rgb        & 0xff) / 255f) / 3;
    // Pull out the alpha channel.
    float alpha = (((rgb >> 24) & 0xff) / 255f);

    // Calculate the average.
    // Formula: Math.min(1.0f, (1f - avg) / (100.0f / 35.0f) + avg);
    // The following formula uses less operations and hence is faster.
    avg = Math.min(1.0f, 0.35f + 0.65f * avg);
    // Convert back into RGB.
   return (int) (alpha * 255f) << 24 |
          (int) (avg   * 255f) << 16 |
          (int) (avg   * 255f) << 8  |
          (int) (avg   * 255f);
}

-3
投票

平均方法是最简单的方法。你只需要取三种颜色的平均值。由于它是一个RGB图像,所以这意味着你用g添加r与b然后除以3得到你想要的灰度图像。

它以这种方式完成。

Grayscale = (R + G + B / 3)

如果您有一个彩色图像,如上图所示,并且您想使用平均方法将其转换为灰度。

© www.soinside.com 2019 - 2024. All rights reserved.