OpenCV多通道图像混合(六)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using OpenCvSharp;
namespace _03_多通道图像混合
{
internal class Program
{
static void Main(string[] args)
{
if (MultichangeBlending())
{
Console.WriteLine("混合成功");
}
Cv2.WaitKey(0);
}
static bool MultichangeBlending()
{
#region 多通道混合_蓝色部分
//1.加载两张图像
Mat logoImage = Cv2.ImRead(@"F:\AI视觉23班\06_OpenCV\images\dota_logo.jpg",ImreadModes.Grayscale);//加载灰度图
Mat scrImage = Cv2.ImRead(@"F:\AI视觉23班\06_OpenCV\images\dota_jugg.jpg");
if (logoImage.Data == null &&scrImage.Data == null)
{
Console.WriteLine("图像加载失败");
return false;
}
//2.把2通道图像转成3个单通道图像
Mat[] changles;
Cv2.Split(scrImage,out changles);
//3.返回原图的蓝色通道
Mat iamgeBlue= changles[0];
//4.将原图的蓝色通道 和logo图进行合
Cv2.AddWeighted(iamgeBlue[new Rect(500,250, logoImage.Cols, logoImage.Rows)]
,1.0,logoImage,0.5,0,
iamgeBlue[new Rect(500, 250, logoImage.Cols, logoImage.Rows)]);
//5.将三个单通道重新合并成一个三通道
Cv2.Merge(changles, scrImage);
//6.显示
Cv2.ImShow("合并_蓝色通道", scrImage);
#endregion
#region 多通道混合_绿色部分
//1.加载两张图像
logoImage = Cv2.ImRead(@"F:\AI视觉23班\06_OpenCV\images\dota_logo.jpg", ImreadModes.Grayscale);//加载灰度图
scrImage = Cv2.ImRead(@"F:\AI视觉23班\06_OpenCV\images\dota_jugg.jpg");
if (logoImage.Data == null && scrImage.Data == null)
{
Console.WriteLine("图像加载失败");
return false;
}
//2.把2通道图像转成3个单通道图像
Cv2.Split(scrImage, out changles);
//3.返回原图的蓝色通道
Mat iamgeGreen = changles[1];
//4.将原图的蓝色通道 和logo图进行合
Cv2.AddWeighted(iamgeGreen[new Rect(500, 250, logoImage.Cols, logoImage.Rows)]
, 1.0, logoImage, 0.5, 0,
iamgeGreen[new Rect(500, 250, logoImage.Cols, logoImage.Rows)]);
//5.将三个单通道重新合并成一个三通道
Cv2.Merge(changles, scrImage);
//6.显示
Cv2.ImShow("合并_绿色通道", scrImage);
#endregion
return true;
}
}
}