Skip to content

Instantly share code, notes, and snippets.

View peeush-the-developer's full-sized avatar

Peeush Agarwal peeush-the-developer

  • Pune, MH, India
View GitHub Profile
@peeush-the-developer
peeush-the-developer / opencv_erosion.py
Last active May 15, 2021 02:34
OpenCV: Erosion - Morpholigical operations
# gray is the numpy array of grayscale of binary image.
# Perform erosion operations on the binary image
for i in range(1, 6):
# Parameter as None means that we apply default kernel
# (3,3) 8-neighborhood rectangle kernel.
eroded = cv2.erode(gray.copy(), None, iterations=i)
cv2.imshow(f'Eroded: {i} times', eroded)
# Wait for the key press to move further.
@peeush-the-developer
peeush-the-developer / opencv_dilation.py
Last active May 15, 2021 02:35
OpenCV: Dilation - Morphological operations
# gray is the numpy array of grayscale of binary image.
# Perform Dilation operation
for i in range(1, 6):
# Parameter as None means that we apply default kernel
# (3,3) 8-neighborhood rectangle kernel.
dilated = cv2.dilate(gray.copy(), None, iterations=i)
cv2.imshow(f"Dilated: {i} times", dilated)
cv2.waitKey(0)
@peeush-the-developer
peeush-the-developer / opencv_opening.py
Last active May 15, 2021 04:42
OpenCV: Opening - Morphological operations
# gray is the numpy array of grayscale of binary image.
# Perform Opening: Erosion followed by Dilation.
kernel_sizes = [(3, 3), (5, 5), (7, 7)]
for kernel_size in kernel_sizes:
# Get 8-neighborhood kernel based on the kernel size
kernel = cv2.getStructuringElement(cv2.MORPH_RECT, kernel_size)
# Apply kernel to the grayscale binary image
opened = cv2.morphologyEx(gray.copy(), cv2.MORPH_OPEN, kernel)
# Display "Opened" image
@peeush-the-developer
peeush-the-developer / opencv_closing.py
Created May 15, 2021 02:32
OpenCV: Closing - Morphological operations
# gray is the numpy array of grayscale of binary image.
kernel_sizes = [(3, 3), (5, 5), (7, 7)]
# Perform Closing: Dilation followed by Erosion.
for kernel_size in kernel_sizes:
# Get 8-neighborhood kernel based on the kernel size
kernel = cv2.getStructuringElement(cv2.MORPH_RECT, kernel_size)
# Apply kernel to the grayscale binary image
closed = cv2.morphologyEx(gray.copy(), cv2.MORPH_CLOSE, kernel)
@peeush-the-developer
peeush-the-developer / opencv_gradient.py
Created May 15, 2021 02:44
OpenCV: Gradient - Morphological operations
# gray is the numpy array of grayscale of binary image.
kernel_sizes = [(3, 3), (5, 5), (7, 7)]
# Perform Morpholigical_gradient: Difference between Dilation and Erosion
for kernel_size in kernel_sizes:
# Get 8-neighborhood kernel based on the kernel size
kernel = cv2.getStructuringElement(cv2.MORPH_RECT, kernel_size)
# Apply kernel to the grayscale binary image
grad = cv2.morphologyEx(gray.copy(), cv2.MORPH_GRADIENT, kernel)
@peeush-the-developer
peeush-the-developer / opencv_tophat.py
Created May 15, 2021 04:42
OpenCV: Top Hat (or White hat) - Morphological operations
# gray is the numpy array of grayscale of binary image.
# kernel defined. The size of kernel is based on the problem statement.
# Following kernel is for revealing License plate region in the image.
kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (20, 5))
# Perform TopHat: Difference between grayscale image and opening. This will give bright foreground over dark background
top_hat = cv2.morphologyEx(gray.copy(), cv2.MORPH_TOPHAT, kernel)
# Display output after TopHat operation
cv2.imshow("TopHat", top_hat)
@peeush-the-developer
peeush-the-developer / opencv_blackhat.py
Created May 15, 2021 04:51
OpenCV: Black Hat - Morphological operations
# gray is the numpy array of grayscale of binary image.
# kernel defined. The size of kernel is based on the problem statement.
# Following kernel is for revealing License plate region in the image.
kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (20, 5))
# Perform BlackHat: Difference between grayscale image and closing. This will give dark foreground over light background
black_hat = cv2.morphologyEx(gray.copy(), cv2.MORPH_BLACKHAT, kernel)
# Display output after BlackHat operation
cv2.imshow("BlackHat", black_hat)
@peeush-the-developer
peeush-the-developer / create_env_install_libraries.sh
Created June 5, 2021 05:43
Create virtual environment and install libraries
python3 -m venv {env_name}
source {env_name}/bin/activate
python3 -m pip install requests bs4 pandas
import datetime
import requests
from bs4 import BeautifulSoup
import pandas as pd
@peeush-the-developer
peeush-the-developer / get_response.py
Created June 5, 2021 05:48
Get HTTP response for web request
response = requests.get('https://www.nytimes.com/interactive/2017/06/23/opinion/trumps-lies.html')