# OpenCV Basics

Jaya chandrika reddy Updated on ・3 min read

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

• [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
``````

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
``````

Output:

array([2, 7])

For more practice on Numpy visit

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

## 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
``````

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
``````

### Example

``````import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
from PIL import Image
``````
``````img=Image.open(r'C:\Desktop\image.jpg')

# Opens Image
``````
``````img

#To Preview Image
``````

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
``````

Example:

``````img_array=np.asarray(img)

# Converting image into array of values
``````
``````img_array.shape

# Displaying the shape of the array
``````

Output:

(1080,1920,3)

``````plt.imshow(img_array)

# Displaying array into Image
``````

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.
``````

## 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.
``````

Example

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

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

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

#Converting the color image into grayscale using cmap
``````

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:

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

Posted on by:

### Jaya chandrika reddy

A programmer who tries to teach programming knowledge with fun

### Discussion

Good one, you can try creating custom Cover Images, would look better.. Just a suggestion😊

Also, include code formatting for python in your posts. 😊

Sure, will do it soon, thanks for the suggestion😄

Sure, thank you for your valuable suggestion :)

🙂all the best

Thank you Mr.Tharun

Are you planning to write more on projects like Motion detection?