You are on page 1of 5

Cartooning an Image

Submitted in partial fulfilment of the requirements of the degree


BACHELOR OF ENGINEERING IN COMPUTER
ENGINEERING

Sahil Satish Pawar (46)


Shouryan Pawar (47)
Tejaswini Suresh Pawar (48)

Supervisor
Prof. Mr. R. B. Pawar

DEPARTMENT OF COMPUTER ENGINEERING


Gharda Institute of Technology
A/P: Lavel, Tal-Khed, Dist- Ratnagiri, 415708
Mumbai University [2021-22]
CERTIFICATE

This is to certify that the Mini Project entitled “Cartooning an Image ” is a


bonafide work of Sahil Pawar_46, Shouryan Pawar_47, Tejaswini Suresh
Pawar_48 submitted to the University of Mumbai in partial fulfilment of the
requirement for the award of the degree of “Bachelor of Engineering”.

(Prof. Mr. R. B. Pawar)


Supervisor

(Prof. Dr. Raman Bane) Head


of Department
Code:
# ProjectGurukul Cartooning an image using OpenCV-Python

# Import necessary packages


import cv2
import numpy as np

# Reading image
img = cv2.imread('image.jpg')

def cartoonize(img, k):

    # Convert the input image to gray scale


    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

    # Peform adaptive threshold


    edges  = cv2.adaptiveThreshold(gray, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C,
cv2.THRESH_BINARY, 9, 8)

    # cv2.imshow('edges', edges)

    # Defining input data for clustering


    data = np.float32(img).reshape((-1, 3))

    print("shape of input data: ", img.shape)


    print('shape of resized data', data.shape)

    # Defining criteria
    criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 20, 1.0)

    # Applying cv2.kmeans function


    _, label, center = cv2.kmeans(data, k, None, criteria, 10,
cv2.KMEANS_RANDOM_CENTERS)
    center = np.uint8(center)
    # print(center)

    # Reshape the output data to the size of input image


    result = center[label.flatten()]
    result = result.reshape(img.shape)
    #cv2.imshow("result", result)

    # Smooth the result


    blurred = cv2.medianBlur(result, 3)

    # Combine the result and edges to get final cartoon effect


    cartoon = cv2.bitwise_and(blurred, blurred, mask=edges)

    return cartoon

cartoonized = cartoonize(img, 8)

# Show the output

cv2.imshow('input', img)
cv2.imshow('output',cartoonized)

cv2.waitKey(0)
cv2.destroyAllWindows()

Output
CONCLUSION:
In this project we have successfully studied and implemented
Cartooning an Image using OpenCV & Python
.

You might also like