OpenCV 为图像转换为漫画效果

From 《OpenCV By Example》

1、先canny提取图像的边缘并强化,翻转边缘为黑色,将像素值转换为0-1的值
2、将图像进行双边滤波处理,然后将像素值缩短为每10个灰度级为一个值
3、将前两步得到的结果相乘,显示结果

#include <iostream>

using namespace std;

#include "opencv2/core.hpp"
#include "opencv2/highgui.hpp"
#include "opencv2/imgproc.hpp"

using namespace cv;

int main()
{
	Mat img = imread("1.jpg");
	float radius = img.cols > img.rows ? (img.rows / 3) : (img.cols / 3);


	const double exponential_e = exp(1.0);
	/** EDgES **/
	// Apply median filter to remove possible noise
	Mat imgMedian;
	medianBlur(img, imgMedian, 7);
	// Detect edges with canny
	Mat imgCanny;
	Canny(imgMedian, imgCanny, 50, 150);
	// Dilate the edges
	Mat kernel = getStructuringElement(MORPH_RECT, Size(2, 2));
	dilate(imgCanny, imgCanny, kernel);

	// Scale edges values to 1 and invert values
	imgCanny = imgCanny / 255;
	imgCanny = 1 - imgCanny;
	// Use float values to allow multiply between 0 and 1
	Mat imgCannyf;
	imgCanny.convertTo(imgCannyf, CV_32FC3);
	// Blur the edgest to do smooth effect
	blur(imgCannyf, imgCannyf, Size(5, 5));

	/** COLOR **/
	// Apply bilateral filter to homogenizes color
	Mat imgBF;
	bilateralFilter(img, imgBF, 9, 150.0, 150.0);
	// truncate colors
	Mat result = imgBF / 25;
	result = result * 25;
	/** MERgES COLOR + EDgES **/
	// Create a 3 channles for edges
	Mat imgCanny3c;
	Mat cannyChannels[] = { imgCannyf, imgCannyf, imgCannyf };
	merge(cannyChannels, 3, imgCanny3c);
	// Convert color result to float
	Mat resultf;
	result.convertTo(resultf, CV_32FC3);
	// Multiply color and edges matrices
//	cout << imgCanny3c << endl;
	multiply(resultf, imgCanny3c, resultf);
//	cout << resultf << endl;
	// convert to 8 bits color
	resultf.convertTo(result, CV_8UC3);
	// Show image
	imshow("Result", result);

	waitKey(0);

    return 0;
}

原图为:


效果图为:



  • 4
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值