2023.10.19
Today's agenda
#include <iostream>
#include "opencv2/opencv.hpp"
using namesapce std;
using namespace cv;
int main(){
Mat src = imread("lenna.bmp", IMREAD_GREYSCALE);
if(src.empty()){
cerr << "Image load failed" << "\n";
}
Mat dst1 = src - Scalar(50); // darker
add(src, 50, dst1); // darker(same result)
Mat dst2 = src + Scalar(50); // brighter
add(src, -50, dst2) // brighter(same result)
Mat dst3 = Scalar(255) - src; // inverse image
imshow("src", src);
imshow("dst1", dst1);
imshow("dst2", dst2);
imshow("dst3", dst3);
waitKey();
}
If want to change brightness of the image just simple function you have
but in color image, - + operator just can not change the brightness. because color image has 3 or 4 channels for presents color. if you want to change brightness in color image, Scalar must needed.
#include <iostream>
#include "opencv2/opencv.hpp"
using namesapce std;
using namespace cv;
int main(){
Mat src = imread("lenna.bmp", IMREAD_GREYSCALE);
if(src.empty()){
cerr << "Image load failed" << "\n";
}
float alpha = 1.0f;
int m = (int)mean(src)[0];
Mat dst = src + (src - m) * alpha;
imshow("src", src);
imshow("dst", dst);
waitKey();
}
In this sample code, given alpha value is 1.0f. But this method is not quite run a good result.
Actually get good constrast image is get m(average constrat value) in image and give a line to (m, m) line
Histogram : Given image's pixel distribution present to such a graph
but histogram can not get each pixel's location
#include <iostream>
#include "opencv2/opencv.hpp"
using namespace std;
using namespace cv;
Mat calcGrayHist(const Mat& img)
{
CV_Assert(img.type() == CV_8U);
Mat hist;
int channels[] = { 0 };
int dims = 1;
const int histSize[] = { 256 };
float graylevel[] = { 0, 256 };
const float* ranges[] = { graylevel };
calcHist(&img, 1, channels, noArray(), hist, dims, histSize, ranges);
return hist;
}
Mat getGrayHistImage(const Mat& hist)
{
CV_Assert(hist.type() == CV_32FC1);
CV_Assert(hist.size() == Size(1, 256));
double histMax = 0.;
minMaxLoc(hist, 0, &histMax);
Mat imgHist(100, 256, CV_8UC1, Scalar(255));
for (int i = 0; i < 256; i++) {
line(imgHist, Point(i, 100),
Point(i, 100 - cvRound(hist.at<float>(i, 0) * 100 / histMax)), Scalar(0));
}
return imgHist;
}
int main()
{
Mat src = imread("lenna.bmp", IMREAD_GRAYSCALE);
if (src.empty()) {
cerr << "Image load failed!" << endl;
return -1;
}
Mat hist = calcGrayHist(src);
Mat imgHist = getGrayHistImage(hist);
imshow("src", src);
imshow("hist", imgHist);
waitKey();
}
#include <iostream>
#include "opencv2/opencv.hpp"
using namespace std;
using namespace cv;
Mat calcGrayHist(const Mat& img)
{
CV_Assert(img.type() == CV_8U);
Mat hist;
int channels[] = { 0 };
int dims = 1;
const int histSize[] = { 256 };
float graylevel[] = { 0, 256 };
const float* ranges[] = { graylevel };
calcHist(&img, 1, channels, noArray(), hist, dims, histSize, ranges, true);
return hist;
}
Mat getGrayHistImage(const Mat& hist)
{
CV_Assert(!hist.empty());
CV_Assert(hist.type() == CV_32F);
double histMax = 0.;
minMaxLoc(hist, 0, &histMax);
Mat imgHist(100, 256, CV_8UC1, Scalar(255));
for (int i = 0; i < 256; i++) {
line(imgHist, Point(i, 100),
Point(i, 100 - cvRound(hist.at<float>(i) * 100 / histMax)), Scalar(0));
}
return imgHist;
}
int main()
{
Mat src = imread("lenna.bmp", IMREAD_GRAYSCALE);
if (src.empty()) {
cerr << "Image load failed!" << endl;
return -1;
}
double gmin, gmax;
minMaxLoc(src, &gmin, &gmax);
Mat dst = (src - gmin) * 255 / (gmax - gmin);
imshow("src", src);
imshow("dst", dst);
imshow("hist_src", getGrayHistImage(calcGrayHist(src)));
imshow("hist_dst", getGrayHistImage(calcGrayHist(dst)));
waitKey();
}