OpenCV实现图像转换为漫画效果
程序员文章站
2022-03-07 12:43:24
本文实例为大家分享了opencv实现图像转换为漫画的具体代码,供大家参考,具体内容如下from 《opencv by example》1、先canny提取图像的边缘并强化,翻转边缘为黑色,将像素值转换...
本文实例为大家分享了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; }
原图为:
效果图为:
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
上一篇: 中文姓名笔画计算(VBS脚本版)