Thursday, July 18, 2019

Paintbrush using Mouse in OpenCV

Paintbrush using Mouse in OpenCV :
In this video you will learn to handle mouse to draw as a paintbrush in OpenCV programming with practical examples.
Friends if you like this tutorials pls like it and share with your friends and also Subscribe my channel for further lessons. Thank you all.

Thursday, July 4, 2019

OpenCV – Image Resizing techniques


In this tutprial you will learn the various Image Resizing techniques.

  1. How to downsize the image using OpenCV ?
To resize the image, we have to use cv2.resize() function, in this example we will downsize image to 60%, and print the dimensions of Original size and Resized dimensions.

''' How to downsize the image to 60%?'''

import cv2

# input image
img = cv2.imread('messi5.jpg')

# to know the dimensions of image
print("Original Dimensions: ", img.shape)

# input the percentage for resizing the image
scale_percent = 60   # used 60 for downsize the image to 60%,
                      
# calculations for resize image
width = int(img.shape[1]*scale_percent/100)
height = int(img.shape[0]*scale_percent/100)
dim = (width, height)

# resize image
resized = cv2.resize(img, dim)

print('Resized Dimensions: ', resized.shape)

cv2.imshow("Resized image", resized)
cv2.waitKey(0)
cv2.destroyAllWindows()

Output
Original Dimensions:  (342, 548, 3)
Resized Dimensions:  (205, 328, 3)

2. How to upsize the image using OpenCV ?
To resize the image, we have to use cv2.resize() function, in this example we will upsize image to 150%, and print the dimensions of Original size and Resized dimensions.

''' How to upsize the image to 150%?'''

import cv2

# input image
img = cv2.imread('messi5.jpg')

# to know the dimensions of image
print("Original Dimensions: ", img.shape)

# input the percentage for resizing the image
scale_percent = 150   # used 150 for upsize the image to 150%,
                     # if we want to upsize 200% use 200

# calculations for resize image
width = int(img.shape[1]*scale_percent/100)
height = int(img.shape[0]*scale_percent/100)
dim = (width, height)

# resize image
resized = cv2.resize(img, dim)

print('Resized Dimensions: ', resized.shape)

cv2.imshow("Resized image", resized)
cv2.waitKey(0)
cv2.destroyAllWindows()

Output
Original Dimensions:  (342, 548, 3)
Resized Dimensions:  (513, 822, 3)

3. How to resize width of image using OpenCV ?
To resize the image, we have to use cv2.resize() function, in this example we will resize width of image to 700 pixels, and print the dimensions of Original size and Resized dimensions.
''' How to resize width of image to 700px?'''

import cv2

# input image
img = cv2.imread('messi5.jpg')

# to know the dimensions of image
print("Original Dimensions: ", img.shape)

# calculations for resize image
width = 700
height = img.shape[0]   # keep the original height
dim = (width, height)

# resize image
resized = cv2.resize(img, dim)

print('Resized Dimensions: ', resized.shape)

cv2.imshow("Resized image", resized)
cv2.waitKey(0)
cv2.destroyAllWindows()

Output
Original Dimensions:  (342, 548, 3)
Resized Dimensions:  (342, 700, 3)

4.How to resize Height of image using OpenCV ?
To resize the image, we have to use cv2.resize() function, in this example we will resize height of image to 500 pixels, and print the dimensions of Original size and Resized dimensions.
''' How to resize height of image to 500px?'''

import cv2

# input image
img = cv2.imread('messi5.jpg')

# to know the dimensions of image
print("Original Dimensions: ", img.shape)

# calculations for resize image
width = img.shape[1]   # keep the original width
height = 500
dim = (width, height)

# resize image
resized = cv2.resize(img, dim)

print('Resized Dimensions: ', resized.shape)

cv2.imshow("Resized image", resized)
cv2.waitKey(0)
cv2.destroyAllWindows()

Output
Original Dimensions:  (342, 548, 3)
Resized Dimensions:  (500, 548, 3)

5.How to resize Width, Height of image using OpenCV ?
To resize the image, we have to use cv2.resize() function, in this example we will resize width of image to 600 pixels and height to 500 pixels, and print the dimensions of Original size and Resized dimensions.

''' How to resize width, height of the image to required value?'''

import cv2

# input image
img = cv2.imread('messi5.jpg')

# to know the dimensions of image
print("Original Dimensions: ", img.shape)

# input values for resize image
width = 600  
height = 500
dim = (width, height)

# resize image
resized = cv2.resize(img, dim)

print('Resized Dimensions: ', resized.shape)

cv2.imshow("Resized image", resized)
cv2.waitKey(0)
cv2.destroyAllWindows()

Output
Original Dimensions:  (342, 548, 3)
Resized Dimensions:  (500, 600, 3)


=======================================

Wednesday, July 3, 2019

How to get updated key and value pairs to a new variable in Python Dictionary, but keep the values same for the variables?

Question : if a = {'x': 10 , 'y': 20}
                      b = {'y': 30, ' z': 40},
how to get c = {'x': 10, 'y': 30, 'z': 40} , but a and b should have same key and value pairs?


Answer :
>>> a = {'x': 10 , 'y': 20}
>>> b = {'y': 30, ' z': 40}

>>> c = {}
>>> c.update(a) or c.update(b)

>>> c
{'x': 10, 'y': 30, 'z': 40}
>>> a
{'x': 10, 'y': 20}
>>> b
{'y': 30, 'z': 40}
>>> c
{'x': 10, 'y': 30, 'z': 40}

This will not update a and b , but updates c.

Question solved.

OpenCV - Drawing Geometric Shapes with examples and code