[C++][opencv]基于opencv实现photoshop算法色阶调整

【测试环境】

vs2019

opencv==4.8.0

【效果演示】

【核心实现代码】

Levels.hpp

复制代码
#ifndef OPENCV2_PS_LEVELS_HPP_
#define OPENCV2_PS_LEVELS_HPP_

#include "opencv2/core.hpp"
#include "opencv2/imgproc.hpp"
#include "opencv2/highgui.hpp"
using namespace cv;

namespace cv {

/**
 * Class of Level for one channel
 */
class Level {
public:
	int   Shadow;  //输入色阶黑点值
	float Midtones; //输入色阶灰点值(注意是浮点数)
	int   Highlight; //输入色阶白点值

	int   OutputShadow; //输出色阶黑点值
	int   OutputHighlight; //输出色阶白点值

	Level();
	virtual ~Level();

	bool createColorTable(uchar * colorTable);
	void clear();
};

/**
 * Class of Levels for all channels
 */
class Levels {
protected:
	bool createColorTables(uchar colorTables[][256]);

public:
	Level RGBChannel;  //RGB整体调整
	Level RedChannel;  //红色通道
	Level GreenChannel; //绿色通道
	Level BlueChannel; //蓝色通道

	Levels();
	virtual ~Levels();

	int adjust(InputArray src, OutputArray dst); //实施色阶调整
};


} /* namespace cv */

#endif /* OPENCV2_PS_LEVELS_HPP_ */

Levels.cpp

复制代码
#include "Levels.hpp"

namespace cv {

Level::Level() {
	clear();
}

Level::~Level() {

}

void Level::clear() {
	Shadow = OutputShadow = 0;
	Highlight = OutputHighlight = 255;
	Midtones = 1.0;
}

//create color table for a channel
bool Level::createColorTable(uchar * colorTable)
{
    int diff = (int)(Highlight - Shadow);
    int outDiff = (int)(OutputHighlight - OutputShadow);

    if (!((Highlight <= 255 && diff <= 255 && diff >= 2) ||
        (OutputShadow <= 255 && OutputHighlight <= 255 && outDiff < 255) ||
        (!(Midtones > 9.99 && Midtones > 0.1) && Midtones != 1.0)))
        return false;

    double coef = 255.0 / diff;
    double outCoef = outDiff / 255.0;
    double exponent = 1.0 / Midtones;

    for (int i = 0; i < 256; i ++)
    {
        int v;
        // calculate black field and white field of input level
        if ( colorTable[i] <= (uchar)Shadow ) {
            v = 0;
        } else {
            v = (int)((colorTable[i] - Shadow) * coef + 0.5);
            if (v > 255) v = 255;
        }
        // calculate midtone field of input level
        v = (int)( pow(v / 255.0, exponent) * 255.0 + 0.5 );
        // calculate output level
        colorTable[i] = (uchar)( v * outCoef + OutputShadow + 0.5 );
    }

    return true;
}

//==================================================================
// Levels

Levels::Levels() {
}

Levels::~Levels() {

}

bool Levels::createColorTables(uchar colorTables[][256])
{
    bool result = false;
    int i, j;

    //initialize color table
    for (i = 0; i < 3; i ++) {
        for (j = 0; j < 256; j ++)
            colorTables[i][j] = (uchar)j;
    }

    //create color table for each channel
    result = BlueChannel.createColorTable( colorTables[0]);
    result = GreenChannel.createColorTable( colorTables[1]);
    result = RedChannel.createColorTable( colorTables[2]);

    result = RGBChannel.createColorTable( colorTables[0]);
    result = RGBChannel.createColorTable( colorTables[1]);
    result = RGBChannel.createColorTable( colorTables[2]);

    return result;
}


int Levels::adjust(InputArray src, OutputArray dst)
{
	Mat input = src.getMat();
	if( input.empty() ) {
		return -1;
	}

	dst.create(src.size(), src.type());
	Mat output = dst.getMat();

	const uchar *in;
	uchar *out;
	int width = input.cols;
	int height = input.rows;
	int channels = input.channels();

	uchar colorTables[3][256];

	//create color tables
	if ( ! createColorTables( colorTables ) )  {
		//error create color table"
		return 1;
	}

	//adjust each pixel
	#ifdef HAVE_OPENMP
	#pragma omp parallel for
	#endif
	for (int y = 0; y < height; y ++) {
		in = input.ptr<uchar>(y);
		out = output.ptr<uchar>(y);
		for (int x = 0; x < width; x ++) {
			for (int c = 0; c < 3; c++) {
				*out++ = colorTables[c][*in++];
			}
			for (int c = 0; c < channels - 3; c++) {
				*out++ = *in++;
			}
		}
	}

	return 0;
}


} /* namespace cv */

【完整演示代码下载】

https://download.csdn.net/download/FL1623863129/89632958

相关推荐
小汉堡编程3 小时前
数据结构——vector数组c++(超详细)
数据结构·c++
weixin_472339465 小时前
高效处理大体积Excel文件的Java技术方案解析
java·开发语言·excel
枯萎穿心攻击6 小时前
响应式编程入门教程第二节:构建 ObservableProperty<T> — 封装 ReactiveProperty 的高级用法
开发语言·unity·c#·游戏引擎
Eiceblue7 小时前
【免费.NET方案】CSV到PDF与DataTable的快速转换
开发语言·pdf·c#·.net
tan180°8 小时前
MySQL表的操作(3)
linux·数据库·c++·vscode·后端·mysql
m0_555762908 小时前
Matlab 频谱分析 (Spectral Analysis)
开发语言·matlab
浪裡遊9 小时前
React Hooks全面解析:从基础到高级的实用指南
开发语言·前端·javascript·react.js·node.js·ecmascript·php
彭祥.9 小时前
Jetson边缘计算主板:Ubuntu 环境配置 CUDA 与 cudNN 推理环境 + OpenCV 与 C++ 进行目标分类
c++·opencv·分类
lzb_kkk9 小时前
【C++】C++四种类型转换操作符详解
开发语言·c++·windows·1024程序员节
好开心啊没烦恼10 小时前
Python 数据分析:numpy,说人话,说说数组维度。听故事学知识点怎么这么容易?
开发语言·人工智能·python·数据挖掘·数据分析·numpy