Opencv第四讲:分离合并通道

前言:

开发环境:win10 Visual Studio 2017

开发语言:C++

编译器:Debug X64

Opencv版本:4.1.1

代码实现:

#include <iostream>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>

using namespace cv;
using namespace std;

bool MultiChannelBlending();

int main()
{
	system("color 0F");

	if (MultiChannelBlending())
	{
		cout << endl << "OK! Please start your performance ! ";
	}

	while (1)
	{
		if (waitKey(100) == 27)
			break;
	}
}

bool MultiChannelBlending()
{
	Mat Background, Bull, Rocket, Celts, Toronto, NewYork, Pelicans;
	Mat  BlueChannelsBackground, BlueChannelsBull, BlueChannelsSpur;
	Mat  GreenChannelsBackground, GreenChannelsCelts, GreenChannelsToronto;
	Mat  RedChannelsBackground,RedChannelsNewYork, RedChannelsPelicans;
	vector<Mat>ChannelsBackground, ChannelsBull, ChannelsSpur, ChannelsCelts, ChannelsToronto, ChannelsNewYork, ChannelsPelicans;

	//【1】读入图片
	Background = imread("D:\\vs2017_Project\\5th-Separate_and_combine_channels\\背景.png");
	Bull = imread("D:\\vs2017_Project\\5th-Separate_and_combine_channels\\bull.png");
	Rocket = imread("D:\\vs2017_Project\\5th-Separate_and_combine_channels\\火箭.png");
	Celts = imread("D:\\vs2017_Project\\5th-Separate_and_combine_channels\\凯尔特人.png");
	Toronto = imread("D:\\vs2017_Project\\5th-Separate_and_combine_channels\\猛龙.png");
	NewYork = imread("D:\\vs2017_Project\\5th-Separate_and_combine_channels\\NewYork.png");
	Pelicans = imread("D:\\vs2017_Project\\5th-Separate_and_combine_channels\\鹈鹕.png");

	//【2】把一个3通道图像转换成3个单通道图像
	split(Background, ChannelsBackground);//分离色彩通道
	split(Bull, ChannelsBull);//分离色彩通道
	split(Rocket, ChannelsSpur);//分离色彩通道
	split(Celts, ChannelsCelts);//分离色彩通道
	split(Toronto, ChannelsToronto);//分离色彩通道
	split(NewYork, ChannelsNewYork);//分离色彩通道
	split(Pelicans, ChannelsPelicans);//分离色彩通道

	//【3】将原图的某通道引用返回给imageBlueChannel,注意是引用,相当于两者等价,修改其中一个另一个跟着变
	// 【3-1】蓝色通道
	BlueChannelsBackground = ChannelsBackground.at(0);
	BlueChannelsBull = ChannelsBull.at(2);
	BlueChannelsSpur = ChannelsSpur.at(0);
	// 【3-2】绿色通道
	GreenChannelsBackground = ChannelsBackground.at(1);
	GreenChannelsCelts = ChannelsCelts.at(1);
	GreenChannelsToronto = ChannelsToronto.at(1);
	// 【3-3】红色通道
	RedChannelsBackground = ChannelsBackground.at(2);
	RedChannelsNewYork = ChannelsNewYork.at(2);
	RedChannelsPelicans = ChannelsPelicans.at(2);

	//【4】将原图的某通道的某坐标处下的一块区域和logo图进行加权操作,将得到的混合结果存到对应背景图的通道中
	// 【4-1】蓝色通道
	addWeighted(BlueChannelsBackground(Rect(0, 220, Bull.cols, Bull.rows)), 1.0, BlueChannelsBull, 0.5, 0,BlueChannelsBackground(Rect(0, 220, Bull.cols, Bull.rows)));
	addWeighted(BlueChannelsBackground(Rect(0, 0, Rocket.cols, Rocket.rows)), 1.0, BlueChannelsSpur, 0.5, 0,BlueChannelsBackground(Rect(0, 0, Rocket.cols, Rocket.rows)));
	// 【4-2】绿色通道
	addWeighted(GreenChannelsBackground(Rect(220, 0, Celts.cols, Celts.rows)), 1.0,GreenChannelsCelts, 0.5, 0, GreenChannelsBackground(Rect(220, 0, Celts.cols, Celts.rows)));
	addWeighted(GreenChannelsBackground(Rect(220, 220, Toronto.cols, Toronto.rows)), 1.0,GreenChannelsToronto, 0.5, 0, GreenChannelsBackground(Rect(220, 220, Toronto.cols, Toronto.rows)));
	// 【4-3】红色通道
	addWeighted(RedChannelsBackground(Rect(440, 0, NewYork.cols, NewYork.rows)), 1.0, RedChannelsNewYork, 0.5, 0, RedChannelsBackground(Rect(440, 0, NewYork.cols, NewYork.rows)));
	addWeighted(RedChannelsBackground(Rect(440, 220, Pelicans.cols, Pelicans.rows)), 1.0, RedChannelsPelicans, 0.5, 0, RedChannelsBackground(Rect(440, 220, Pelicans.cols, Pelicans.rows)));

	//【5】将三个单通道重新合并成一个三通道
	merge(ChannelsBackground, Background);  // 注意,这里是将三个合并后的单通道再次合并为一个三通道,只需要一步操作!

	//【6】显示效果图
	imshow("NBA", Background);
	

	return true;
}

结果:

参考资料:

https://blog.csdn.net/poem_qianmo/article/details/21176257

https://blog.csdn.net/gdfsg/article/details/50927257

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值