前言
這是我《OpenCV:從零到一》專欄的第六篇博客,想看跟多請戳這。
本文概要
對比度和亮度的概念
Mat new_image = Mat::zeros( image.size(), image.type() );
saturate_cast(value)
Mat.at(y,x)[index]=value
案例代碼
大概內容:調整圖像亮度對比度 。
#include <opencv2/opencv.hpp>
#include <iostream>
#include <opencv2\imgproc\types_c.h>//CV_BGR2GRAY
using namespace cv;
using std::cout;
using std::endl;
int main(int argc, char** argv) {
Mat src, dst;
src = imread("D:/86186/Documents/opencv/lena.jpg");
if (!src.data) {
printf("could not load image...\n");
return -1;
}
char input_win[] = "input image";
cvtColor(src, src, CV_BGR2GRAY);//#include <opencv2\imgproc\types_c.h>//注意是BGR 而不是RGB
namedWindow(input_win);
imshow(input_win, src);
// contrast and brigthtness changes
int height = src.rows;
int width = src.cols;
dst = Mat::zeros(src.size(), src.type());//初始化dst,全部置零
float alpha = 1.2;
float beta = 30;
Mat m1;
src.convertTo(m1, CV_32F);
//法一
double t1 = (double)getTickCount();//獲取開始時間信息
for (int row = 0; row < height; row++) {
for (int col = 0; col < width; col++) {
if (src.channels() == 3) {//三通道圖像處理
//讀取原來的像素值
float b = m1.at<Vec3f>(row, col)[0];// blue
float g = m1.at<Vec3f>(row, col)[1]; // green
float r = m1.at<Vec3f>(row, col)[2]; // red
//帶入公式輸出
dst.at<Vec3b>(row, col)[0] = saturate_cast<uchar>(b*alpha + beta);
dst.at<Vec3b>(row, col)[1] = saturate_cast<uchar>(g*alpha + beta);
dst.at<Vec3b>(row, col)[2] = saturate_cast<uchar>(r*alpha + beta);
}
else if (src.channels() == 1) {//單通道圖像處理
float v = src.at<uchar>(row, col);
dst.at<uchar>(row, col) = saturate_cast<uchar>(v*alpha + beta);
}
}
}
t1 = ((double)getTickCount() - t1) / getTickFrequency();
imshow("output1", dst);
//法二
double t2 = (double)getTickCount();//獲取開始時間信息
if (src.channels() == 3) {//三通道圖像處理
for (int row = 0; row < height; row++) {
for (int col = 0; col < width; col++) {
//讀取原來的像素值
float b = m1.at<Vec3f>(row, col)[0];// blue
float g = m1.at<Vec3f>(row, col)[1]; // green
float r = m1.at<Vec3f>(row, col)[2]; // red
//帶入公式輸出
dst.at<Vec3b>(row, col)[0] = saturate_cast<uchar>(b*alpha + beta);
dst.at<Vec3b>(row, col)[1] = saturate_cast<uchar>(g*alpha + beta);
dst.at<Vec3b>(row, col)[2] = saturate_cast<uchar>(r*alpha + beta);
}
}
}
else if (src.channels() == 1) {//單通道圖像處理
for (int row = 0; row < height; row++) {
for (int col = 0; col < width; col++) {
float v = src.at<uchar>(row, col);
dst.at<uchar>(row, col) = saturate_cast<uchar>(v*alpha + beta);
}
}
}
t2 = ((double)getTickCount() - t2) / getTickFrequency();
cout << "t1=" << t1 << endl << "t2=" << t2 << endl;
imshow("output2", dst);
waitKey(0);
return 0;
}
運行效果:
float alpha = 1.2; float beta = 30;
float alpha = 1.2; float beta = 100;
float alpha = 1.5; float beta = 0;
float alpha = 0.5; float beta = 80;
解析及注意事項
- alpha和beta都可以用來調整圖像亮度的(當他們大於0時會是圖片整體亮度增加),對於對比度的影響要分情況討論。其實很容易對比度就是黑的與白的之差的值,一個大的值和一個小的值同時乘一個大於0的數,他們的差會變大,也就是說對比度會變大,但是大的值有可能會越界,以至於使得很多原本有區別的像素變得一樣的值了,所以alpha和beta對於對比度的影響是不一定的要看圖像本身。而alpha
- Mat new_image = Mat::zeros( image.size(), image.type() ); 創建一張跟原圖像大小和類型一致的空白圖像、像素值初始化爲0
- saturate_cast(value)確保值大小範圍爲0~255之間
- Mat.at(y,x)[index]=value 給每個像素點每個通道賦值
- 圖片地址(url)可以用 / 來代替 \ 。
- 用double t1 = (double)getTickCount(); 和t1 = ((double)getTickCount() - t1) / getTickFrequency(); 獲取運行時間
- #include <opencv2\imgproc\types_c.h>//CV_BGR2GRAY未聲明的標識符的解決辦法