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:貌似没把宽高比例调整好。