当前位置: 首页 > news >正文

[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 levelif ( 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 levelv = (int)( pow(v / 255.0, exponent) * 255.0 + 0.5 );// calculate output levelcolorTable[i] = (uchar)( v * outCoef + OutputShadow + 0.5 );}return true;
}//==================================================================
// LevelsLevels::Levels() {
}Levels::~Levels() {}bool Levels::createColorTables(uchar colorTables[][256])
{bool result = false;int i, j;//initialize color tablefor (i = 0; i < 3; i ++) {for (j = 0; j < 256; j ++)colorTables[i][j] = (uchar)j;}//create color table for each channelresult = 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 tablesif ( ! createColorTables( colorTables ) )  {//error create color table"return 1;}//adjust each pixel#ifdef HAVE_OPENMP#pragma omp parallel for#endiffor (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

相关文章:

  • 职场英语培训柯桥外语学校学外语学英语到银泰泓畅学校
  • 【Python学习手册(第四版)】学习笔记19-函数的高级话题
  • 虚拟机macos安装brew、llvm并使用cmake构建项目
  • vue3前端开发-小兔鲜项目-添加购物车操作第一步
  • 59.螺旋矩阵II54.螺旋矩阵
  • Langchain框架深度剖析:解锁大模型-RAG技术的无限潜能,引领AI应用新纪元
  • css水波浪动画效果
  • (回溯) LeetCode 46. 全排列
  • 如何用 CocosCreator 对接抖音小游戏的侧边栏复访
  • 排查MAC地址是否冲突——arping工具详解
  • MySQL中的索引——适合创建索引的情况
  • rknn yolo系列之量化前预处理,解决量化精度低以及出现类似未作nms的很多框子的问题
  • 在js中实现两个对象合并,若重复以第一个对象中的数据为准
  • 【机器学习】卷积神经网络简介
  • Android控件(示例)
  • 《用数据讲故事》作者Cole N. Knaflic:消除一切无效的图表
  • Fabric架构演变之路
  • iOS动画编程-View动画[ 1 ] 基础View动画
  • Java到底能干嘛?
  • Java多态
  • Java知识点总结(JavaIO-打印流)
  • Joomla 2.x, 3.x useful code cheatsheet
  • React 快速上手 - 06 容器组件、展示组件、操作组件
  • springboot_database项目介绍
  • SSH 免密登录
  • 创建一个Struts2项目maven 方式
  • 和 || 运算
  • 聚类分析——Kmeans
  • 深入浏览器事件循环的本质
  • Unity3D - 异步加载游戏场景与异步加载游戏资源进度条 ...
  • 数据库巡检项
  • ​【经验分享】微机原理、指令判断、判断指令是否正确判断指令是否正确​
  • ‌U盘闪一下就没了?‌如何有效恢复数据
  • ## 1.3.Git命令
  • (2)nginx 安装、启停
  • (3)STL算法之搜索
  • (cos^2 X)的定积分,求积分 ∫sin^2(x) dx
  • (Matalb回归预测)PSO-BP粒子群算法优化BP神经网络的多维回归预测
  • (八)Spring源码解析:Spring MVC
  • (二十九)STL map容器(映射)与STL pair容器(值对)
  • (附源码)springboot宠物医疗服务网站 毕业设计688413
  • (论文阅读32/100)Flowing convnets for human pose estimation in videos
  • (七)glDrawArry绘制
  • (十二)Flink Table API
  • (一)使用IDEA创建Maven项目和Maven使用入门(配图详解)
  • (转)人的集合论——移山之道
  • (转载)深入super,看Python如何解决钻石继承难题
  • .[backups@airmail.cc].faust勒索病毒的最新威胁:如何恢复您的数据?
  • .NET CLR基本术语
  • .net core 依赖注入的基本用发
  • .Net Remoting常用部署结构
  • .NET 实现 NTFS 文件系统的硬链接 mklink /J(Junction)
  • .netcore 如何获取系统中所有session_ASP.NET Core如何解决分布式Session一致性问题
  • .NET建议使用的大小写命名原则
  • .net开发引用程序集提示没有强名称的解决办法