您的位置:首页 > 运维架构

Opencv改进型图片素描风(实时摄像头+Sobel边缘检测+三通道图片Mat上叠加另外一个单通道图片Mat)

2016-09-30 16:40 411 查看
惯例效果图震楼





感觉物体比人看上去好看多了。。。摔!!

惯例opencv配置环境地址:http://blog.csdn.net/zmdsjtu/article/details/52235056

实现代码如下:

#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/core/core.hpp>
using namespace cv;
void sumiao(Mat &image);
int main() {
VideoCapture cap(0);
Mat frame;
while (waitKey(30) != 27)
{
cap >> frame;
sumiao(frame);

}
}
void sumiao(Mat &image) {

Mat image2 = image.clone();
cvtColor(image, image, CV_BGR2GRAY);//灰度图
Mat sobel_x, sobel_y;
Sobel(image, sobel_x, CV_16S, 1, 0); // Sobel
Sobel(image, sobel_y, CV_16S, 0, 1);
Mat sobel;
sobel = abs(sobel_x) + abs(sobel_y);

double sobmin, sobmax;
minMaxLoc(sobel, &sobmin, &sobmax);
Mat sobelImage;
sobel.convertTo(sobelImage, CV_8U, -255.0 / sobmax, 255);

int 倍数 = 2;
cv::resize(sobelImage, sobelImage, Size(640/倍数, 480/倍数));

for (int i = 0; i < sobelImage.rows; ++i) {
for (int j = 0; j < sobelImage.cols; ++j) {
image2.data[3*(j + i*image2.cols+image2.cols-sobelImage.cols)] = sobelImage.data[j + i*sobelImage.cols];
image2.data[3 * (j + i*image2.cols + image2.cols - sobelImage.cols)+1] = sobelImage.data[j + i*sobelImage.cols];
image2.data[3 * (j + i*image2.cols + image2.cols - sobelImage.cols)+2] = sobelImage.data[j + i*sobelImage.cols];
}

}

imshow("素描图", image2);

}


最后显示图片的时候在三通道的彩色图上叠加了单通道的灰度图,代码如下:

image2.data[3*(j + i*image2.cols+image2.cols-sobelImage.cols)] = sobelImage.data[j + i*sobelImage.cols];
image2.data[3 * (j + i*image2.cols + image2.cols - sobelImage.cols)+1] = sobelImage.data[j + i*sobelImage.cols];
image2.data[3 * (j + i*image2.cols + image2.cols - sobelImage.cols)+2] = sobelImage.data[j + i*sobelImage.cols];
三通道对应像素点直接都赋值为单通道的值就好



最后祝大家opencv使用愉快~
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  opencv 素描风