opencv分离RGB三通道

注:本文非标准教程,仅是总结个人学习过程,可能存在纰漏,如有错误之处欢迎留言告知,非常感谢

本文用RGB通道来改变图像颜色。

opencv分离RGB三通道_第1张图片

#include 
#include 
#include 
#include 
#include 

using namespace std;
using namespace cv;
int R = 10, G = 10, B = 10;
static Mat frame;
//1//
//1//
//2//
vector<Mat> channels;
Mat channels1, channels2, channels3;
Mat img_color_channel;
vector<Mat> newChannels;

//2//
static void callbackAdjust(int, void *);
int main()
{

	frame = imread("1.jpg");
	//2//
	split(frame, channels);
	channels1 = channels[0];//获取通道1
	channels2 = channels[1];//获取通道2
	channels3 = channels[2];//获取通道3

	//2//
	namedWindow("test");
	createTrackbar("R", "test", &R, 255, callbackAdjust);
	createTrackbar("G", "test", &G, 255, callbackAdjust);
	createTrackbar("B", "test", &B, 255, callbackAdjust);

	/*for (;;)
	{*/
	//cap >> frame;
	imshow("src", frame);
	callbackAdjust(0, 0);
	waitKey(0);
//}
	return 0;
}

//static void callbackAdjust(int, void *)
//{
//	//2//
//	for (int i = 0; i < frame.rows; i++)
//	{
//		for (int j = 0; j < frame.cols; j++)
//		{
//			channels3.at(i, j)[0] += B;
//			channels2.at(i, j)[1] += G;
//			channels1.at(i, j)[2] += R;
//		}
//	}
//	newChannels.push_back(channels3);
//	newChannels.push_back(channels2);
//	newChannels.push_back(channels1);
//	merge(newChannels, img_color_channel);
//	imshow("test", img_color_channel);
//	//2//
//}


static void callbackAdjust(int , void *)
{
    Mat img_color_channel(frame.rows, frame.cols, CV_8UC3);
    for (int i = 0; i < frame.rows; i++)
    {
        for (int j = 0; j < frame.cols; j++)
        {
            img_color_channel.at<Vec3b>(i,j)[2] = frame.at<Vec3b>(i,j)[2] ;
            img_color_channel.at<Vec3b>(i,j)[1] = frame.at<Vec3b>(i,j)[1] ;
            img_color_channel.at<Vec3b>(i,j)[0] = frame.at<Vec3b>(i,j)[0] ;
        }
    }


    for (int i = 0; i < frame.rows; i++)
    {
        for (int j = 0; j < frame.cols; j++)
        {
            img_color_channel.at<Vec3b>(i,j)[2] += R ;
            img_color_channel.at<Vec3b>(i,j)[1] += G ;
            img_color_channel.at<Vec3b>(i,j)[0] += B ;
        }
    }
    imshow("test", img_color_channel);
}

opencv分离RGB三通道_第2张图片
emmmmm炸了。。

所以改代码。。。。。。

#include
#include
#include

using namespace std;
using namespace cv;

int aSlider_1, aSlider_2, aSlider_3;
double Blue, Green, Red;
int Max = 100;

void on_Trackbar_1(int, void*)
{
	Blue = (double)aSlider_1;
}
void on_Trackbar_2(int, void*)
{
	Green = (double)aSlider_2;
}
void on_Trackbar_3(int, void*)
{
	Red = (double)aSlider_3;
}

bool MultiChannelBlending()
{
	while (1)
	{
		Mat frame=imread("1.jpg");
		Mat temp1(frame.rows, frame.cols, CV_8UC3, Scalar(0, 0, 0));
		Mat temp2(frame.rows, frame.cols, CV_8UC3, Scalar(0, 0, 0));
		Mat temp3(frame.rows, frame.cols, CV_8UC3, Scalar(0, 0, 0));
		Mat temp4(frame.rows, frame.cols, CV_8UC3, Scalar(0, 0, 0));

		createTrackbar("蓝%d", "red", &aSlider_1, Max, on_Trackbar_1);
		createTrackbar("绿%d", "red", &aSlider_2, Max, on_Trackbar_2);
		createTrackbar("红%d", "red", &aSlider_3, Max, on_Trackbar_3);

		for (int i = 0; i < frame.rows; i++)
		{
			for (int j = 0; j < frame.cols; j++)
			{
				int red = frame.at<Vec3b>(i, j)[2];
				temp1.at<Vec3b>(i, j)[2] = red * Red / Max;
				int blue = frame.at<Vec3b>(i, j)[0];
				temp1.at<Vec3b>(i, j)[0] = blue * Blue / Max;
				int green = frame.at<Vec3b>(i, j)[1];
				temp1.at<Vec3b>(i, j)[1] = green * Green / Max;
			}
		}

		on_Trackbar_1(aSlider_1, 0);
		on_Trackbar_2(aSlider_2, 0);
		on_Trackbar_3(aSlider_3, 0);

		imshow("girlfriend", temp1);
		imshow("girlfriend", frame);
		int num = waitKey(1);
		if ((char)num == 30)
		break;
	}
	return 0;
}
int main()
{
	if (MultiChannelBlending())
	{
		cout << endl << "\n!";
	}
	waitKey(0);
}

opencv分离RGB三通道_第3张图片

你可能感兴趣的:(opencv)