参考算法:

闲人阿发伯的博客

// define head function
#ifndef PS_ALGORITHM_H_INCLUDED
#define PS_ALGORITHM_H_INCLUDED #include <iostream>
#include <string>
#include "cv.h"
#include "highgui.h"
#include "cxmat.hpp"
#include "cxcore.hpp" using namespace std;
using namespace cv; void Show_Image(Mat&, const string &); #endif // PS_ALGORITHM_H_INCLUDED /*
This program will transform
the color image to Black-whithe
image. */ #include "PS_Algorithm.h"
#include <time.h> using namespace std;
using namespace cv; int main(void)
{
string Img_name("9.jpg");
Mat Img_in;
Img_in=imread(Img_name);
Show_Image(Img_in, Img_name); Mat Img_out(Img_in.size(), CV_32FC3);
Img_in.convertTo(Img_out, CV_32FC3); Mat R(Img_in.size(),CV_32FC1);
Mat G(Img_in.size(),CV_32FC1);
Mat B(Img_in.size(),CV_32FC1); Mat I(Img_in.size(),CV_32FC1); Mat BW_out(Img_in.size(), CV_32FC1); Mat rgb[]={B, G, R};
cv::split(Img_out, rgb); I=B+G+R; float maxVal, minVal, midVal; float color_ratio[6]={0.4,0.6,0.4,0.6,0.2,0.8};
float r_max_mid, r_max;
int Ind; for(int i=0; i<I.rows; i++)
{
for(int j=0; j<I.cols; j++)
{
maxVal=std::max(R.at<float>(i,j), std::max(G.at<float>(i,j),
B.at<float>(i,j)));
minVal=std::min(R.at<float>(i,j), std::min(G.at<float>(i,j),
B.at<float>(i,j)));
midVal=I.at<float>(i,j)-maxVal-minVal; if(minVal==R.at<float>(i,j))
{
Ind=0;
}
else if(minVal==G.at<float>(i,j))
{
Ind=2;
}
else
{
Ind=4;
}
r_max_mid=color_ratio[(Ind+3)%6+1]; if(maxVal==R.at<float>(i,j))
{
Ind=1;
}
else if(maxVal==G.at<float>(i,j))
{
Ind=3;
}
else
{
Ind=5;
} r_max=color_ratio[Ind]; BW_out.at<float>(i,j)=(maxVal-midVal)*r_max+(midVal-minVal)
*r_max_mid+minVal; }
} BW_out=BW_out/255;
Show_Image(BW_out, "out"); imwrite("out.jpg", BW_out*255); waitKey();
cout<<"All is well."<<endl; } // define the show image
#include "PS_Algorithm.h"
#include <iostream>
#include <string> using namespace std;
using namespace cv; void Show_Image(Mat& Image, const string& str)
{
namedWindow(str.c_str(),CV_WINDOW_AUTOSIZE);
imshow(str.c_str(), Image); }

原图

效果图

OpenCV——黑白调整的更多相关文章

  1. Opencv学习笔记4:Opencv处理调整图片亮度和对比度

    一.理论基础 在数学中我们学过线性理论,在图像亮度和对比度调节中同样适用,看下面这个公式: 在图像像素中其中: 参数f(x)表示源图像像素. 参数g(x) 表示输出图像像素. 参数a(需要满足a> ...

  2. OpenCV尺寸调整

    #include<cv.h> #include<highgui.h> int main(int argc, char** argv) { IplImage* img = cvL ...

  3. OpenCV——饱和度调整

    参考: 闲人阿发伯的博客 // define head function #ifndef PS_ALGORITHM_H_INCLUDED #define PS_ALGORITHM_H_INCLUDED ...

  4. PS 图像调整算法——黑白

    这个算法是参考自 阿发伯 的博客: http://blog.csdn.net/maozefa 黑白调整 Photoshop CS的图像黑白调整功能,是通过对红.黄.绿.青.蓝和洋红等6种颜色的比例调节 ...

  5. UI设计师需要熟记的45个快捷键Windows、Mac

    大家都知道PS快捷键很多,其实没必要都记住,今天为大家整理了45个比较实用的,别忘了收藏. 图层 填充图层 MAC: Alt+Backspace (前景) or Cmd+Backspace (背景) ...

  6. Delphi图像处理 -- 文章索引

    转载:http://blog.csdn.net/maozefa/article/details/7188354 本文对已发布<Delphi图像处理>系列文章进行索引链接,以方便阅读和查找. ...

  7. qt使用中的一些问题(linux)

    ui_xxx.h的问题 工程中(工作空间)中包含的ui_xxx.h文件是系统自动生成的.这个是xxx.ui的创建文件来的,xxx.ui界面上的都是ui_xxx.h在控制的,在项目里是不显示这个头文件的 ...

  8. OpenCV-Python 直方图-2:直方图均衡 | 二十七

    目标 在本节中, 我们将学习直方图均衡化的概念,并利用它来提高图像的对比度. 理论 考虑这样一个图像,它的像素值仅局限于某个特定的值范围.例如,较亮的图像将把所有像素限制在高值上.但是一幅好的图像会有 ...

  9. 用OpenCV实现Photoshop算法(三): 曲线调整

    http://blog.csdn.net/c80486/article/details/52499919 系列文章: 用OpenCV实现Photoshop算法(一): 图像旋转 用OpenCV实现Ph ...

随机推荐

  1. Ubuntu 12.04使用uginx+fastcgi-mono-server2部署asp.net 网站

    Ubuntu 12.04使用uginx+fastcgi-mono-server2部署asp.net 网站 1.安装nginx和mono-fastcgi-server2 sodu apt-get  in ...

  2. Just a Hook(线段树)

    Just a Hook Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

  3. elasticsearch从入门到出门-06-剖析Elasticsearch的基础分布式架构

    这个图来自中华石杉:

  4. antd-mobile的例子--cnode

    antd-mobile    简单的例子 预览地址 https://shenggen1987.github.io/antd-mobile-roadhog/#/crm/pages/users githu ...

  5. Django模型系统——ORM校园管理系统代码

    1.models.py from django.db import models # Create your models here. class Class(models.Model): id = ...

  6. 牛客小白月赛1 C 分元宵【快速幂】

    题目链接 https://www.nowcoder.com/acm/contest/85/C 思路 有 A 种 元宵馅,B 种元宵皮 所以 我们可以认为 有Q = A * B 种 元宵 有 C 张桌子 ...

  7. python中的id

    python对象都有三个特性分别是身份.类型.值,身份指该对象内存地址,内建函数id()可获得身份,类似于指针的地址,但不能控制这个值,类型决定对象可以保存什么类型的值,值是对象表示的数据项,pyth ...

  8. [原创]java WEB学习笔记14:JSP的9 个隐含对象 及 JSP 的基本语法

    本博客为原创:综合 尚硅谷(http://www.atguigu.com)的系统教程(深表感谢)和 网络上的现有资源(博客,文档,图书等),资源的出处我会标明 本博客的目的:①总结自己的学习过程,相当 ...

  9. poj 3264 Balanced Lineup【RMQ-ST查询区间最大最小值之差 +模板应用】

    题目地址:http://poj.org/problem?id=3264 Sample Input 6 3 1 7 3 4 2 5 1 5 4 6 2 2 Sample Output 6 3 0分析:标 ...

  10. hiho一下 第四十八周 拓扑排序·二【拓扑排序的应用 + 静态数组 + 拓扑排序算法的时间优化】

    题目1 : 拓扑排序·二 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 小Hi和小Ho所在学校的校园网被黑客入侵并投放了病毒.这事在校内BBS上立刻引起了大家的讨论,当 ...