OpenCV Basics
Tags: computerscience, python, codepen, jupyter
Published: 2020-05-07 20:43:15

_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]


<h3 id='what-is-yaml'> Blend images: What is it? </h3>

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

<br/>

    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]])

<br/>
  
    bigarr[:, 2]

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

Output:

array([2, 7])

<br/>
   
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

<br/>
 
    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).

<br/>
    
    plt.imshow                         
    
    # Displays the image

### **Example**
<br/>
   
    import numpy as np
    import matplotlib.pyplot as plt 
    %matplotlib inline
    from PIL import Image

<br/>

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

    # Opens Image

<br/>

    img

    #To Preview Image
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/4c91ashgm0y69g7d7lqo.JPG)

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).
<br/>

    plt.imshow             
    
    # Displays the image

**Example:**  
<br/> 
  
    img_array=np.asarray(img)

    # Converting image into array of values
<br/> 
  
    img_array.shape

    # Displaying the shape of the array
 Output:    

 (1080,1920,3) 

 <br/> 
  
    plt.imshow(img_array)

    # Displaying array into Image  

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/h6wjgqx60g7s0kwzznkh.JPG)
    
What if only one channel is used in displaying image instead all 3 in RGB?  

<br/>

    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.

<br/>
   
    plt.imshow(array[:,:,0],cmap=’gray’) 
    
    # Displays image with grayscale values.

**Example**
<br/>
 
    plt.imshow(img_array[:,:,0])
    
    #Displaying only RED channel in RGB. Replace 0 with 1 for Green and 2 for Blue Channel. 

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/h8kz628bltzmzi44wp5h.JPG)
<br/>
 
    plt.imshow(img_array[:,:,0],cmap='gray')
    
    #Converting the color image into grayscale using cmap

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/gdp39vgo9aiu5e9vdyfn.JPG)     

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!😊