OpenCV實現(xiàn)圖像轉換為漫畫效果
更新時間:2020年08月19日 15:03:03 作者:瘋狂的小豬oO
這篇文章主要為大家詳細介紹了OpenCV實現(xiàn)圖像轉換為漫畫效果,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
本文實例為大家分享了OpenCV實現(xiàn)圖像轉換為漫畫的具體代碼,供大家參考,具體內(nèi)容如下
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; }
原圖為:
效果圖為:
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
C++函數(shù)返回值為對象時,構造析構函數(shù)的執(zhí)行細節(jié)
C++函數(shù)返回值為對象時,構造析構函數(shù)的執(zhí)行細節(jié),需要的朋友,可以參考下2013-02-02pybind11: C++ 工程提供 Python 接口的實例代碼
這篇文章主要介紹了pybind11: C++ 工程如何提供 Python 接口,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-09-09探討:將兩個鏈表非降序合并為一個鏈表并依然有序的實現(xiàn)方法
本篇文章是對將兩個鏈表非降序合并為一個鏈表并依然有序的實現(xiàn)方法進行了詳細的分析介紹,需要的朋友參考下2013-05-05