OpenCV Basics

Want to combine two images using just a line of code? Or wanna blur an image? Lets find out how!

In this Article (Click to skip to that topic):

  • [Blend images: What is it?]
  • [Code to Blend]
  • [Methods to Blur an Image ]
  • [Code to Blur Image]

Blend images: What is it?

Blending images in opencv, is combining to images of same size. So we have to remember to resize those two images to same size(which will be shown in the code later).

Using this, we can make pretty cool posters, wallpapers, beautiful creations in your imaginations and many more…

So lets learn how to do it in a simple way

bigarr=np.arange(0, 10).reshape((2,5))
bigarr

#'arange' gives an array withn values from 0 to 9, 'reshapes' converts into a matrix of 2x5

Enter fullscreen mode Exit fullscreen mode

Output:

array([[0, 1, 2, 3, 4],
[5, 6, 7, 8, 9]])

bigarr[:, 2]

#Here only column 2 is considered but not row as colon(:) is mentioned

Enter fullscreen mode Exit fullscreen mode

Output:

array([2, 7])

For more practice on Numpy visit

 https://numpy.org/devdocs/user/quickstart.html

Enter fullscreen mode Exit fullscreen mode

## Display Image in Opencv using Numpy

Now,how to open or show an image using our numpy knowledge:

We use libraries

  • PIL(Python Imaging Library)

  • Matplotlib( For visualization) and import Image from PIL

Image.open(image file path)       

# Opens and identifies the given image file

Enter fullscreen mode Exit fullscreen mode

Each pixel has a shape.

For Eg:(1080,1080,3)which is (pixel height,pixel width,color channels)
The 3 color channels are red,green and blue(RGB).

plt.imshow                         

# Displays the image

Enter fullscreen mode Exit fullscreen mode

Example

import numpy as np
import matplotlib.pyplot as plt 
%matplotlib inline
from PIL import Image

Enter fullscreen mode Exit fullscreen mode

img=Image.open(r'C:\Desktop\image.jpg') 

# Opens Image

Enter fullscreen mode Exit fullscreen mode

img

#To Preview Image

Enter fullscreen mode Exit fullscreen mode

Each pixel has a shape.

For Eg:(1080,1080,3) which is (pixel height,pixel width,color channels).

The 3 color channels are red,green and blue(RGB).

plt.imshow             

# Displays the image

Enter fullscreen mode Exit fullscreen mode

Example:

img_array=np.asarray(img)

# Converting image into array of values

Enter fullscreen mode Exit fullscreen mode

img_array.shape

# Displaying the shape of the array

Enter fullscreen mode Exit fullscreen mode

Output:

(1080,1920,3)

plt.imshow(img_array)

# Displaying array into Image  

Enter fullscreen mode Exit fullscreen mode

What if only one channel is used in displaying image instead all 3 in RGB?

plt.imshow(array[:,:,0])           # Displays image with only Red channel.

plt.imshow(array[:,:,1])           # Displays image with only Green channel.

plt.imshow(array[:,:,2])           # Displays image with only Blue channel.

Enter fullscreen mode Exit fullscreen mode

## Gray Image using Numpy

What if we want a gray image like in old movies?

The parameter cmap=’gray’ is used to convert color images into a gray image.

plt.imshow(array[:,:,0],cmap=’gray’) 

# Displays image with grayscale values.

Enter fullscreen mode Exit fullscreen mode

Example

plt.imshow(img_array[:,:,0])

#Displaying only RED channel in RGB. Replace 0 with 1 for Green and 2 for Blue Channel. 

Enter fullscreen mode Exit fullscreen mode

plt.imshow(img_array[:,:,0],cmap='gray')

#Converting the color image into grayscale using cmap

Enter fullscreen mode Exit fullscreen mode

For more code on fliping, resizing etc visit the blog, follow the Instagram page mentioned below

If you are building a project or wanna upgrade your skills for the job or just excited to build cool stuff, learn opencv easily and quickly from Instagram page code_voyager:

https://www.instagram.com/p/B_5ey22pLu_/?igshid=1t6k2adh9a9zk

Or more detailed version feel free to visit :
https://jayachandrika.com/computer-vision-images

Looking forward for you to join us!

原文链接:OpenCV Basics

© 版权声明
THE END
喜欢就支持一下吧
点赞13 分享
评论 抢沙发

请登录后发表评论

    暂无评论内容