opencv 彩色图像亮度、对比度调节 直方图均衡化

摘要:
直接代码:1#include<Windows h>2#include<iostream>//forstandI/O3#include<string>//forstring 4#include˂iomanip>//用于控制浮点打印精度5#include<ssstream>//stringnumberconversion6#include˂cmath>

直接上代码:

 1 #include <Windows.h>
 2 #include <iostream>// for stand I/O
 3 #include <string> // for strings
 4 #include <iomanip> // for controlling float print precision
 5 #include <sstream> // string to number conversion
 6 #include <cmath>
 7 #include <ctype.h>
 8 #include <opencv2/opencv.hpp>
 9 
10 using namespace std;
11 using namespace cv;
12 
13 int main()
14 {
15     Mat                m_frame;    
16     Mat                m_ycrcb;
17     Mat                m_result;
18     Mat                m_result2;
19     VideoCapture    m_Cap;
20     int                m_nWidth = 640;
21     int                m_nHeight = 480;
22 
23     double alpha = 2.0; /**< Simple contrast control */
24     int beta = 0;  /**< Simple brightness control */
25     /// Initialize values
26     std::cout<<" Basic Linear Transforms "<<std::endl;
27     std::cout<<"-------------------------"<<std::endl;
28     std::cout<<"* Enter the alpha value [1.0-3.0]: ";std::cin>>alpha;
29     std::cout<<"* Enter the beta value [0-100]: "; std::cin>>beta;
30 
31     m_Cap.open(0);
32 
33     bool bSuccess1 = false,bSuccess2 = false;
34     bSuccess1 = m_Cap.set(CV_CAP_PROP_FRAME_WIDTH,m_nWidth);
35     bSuccess2 = m_Cap.set(CV_CAP_PROP_FRAME_HEIGHT,m_nHeight);
36     if (bSuccess1==false || bSuccess2==false)
37     { return -1;}
38 
39     m_Cap>>m_frame;
40     if (m_frame.empty())
41     {
42         return -1;
43     }
44 
45     //用来存储各通道图片的向量
46     vector<Mat> splitBGR(m_frame.channels());
47     vector<Mat> splitYCrCb(m_frame.channels());
48 
49     Mat new_image = Mat::zeros( m_frame.size(), m_frame.type() );
50 
51     for (;;)
52     {
53         m_Cap>>m_frame;
54         if (m_frame.empty())
55         { return -1;    }
56 
57         //方法一:直方图均衡化
58         //分割通道,存储到splitBGR中
59         split(m_frame,splitBGR);
60         //对各个通道分别进行直方图均衡化
61         for(int i=0; i<m_frame.channels(); i++)
62             equalizeHist(splitBGR[i],splitBGR[i]);
63         //合并通道
64         merge(splitBGR,m_result);
65 
66         //方法二: 直方图均衡化
67         //转化为ycrcb
68         cvtColor(m_frame,m_ycrcb,CV_BGR2YCrCb);
69         split(m_ycrcb,splitYCrCb);
70         equalizeHist(splitYCrCb[0],splitYCrCb[0]);
71         merge(splitYCrCb,m_ycrcb);
72         cvtColor(m_ycrcb,m_result2,CV_YCrCb2BGR);
73         
74 
75         //方法三:
76         //g(i,j) = alpha * f(i,j) + beta
77         for( int y = 0; y < m_frame.rows; y++ ) {
78             for( int x = 0; x < m_frame.cols; x++ ) {
79                 for( int c = 0; c < 3; c++ ) {
80                     new_image.at<Vec3b>(y,x)[c] =
81                         saturate_cast<uchar>( alpha*( m_frame.at<Vec3b>(y,x)[c] ) + beta );
82                 }
83             }
84         }
85 
86         imshow("原图",m_frame);
87         imshow("对比度1",m_result);
88         imshow("对比度2",m_result2);
89         imshow("对比度3",new_image);
90 
91         int c = waitKey(33);
92         if (c==27)//ESC退出
93         {    break;    }
94 
95     }
96     
97     return 0;
98 }

参考网址:http://www.opencv.org.cn/opencvdoc/2.3.2/html/doc/tutorials/core/basic_linear_transform/basic_linear_transform.html

免责声明:文章转载自《opencv 彩色图像亮度、对比度调节 直方图均衡化》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇RMAN 系列(四) RMAN 备份单片机的堆和栈(Heap &amp;amp; Stack)详解下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

直方图均衡化

直方图均衡化 clc, clear, close all img = rgb2gray(imread('/Users/jh/Wallpapers/2.jpg')); imshow(img); % 计算直方图 [m, n] = size(img); s1 = zeros(1, 256); for i = 1:m for j = 1:n...

使用局部标准差实现图像的局部对比度增强算法。

图像的对比度增强算法在很多场合都有着重要的应用,特别是在医学图像上,这是因为在众多疾病的诊断中,医学图像的视觉检查时很有必要的。而医学图像由于本身及成像条件的限制,图像的对比度很低。因此,在这个方面已经开展了很多的研究。这种增强算法一般都遵循一定的视觉原则。众所周知,人眼对高频信号(边缘处等)比较敏感。虽然细节信息往往是高频信号,但是他们时常嵌入在大量的低...