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

来源:互联网 发布:淘宝lol卫衣 编辑:程序博客网 时间:2024/04/29 01:44

惯例效果图震楼



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


惯例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);  // SobelSobel(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使用愉快~

4 0