RGB转Lab

C语言

#include "opencv2/core/core.hpp"
#include "opencv2/core/core.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/highgui/highgui.hpp"
#include <iostream>
#include <stdio.h>
 
using namespace cv;


inline float gamma(float x)
{
	return x > 0.04045 ? pow((x + 0.055f) / 1.055f, 2.4f) : x / 12.92;
};


void RGBToLab(unsigned char R, unsigned char G, unsigned char B,
	float *L, float *a, float *b)
{
	float Br = gamma(B / 255.0f);
	float Gr = gamma(G / 255.0f);
	float Rr = gamma(R / 255.0f);

	double X = 0.412453*Rr + 0.357580*Gr + 0.180423*Br;
	double Y = 0.212671*Rr + 0.715160*Gr + 0.072169*Br;
	double Z = 0.019334*Rr + 0.119193*Gr + 0.950227*Br;

	X /= 0.95047;
	Y /= 1.0;
	Z /= 1.08883;

	float FX = X > 0.008856f ? pow(X, 1.0f / 3.0f) : (7.787f * X + 0.137931f);
	float FY = Y > 0.008856f ? pow(Y, 1.0f / 3.0f) : (7.787f * Y + 0.137931f);
	float FZ = Z > 0.008856f ? pow(Z, 1.0f / 3.0f) : (7.787f * Z + 0.137931f);

	*L = 116.0f * FY - 16.0f;
	*a = 500.0f * (FX - FY);
	*b = 200.0f * (FY - FZ);
}

int main(int argc, const char ** argv)
{

	Mat img = imread("E:\\Self\\BackGround\\test.jpeg");


	int ichannels = img.channels();
	int irows = img.rows;
	int icols = img.cols;
	int gap = irows * icols;


	FILE * out;
	out = fopen("E:\\track\\C_Lab.txt", "a");


	unsigned char B, G, R;
	float L, a, b;

	for (int nrow = 0; nrow < irows; nrow++)
	{
		uchar * data = img.ptr<uchar>(nrow);
		for (int ncol = 0; ncol < icols; ncol++)
		{
		
			B = img.at<Vec3b>(nrow, ncol)[0];
			G = img.at<Vec3b>(nrow, ncol)[1];
			R = img.at<Vec3b>(nrow, ncol)[2];

			RGBToLab(R, G, B, &L, &a, &b);
			fprintf(out, "%f %f %f\n", L, a, b);
		}
	}
	fclose(out);

}

Python

from skimage import color
import numpy as np
import cv2

xyz_from_rgb = np.array([[0.412453, 0.357580, 0.180423],
                         [0.212671, 0.715160, 0.072169],
                         [0.019334, 0.119193, 0.950227]])

'''
正常来说,L, a, b 每个取 x,y,z的分量和应该为1,但这里不为1,所以就有一个除以的操作;
(0.95047, 1.0, 1.08883) 就是分别对应L, a, b的和;【不完全是和,但为了和接口一致。。。】

接下来三行就是处理函数
f(t) = 
        t^(1./3.),                        if t > (6/29)^3
        1./3. * (29./6.)^2 * t + 4./29.,  otherwise
'''
def xyz2lab(xyz):
    #arr = xyz / np.array([0.950456, 1.0, 1.088754])
    arr = xyz / np.array([0.95047, 1., 1.08883])

    mask = arr > 0.008856
    arr[mask] = np.power(arr[mask], 1./3.)
    arr[~mask] = 7.787 * arr[~mask] + 4./29.

    x, y, z = arr[..., 0], arr[..., 1], arr[..., 2]

    L = (116. * y) - 16
    a = 500.0 * (x - y)
    b = 200.0 * (y - z)

    return np.concatenate([x[..., np.newaxis] for x in [L, a, b]], axis = -1)

'''
中间的处理,是用来对图像进行非线性色调标记的
目的是提高图像对比度。
这个函数不是唯一的。
'''
def rgb2xyz(rgb):
    arr = np.float64(rgb/255.0)

    mask = arr > 0.04045
    arr[mask] = np.power((arr[mask] + 0.055) / 1.055, 2.4)
    arr[~mask] /= 12.92

    return np.dot(arr, xyz_from_rgb.T)


def rgb2lab(rgb):
    return xyz2lab(rgb2xyz(rgb))

if __name__ == '__main__':
    im = cv2.imread('E:\\Self\\BackGround\\test.jpeg')
    
    im_system = color.rgb2lab(im[..., ::-1])

    im_self = rgb2lab(im[..., ::-1])
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值