2023.10.24
Today's agenda
In opencv using CV_8UC3 type can make 256 256 256 color using red, blue, green space
Actually we called RGB space in real life, but in opencv called BGR space
#include <iostream>
#include "opencv2/opencv.hpp"
using namespace std;
using namespace cv;
#define RGB2GRAY(r, g, b) ((4899*(r) + 9617*(g) + 1868*(b)) >> 14)
int main()
{
Mat src = imread("mandrill.bmp");
if (src.empty()) {
cerr << "Image laod failed!" << endl;
return;
}
#if 0
Mat dst;
cvtColor(src, dst, COLOR_BGR2GRAY);
#else
Mat dst(src.rows, src.cols, CV_8UC1);
for (int y = 0; y < src.rows; y++) {
for (int x = 0; x < src.cols; x++) {
uchar b = src.at<Vec3b>(y, x)[0];
uchar g = src.at<Vec3b>(y, x)[1];
uchar r = src.at<Vec3b>(y, x)[2];
//uchar gray = (uchar)(0.299 * r + 0.587 * g + 0.114 * b + 0.5);
//uchar gray = (uchar)((299 * r + 587 * g + 114 * b) / 1000);
//dst.at<uchar>(y, x) = gray;
dst.at<uchar>(y, x) = (uchar)RGB2GRAY(r, g, b);
}
}
#endif
imshow("src", src);
imshow("dst", dst);
waitKey();
destroyAllWindows();
}
void cvtColor(InputArray src, OutputArray dst, int code, int dstCn = 0);
parameters
#include <iostream>
#include "opencv2/opencv.hpp"
using namespace std;
using namespace cv;
void split_bgr();
void split_hsv();
void split_ycrcb();
int main()
{
split_bgr();
split_hsv();
split_ycrcb();
}
void split_bgr()
{
Mat src = imread("candies.png", IMREAD_COLOR);
if (src.empty()) {
cerr << "Image load failed!" << endl;
return;
}
vector<Mat> bgr_planes;
split(src, bgr_planes);
imshow("src", src);
imshow("B plane", bgr_planes[0]);
imshow("G plane", bgr_planes[1]);
imshow("R plane", bgr_planes[2]);
waitKey();
destroyAllWindows();
}
void split_hsv()
{
Mat src = imread("candies.png", IMREAD_COLOR);
if (src.empty()) {
cerr << "Image load failed!" << endl;
return;
}
Mat src_hsv;
cvtColor(src, src_hsv, COLOR_BGR2HSV);
vector<Mat> hsv_planes;
split(src_hsv, hsv_planes);
imshow("src", src);
imshow("H plane", hsv_planes[0]);
imshow("S plane", hsv_planes[1]);
imshow("V plane", hsv_planes[2]);
waitKey();
destroyAllWindows();
}
void split_ycrcb()
{
Mat src = imread("candies.png", IMREAD_COLOR);
if (src.empty()) {
cerr << "Image load failed!" << endl;
return;
}
Mat src_ycrcb;
cvtColor(src, src_ycrcb, COLOR_BGR2YCrCb);
vector<Mat> ycrcb_planes;
split(src_ycrcb, ycrcb_planes);
imshow("src", src);
imshow("Y plane", ycrcb_planes[0]);
imshow("Cr plane", ycrcb_planes[1]);
imshow("Cb plane", ycrcb_planes[2]);
waitKey();
destroyAllWindows();
}
Previous i leaned only do histogram equalization on greyscale image
but if i want to do histogram equalization on color image, i have to split all color space and do histogram equalization and merge
below image is doing histogram equalization on rgb image but other color type(YCrCb, HSV) also do same order.
void equalizeHist(InputArray src, OutputArray dst);
parameters
void inRange(InputArray src, InputArray lowerb, InputArray upperb, OutputArray dst);
parameters
#include <iostream>
#include <algorithm>
#include "opencv2/opencv.hpp"
using namespace std;
using namespace cv;
int pos_hue1 = 50, pos_hue2 = 80, pos_sat1 = 150, pos_sat2 = 255;
Mat src, src_hsv, dst, mask;
void on_hsv_changed(int, void*)
{
Scalar lowerb(pos_hue1, pos_sat1, 0);
Scalar upperb(pos_hue2, pos_sat2, 255);
inRange(src_hsv, lowerb, upperb, mask);
cvtColor(src, dst, COLOR_BGR2GRAY);
cvtColor(dst, dst, COLOR_GRAY2BGR);
src.copyTo(dst, mask);
imshow("mask", mask);
imshow("dst", dst);
}
int main()
{
src = imread("candies.png", IMREAD_COLOR);
if (src.empty()) {
cerr << "Image load failed!" << endl;
return -1;
}
cvtColor(src, src_hsv, COLOR_BGR2HSV);
namedWindow("src");
namedWindow("mask");
namedWindow("dst");
imshow("src", src);
createTrackbar("Lower Hue", "dst", &pos_hue1, 179, on_hsv_changed);
createTrackbar("Upper Hue", "dst", &pos_hue2, 179, on_hsv_changed);
createTrackbar("Lower Sat", "dst", &pos_sat1, 255, on_hsv_changed);
createTrackbar("Upper Sat", "dst", &pos_sat2, 255, on_hsv_changed);
on_hsv_changed(0, 0);
waitKey();
}