Skip to content

added more image processing basics in opencv-py #293

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions Python/OpenCV/Flipping.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# Flipping Images - Horizontally, Vertically, Horizontally & Vertically

# Import Computer Vision package - cv2
import cv2

# Read the image using imread built in function
image = cv2.imread('image_1.jpg')

# Display original image using imshow built in function
cv2.imshow("Original-Press any Key to contd", image)

# Wait until any key is pressed
cv2.waitKey(0)

# cv2.flip is used to flip images
# Horizontal flipping of images using value '1'
flipping = cv2.flip(image, 1)

# Display horizontally flipped image
cv2.imshow("Horizontal Flipping", flipping)

# Wait until any key is pressed
cv2.waitKey(0)

# Vertical flipping of images using value '0'
flipping = cv2.flip(image, 0)

# Display vertically flipped image
cv2.imshow("Vertical Flipping", flipping)

# Wait until any key is pressed
cv2.waitKey(0)

# Horizontal & Vertical flipping of images using value '-1'
flipping = cv2.flip(image, -1)

# Display horizontally & vertically flipped image
cv2.imshow("Horizontal & Vertical Flipping", flipping)

# Wait until any key is pressed
cv2.waitKey(0)

# Close all windows
cv2.destroyAllWindows()

49 changes: 49 additions & 0 deletions Python/OpenCV/Scaling.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# Scaling (Resizing) Images - Cubic, Area, Linear Interpolations
# Interpolation is a method of estimating values between known data points

# Import Computer Vision package - cv2
import cv2

# Import Numerical Python package - numpy as np
import numpy as np

# Read the image using imread built-in function
image = cv2.imread('image_2.jpg')

# Display original image using imshow built-in function
cv2.imshow("Original", image)

# Wait until any key is pressed
cv2.waitKey()

# cv2.resize(image, output image size, x scale, y scale, interpolation)

# Scaling using cubic interpolation
scaling_cubic = cv2.resize(image, None, fx=.75, fy=.75, interpolation = cv2.INTER_CUBIC)

# Display cubic interpolated image
cv2.imshow('Cubic Interpolated', scaling_cubic)

# Wait until any key is pressed
cv2.waitKey()

# Scaling using area interpolation
scaling_skewed = cv2.resize(image, (600, 300), interpolation = cv2.INTER_AREA)

# Display area interpolated image
cv2.imshow('Area Interpolated', scaling_skewed)

# Wait until any key is pressed
cv2.waitKey()

# Scaling using linear interpolation
scaling_linear = cv2.resize(image, None, fx=0.5, fy=0.5, interpolation = cv2.INTER_LINEAR)

# Display linear interpolated image
cv2.imshow('Linear Interpolated', scaling_linear)

# Wait until any key is pressed
cv2.waitKey()

# Close all windows
cv2.destroyAllWindows()
45 changes: 45 additions & 0 deletions Python/OpenCV/Varying_Brightness.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# Varying Brightness of Images using Add and Subtract Operations

# Import Computer Vision package - cv2
import cv2

# Import Numerical Python package - numpy as np
import numpy as np

# Read the image using imread built-in function
image = cv2.imread('image_2.jpg')

# Display original image using imshow built-in function
cv2.imshow("Original", image)

# Wait until any key is pressed
cv2.waitKey(0)

# np.ones returns an array of given shape and type, filled with ones
# np.ones(shape, dtype)

matrix = np.ones(image.shape, dtype = "uint8") * 120
# image.shape: gives takes the shape of original image
# uint8: unsigned integer (0 to 255)
# matrix: contains ones, having same dimension as original image but mutlipied by 120

# Adding matrix to orginal image, increases brightness
add = cv2.add(image, matrix)

# Display added image
cv2.imshow("Added", add)

# Wait until any key is pressed
cv2.waitKey(0)

# Subtracting matrix and original image, decreases brightness
subtract = cv2.subtract(image, matrix)

# Display subtracted image
cv2.imshow("Subtracted", subtract)

# Wait untill any key is pressed
cv2.waitKey(0)

# Close all windows
cv2.destroyAllWindows()
Binary file added Python/OpenCV/image_1.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Python/OpenCV/image_2.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.