OpenCV得到图像的边沿图并在原图上叠加(灰度和RGB)

OpenCV得到图像的边沿

 想法是得到图像的边沿,并在叠加在原图上,为了醒目,又在RGB图像用彩色线条表示出来。代码如下:

#include <iostream>  
#include "opencv2/opencv.hpp"  
#include "opencv2/core/core.hpp"  
#include "opencv2/highgui/highgui.hpp"

using namespace cv;
using namespace std;

void plotEdge(const Mat& Image1, const Mat& Image2, Mat& overLayer);
void plotRGBEdge(const Mat& Image1, const Mat& Image2, Mat& overLayer);

int main()
{
	Mat refImage, overLayer;
	string FilePath = "pictures\\";
	string refFileName = FilePath + "timg.jpg";

	refImage = imread(refFileName);
	plotEdge(refImage, refImage, overLayer);
	plotRGBEdge(refImage, refImage, overLayer);
	return 0;
}

void plotEdge(const Mat& Image1, const Mat& Image2, Mat& overLayer){
	Mat grayImage1, grayImage2, Canny_Image2, Mask;
	cvtColor(Image1, grayImage1, COLOR_BGR2GRAY);
	cvtColor(Image2, grayImage2, COLOR_BGR2GRAY);
	Canny(grayImage2, Canny_Image2, 40, 200 * 2, 3);

	double alpha, beta;
	alpha = 0.75;
	beta = 1.0 - alpha;

	addWeighted(grayImage1, alpha, Canny_Image2, beta, 0.0, Mask);
	//overLayer = grayImage + out_Canny;
	namedWindow("重叠图", WINDOW_NORMAL);
	imshow("重叠图", Mask);
	waitKey(0);
	Mask.copyTo(overLayer);
}

void plotRGBEdge(const Mat& Image1, const Mat& Image2, Mat& overLayer){
	Mat grayImage1, grayImage2, Canny_Image2, Mask, mergeImage;
	//cvtColor(Image1, grayImage1, COLOR_BGR2GRAY);
	cvtColor(Image2, grayImage2, COLOR_BGR2GRAY);
	blur(grayImage2, grayImage2, Size(3, 3));
	Canny(grayImage2, Canny_Image2, 20, 100 * 2, 3);

	// 图像通道分离
	vector<Mat> channels;
	Mat imageBlueChannel;
	Mat imageGreenChannel;
	Mat imageRedChannel;

	split(Image1, channels);//分离色彩通道
	imageBlueChannel = channels.at(0);
	imageGreenChannel = channels.at(1);
	imageRedChannel = channels.at(2);

	double alpha, beta;
	alpha = 0.3;
	beta = 1.0 - alpha;

	addWeighted(imageRedChannel, alpha, Canny_Image2, beta, 0.0, Mask);

	// 图像通道合并
	//Mask.copyTo(imageRedChannel);
	channels.at(2) = Mask;
	merge(channels, mergeImage);

	namedWindow("重叠图", WINDOW_NORMAL);
	imshow("重叠图", mergeImage);
	waitKey(0);
	mergeImage.copyTo(overLayer);
}

 

ps:貌似没把宽高比例调整好。

参考:opencv学习(5)图像像素的访问、颜色通道的分离和融合

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值