Logo elodees  elodees

A caring AI for a better world













Only alphabetic characters accented or not as well as the space are accepted

Logo IA




Cartoon





No account yet ?

Sign up to access all content


Tested in Anaconda and Python 3.7

# -*- coding: utf-8 -*-
"""
Created on Fri Jun 24 12:42:57 2022
 
@author: http://www.juzicode.com/python-funny-opencv-cartoon-profile-photo/
"""
import cv2
import os,sys,time
 
from matplotlib import pyplot as plt
 
def showimage(myimage, figsize=[10,10]):
    if (myimage.ndim>2):  #This only applies to RGB or RGBA images (e.g. not to Black and White images)
        myimage = myimage[:,:,::-1] #OpenCV follows BGR order, while matplotlib likely follows RGB order
 
    fig, ax = plt.subplots(figsize=figsize)
    ax.imshow(myimage, cmap = 'gray', interpolation = 'bicubic')
    plt.xticks([]), plt.yticks([])  # to hide tick values on X and Y axis
    plt.show()
 
def cartoon(fn_raw,edge='Canny'):
 
    #Lire l'image
    img_raw = cv2.imread(fn_raw)     
 
    #Agrandir l'image
    HEIGHT_MAX = 500
    height,width,_ = img_raw.shape
    ration = HEIGHT_MAX/height
    width = int(width * ration)
    height = HEIGHT_MAX
    print('width,height:',width,height)
    img_resize = cv2.resize(img_raw,(width,height))   
    showimage(img_resize)
 
    #Niveaux de gris
    img_gray = cv2.cvtColor(img_resize,cv2.COLOR_BGR2GRAY)
    #去噪
    img_blur = cv2.medianBlur(img_gray,5)
    #img_blur = cv2.blur(img_gray,(5,5))
    showimage(img_blur)
 
    #Détection des contours
    if edge == 'Laplacian':
        img_edge = cv2.Laplacian(img_blur,cv2.CV_8U,ksize=5)
    elif edge == 'Scharr':
        img_edge1=cv2.Scharr(img_gray,cv2.CV_8U,1,0,1,0)
        img_edge2=cv2.Scharr(img_gray,cv2.CV_8U,0,1,1,0)
        img_edge = cv2.add(img_edge1,img_edge2)
    elif edge == 'Sobel':
        img_edge1=cv2.Sobel(img_gray,cv2.CV_8U,1,0,ksize=3)
        img_edge2=cv2.Sobel(img_gray,cv2.CV_8U,0,1,ksize=3) 
        img_edge = cv2.add(img_edge1,img_edge2)        
    else: #Canny
        img_edge = cv2.Canny(img_blur,80,160,apertureSize=3)
    showimage(img_edge)
 
    #Calculer le masque
    _,img_mask= cv2.threshold(img_edge,100,255,cv2.THRESH_BINARY_INV)
    img_mask = cv2.cvtColor(img_mask,cv2.COLOR_GRAY2BGR)
    showimage(img_mask)
 
    #Lissage
    img_cartoon = img_resize
    print('img_cartoon.shape:',img_cartoon.shape)
    for x in range(11):
        ksize,sigma_color,sigma_space = 21,11,11
        img_cartoon = cv2.bilateralFilter(img_cartoon,ksize,sigma_color,sigma_space)
        #showimage(img_cartoon)
 
    #Masque
    img_cartoon = cv2.bitwise_and(img_cartoon,img_mask)
    showimage(img_cartoon)
 
if __name__ == '__main__':
    cartoon('pexels-ali-pazani-2878373.jpg',edge='Canny')
 


Free image provided by pexel.com



Free image provided by pexel.com



Canny



Free image provided by pexel.com
Free image provided by pexel.com
Free image provided by pexel.com




Laplacian / Laplacien



Free image provided by pexel.com
Free image provided by pexel.com
Free image provided by pexel.com




Scharr



Free image provided by pexel.com
Free image provided by pexel.com
Free image provided by pexel.com




Sobel



Free image provided by pexel.com
Free image provided by pexel.com
Free image provided by pexel.com


Mastering OpenCV with Practical Computer Vision Projects - GitHub





Tested in Anaconda and Python 3.7

# importing libraries 
import cv2
from matplotlib import pyplot as plt
import imageio
 
# reading image  
img = cv2.imread("pexels-andrea-yurko-982585.jpg")
 
# Edges 
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) 
gray = cv2.medianBlur(gray, 5) 
edges = cv2.adaptiveThreshold(gray, 255, cv2.ADAPTIVE_THRESH_MEAN_C,  
                                         cv2.THRESH_BINARY, 9, 9) 
# Cartoonization 
color = cv2.bilateralFilter(img, 9, 250, 250) 
cartoon = cv2.bitwise_and(color, color, mask=edges)
 
plt.imshow(cv2.cvtColor(img.astype('uint8'), cv2.COLOR_BGR2RGB))
plt.axis('off')
plt.title('Image')
plt.show()
 
plt.imshow(edges, 'gray')
plt.axis('off')
plt.title('edges')
plt.show()
cv2.imshow("Cartoon", cartoon)
 
imageio.imwrite('edges.jpg', edges)
 
plt.imshow(cv2.cvtColor(cartoon.astype('uint8'), cv2.COLOR_BGR2RGB))
plt.axis('off')
plt.title('Cartoon')
plt.show()
 
imageio.imwrite('Cartoon.jpg', cv2.cvtColor(cartoon.astype('uint8'), cv2.COLOR_BGR2RGB))
 
cv2.destroyAllWindows()
 


Free image provided by pexel.com
Free image provided by pexel.com
Free image provided by pexel.com


cartoonizer_model - GitHub





Tested in Anaconda and Python 3.7

import cv2
import numpy as np
from matplotlib import pyplot as plt
 
#reading image
 
img = cv2.imread("input-image/Honda-City.jpg")
 
#getting edges
 
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
gray = cv2.medianBlur(gray,7)
edges = cv2.adaptiveThreshold(gray, 255, cv2.ADAPTIVE_THRESH_MEAN_C,cv2.THRESH_BINARY, 9, 9)
 
#cartoonization
 
color = cv2.bilateralFilter(img, 9, 250, 250)
cartoon = cv2.bitwise_and(color, color, mask=edges)
 
# Display 
 
plt.imshow(cv2.cvtColor(img.astype('uint8'), cv2.COLOR_BGR2RGB))
plt.axis('off')
plt.title('Images')
plt.show()
 
plt.imshow(cv2.cvtColor(cartoon.astype('uint8'), cv2.COLOR_BGR2RGB))
plt.axis('off')
plt.title('Cartoon')
plt.show()
 


Original image

Free image provided by pexel.com

Free image provided by pexel.com


Cartoonization - GitHub











Welcome, my name is Eric Soupet and I am the administrator of the site elodees.com. elodees.com is a state of the art of Artificial Intelligence and aims to be collaborative, you can now offer content such as articles, events, tutorials, ... so don't hesitate !

Platform images credit : Pixabay - Pixabay License | Pexels - Pexels License