OpenCV實(shí)現(xiàn)圖像轉(zhuǎn)換為漫畫效果
本文實(shí)例為大家分享了OpenCV實(shí)現(xiàn)圖像轉(zhuǎn)換為漫畫的具體代碼,供大家參考,具體內(nèi)容如下
From 《OpenCV By Example》
1、先canny提取圖像的邊緣并強(qiáng)化,翻轉(zhuǎn)邊緣為黑色,將像素值轉(zhuǎn)換為0-1的值
2、將圖像進(jìn)行雙邊濾波處理,然后將像素值縮短為每10個(gè)灰度級(jí)為一個(gè)值
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;
}
原圖為:

效果圖為:

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Python+OpenCV圖像處理——圖像二值化的實(shí)現(xiàn)
- Python+OpenCV圖像處理——實(shí)現(xiàn)輪廓發(fā)現(xiàn)
- Python+OpenCV圖像處理——實(shí)現(xiàn)直線檢測(cè)
- Python+OpenCV圖像處理—— 色彩空間轉(zhuǎn)換
- Python+OpenCV圖像處理——打印圖片屬性、設(shè)置存儲(chǔ)路徑、調(diào)用攝像頭
- OpenCV利用python來(lái)實(shí)現(xiàn)圖像的直方圖均衡化
- Python Opencv圖像處理基本操作代碼詳解
- OpenCV實(shí)現(xiàn)二值圖像的邊緣光滑處理
- Opencv常見(jiàn)圖像格式Data Type及代碼實(shí)例
相關(guān)文章
OpenGL實(shí)現(xiàn)鼠標(biāo)移動(dòng)方塊
這篇文章主要為大家詳細(xì)介紹了OpenGL實(shí)現(xiàn)鼠標(biāo)移動(dòng)方塊,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-08-08
C語(yǔ)言main函數(shù)的三種形式實(shí)例詳解
這篇文章主要介紹了 C語(yǔ)言main函數(shù)的三種形式實(shí)例詳解的相關(guān)資料,需要的朋友可以參考下2017-06-06
C語(yǔ)言編程C++柔性數(shù)組結(jié)構(gòu)示例講解
這篇文章主要介紹了C語(yǔ)言編程系列中的柔性數(shù)組,文中含有詳細(xì)的示例代碼講解,有需要的朋友可以借鑒參考下,希望能夠有所幫助2021-09-09
C++函數(shù)返回值為對(duì)象時(shí),構(gòu)造析構(gòu)函數(shù)的執(zhí)行細(xì)節(jié)
C++函數(shù)返回值為對(duì)象時(shí),構(gòu)造析構(gòu)函數(shù)的執(zhí)行細(xì)節(jié),需要的朋友,可以參考下2013-02-02
pybind11: C++ 工程提供 Python 接口的實(shí)例代碼
這篇文章主要介紹了pybind11: C++ 工程如何提供 Python 接口,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-09-09
Qt界面中滑動(dòng)條的實(shí)現(xiàn)方式
這篇文章主要介紹了Qt界面中滑動(dòng)條的實(shí)現(xiàn)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-11-11
探討:將兩個(gè)鏈表非降序合并為一個(gè)鏈表并依然有序的實(shí)現(xiàn)方法
本篇文章是對(duì)將兩個(gè)鏈表非降序合并為一個(gè)鏈表并依然有序的實(shí)現(xiàn)方法進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-05-05

