您好,登錄后才能下訂單哦!
這篇文章給大家分享的是有關(guān)OpenCV如何實現(xiàn)圖像轉(zhuǎn)換為漫畫效果的方法的內(nèi)容。小編覺得挺實用的,因此分享給大家做個參考。一起跟隨小編過來看看吧。
1、先canny提取圖像的邊緣并強化,翻轉(zhuǎn)邊緣為黑色,將像素值轉(zhuǎn)換為0-1的值
2、將圖像進行雙邊濾波處理,然后將像素值縮短為每10個灰度級為一個值
3、將前兩步得到的結(jié)果相乘,顯示結(jié)果
#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; }
原圖為:
效果圖為:
感謝各位的閱讀!關(guān)于OpenCV如何實現(xiàn)圖像轉(zhuǎn)換為漫畫效果的方法就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,讓大家可以學(xué)到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到吧!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。