Predictive Hacks

Image Processing in OpenCV Python


We will show how you can apply image processing with OpenCV in Python. We will work with this image obtained from Unsplash.

How to blur the Image

import cv2
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline

img = cv2.imread('panda.jpeg')
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
blurred_img = cv2.blur(img,ksize=(20,20))
cv2.imwrite("blurredpanda.jpg", blurred_img) 

How to apply Sobel Operator

You can have a look about Sobel Operator at Wikipedia and you can also start experimenting with some filters. Let’s apply the horizontal and vertical Sobel Operator.

img = cv2.imread('panda.jpeg',0)
sobelx = cv2.Sobel(img,cv2.CV_64F,1,0,ksize=5)
sobely = cv2.Sobel(img,cv2.CV_64F,0,1,ksize=5)

cv2.imwrite("sobelx_panda.jpg", sobelx) 
cv2.imwrite("sobely_panda.jpg", sobely) 

How to apply a threshold to an Image

We can also binarize the images.

img = cv2.imread('panda.jpeg',0)
ret,th1 = cv2.threshold(img,100,255,cv2.THRESH_BINARY)
fig = plt.figure(figsize=(12,10))

Share This Post

Share on facebook
Share on linkedin
Share on twitter
Share on email

Leave a Comment

Subscribe To Our Newsletter

Get updates and learn from the best

More To Explore


Image Captioning with HuggingFace

Image captioning with AI is a fascinating application of artificial intelligence (AI) that involves generating textual descriptions for images automatically.


Intro to Chatbots with HuggingFace

In this tutorial, we will show you how to use the Transformers library from HuggingFace to build chatbot pipelines. Let’s