Git Product home page Git Product logo

bytesofintelligences / ibm-project-02-transform-photos-to-sketches-and-paintings-with-opencv Goto Github PK

View Code? Open in Web Editor NEW
3.0 2.0 1.0 3.29 MB

Traditionally, a sketch is a crude drawing or painting in which an artist records his first thoughts for a work that will later be produced with more accuracy and detail. Sketches may take on a variety of forms, including drawings, paintings, and drawings on paper.

Home Page: https://cognitiveclass.ai/courses/course-v1:IBM+GPXX0HC7EN+v1

License: MIT License

Jupyter Notebook 100.00%
computer-vision deep-learning image-processing image-transformations opencv python

ibm-project-02-transform-photos-to-sketches-and-paintings-with-opencv's Introduction


IBM Project 02 Transform Photos to Sketches and Paintings with OpenCV

# Description Have you wanted to transform your photographs in an artistic sketch or painting to showcase your creativity? Well using some built in OpenCV functions you can do exactly that! OpenCV is a powerful computer vision and image processing library. In this guided project you will learn how to transform your photos to sketches and paintings using OpenCV in python.

Features

Objectives:

After completing this lab, you will be able to:

  • Convert colored images to grayscale
  • Apply the Gaussian filter to smooth an image
  • Detect the edges of an image using the Gradient Magnitude
  • Convert a grayscale image to a binary image
  • Create a pencil sketch of an image in OpenCV
  • Create a water painting of an image in OpenCV

Get ready to start!

Table of Contents

import requests
import os

# Make a folder to hold our images called "images" if it doesn't already exist
!mkdir -p ./images

# To add the image we will be using to this lab to the folder
if not os.path.isfile("./images/LandscapePhotograph.jpeg"):
    r = requests.get("https://cf-courses-data.s3.us.cloud-object-storage.appdomain.cloud/transform-photos-to-sketches-and-paintings-with-opencv/images/LandscapePhotograph.jpeg")
    f = open("./images/LandscapePhotograph.jpeg", mode = "wb+")
    f.write(r.content)
    f.close()

Detect the Edge

The edges of an image are the parts of an image where the brightness changes quickly. To detect the edges of an image, we need to compute something called the gradient magnitude of an image. The gradient magnitude of an image measures how quickly the image is changing and it is used for edge detection. To get a sketch for our photograph, we will compute the gradient magnitude of the image.

To get the gradient magnitude of an image, we need to calculate the verticle and horizontal components of the gradient of the image. In other words, we need to calculate how much the image changes in the horizontal direction (the x component of the gradient), $dx$, and the verticle direction (the y component of the gradient), $dy$. To calculate how much the image changes in the each direction, we apply a filter called the Sobel filter to the images. To do this in OpenCV, we use the method cv2.Sobel().

The formula for the gradient magnitude is $\sqrt {\left( {dx} \right)^2 + \left( {dy} \right)^2 }$.

sobelx = cv2.Sobel(blur,cv2.CV_64F,1,0,ksize=5) # Change in horizonal direction, dx
sobely = cv2.Sobel(blur,cv2.CV_64F,0,1,ksize=5) # Change in verticle direction, dy
gradmag_sq = np.square(sobelx)+np.square(sobely) # Square the images element-wise and then add them together 
gradmag = np.sqrt(gradmag_sq) # Take the square root of the resulting image element-wise to get the gradient magnitude

plt.imshow(gradmag, cmap ='gray')

Authors:

Yasmine Hemmati

Date (YYYY-MM-DD) Version Changed By Change Description
2021-08-06 0.1 Yasmine Initial Version Created

© IBM Corporation 2021. All rights reserved.

ibm-project-02-transform-photos-to-sketches-and-paintings-with-opencv's People

Contributors

bytesofintelligences avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.