OpenCV——LCC(Local Color Correction)的Python复现

本文解析了《LocalColorCorrection》论文,介绍了使用C++和Python实现的局部色彩校正方法,通过计算和调整图像像素值,实现色彩增强和校准。涉及步骤包括灰度转换、高斯模糊和指数变换,适用于图像处理和视觉效果应用。
摘要由CSDN通过智能技术生成

前言

Python代码

def LCC_ColorImg(src):
    rows = np.shape(src)[0]
    cols = np.shape(src)[1]
    
    I = np.arange(rows * cols).reshape((rows, cols))
    inv_I = np.arange(rows * cols).reshape((rows, cols))
    Mast = np.empty([rows, cols, 1], dtype=np.uint8)
    for i in range(0, rows):
        data1 = Mast[i]
        for j in range(0, cols):
            I[i][j] = (src[i, j][0] + src[i, j][1] + src[i, j][2]) / 3.0
            inv_I[i][j] = 255
            data1[j] = inv_I[i][j] - I[i][j]
  
    Mast = cv2.GaussianBlur(Mast, (41, 41), 0)
    dst = np.empty([rows, cols, 3], dtype=np.uint8)
    for i in range(0, rows):
        data2 = Mast[i]
        for j in range(0, cols):
            for k in range(0, 3):
                Exp = math.pow(2, (128 - data2[j]) / 128.0)
                value = int(255 * math.pow(src[i, j][k] / 255.0, Exp))
                dst[i, j][k] = value
                
    return dst

参考的C++代码

Mat LCC(const Mat &src){
    int rows = src.rows;
    int cols = src.cols;
    int **I;
    I = new int *[rows];
    for(int i = 0; i < rows; i++){
        I[i] = new int [cols];
    }
    int **inv_I;
    inv_I = new int *[rows];
    for(int i = 0; i < rows; i++){
        inv_I[i] = new int [cols];
    }
    Mat Mast(rows, cols, CV_8UC1);
    for(int i = 0; i < rows; i++){
        uchar *data = Mast.ptr<uchar>(i);
        for(int j = 0; j < cols; j++){
            I[i][j] = (src.at<Vec3b>(i, j)[0] + src.at<Vec3b>(i, j)[1] + src.at<Vec3b>(i, j)[1]) / 3.0;
            inv_I[i][j] = 255;
            *data = inv_I[i][j] - I[i][j];
            data++;
        }
    }
    GaussianBlur(Mast, Mast, Size(41, 41), BORDER_DEFAULT);
    Mat dst(rows, cols, CV_8UC3);
    for(int i = 0; i < rows; i++){
        uchar *data = Mast.ptr<uchar>(i);
        for(int j = 0; j < cols; j++){
            for(int k = 0; k < 3; k++){
                float Exp = pow(2, (128 - data[j]) / 128.0);
                int value = int(255 * pow(src.at<Vec3b>(i, j)[k] / 255.0, Exp));
                dst.at<Vec3b>(i, j)[k] = value;
            }
        }
    }
    return dst;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值