Table of contents
2. Thresholding, Binarization and Adaptive Thresholding
This article is my study notes for learning OpenCV on kaggle. It would be great if it helps you. This is the link to the original text Learn OpenCV by Examples - with Python | Kaggle contains not only code but also pictures. If you haven't registered kaggle, you can refer to this article http://t.csdn.cn/oUkxY so that you can quickly register without going over the wall. When referring to and studying on kaggle, a certain foundation of English is required.
About OpenCV
OpenCV (Open Source Computer Vision) was officially released in 1999, initiated by Intel.
The core of OpenCV is written in C++. In python we just use a wrapper to execute c++ code inside python.
The first major version 1.0 was in 2006, the second in 2009, the third in 2015, and the fourth in 2018. Using OpenCV 4.0 beta.
It is an open source library containing more than 2500 optimization algorithms.
It is very useful for almost all computer vision applications and supports Windows, Linux, MacOS, Android, iOS, bindings to Python, Java and Matlab.
Update (19.05.2020)
I will always try to improve this kernel. I made some additions to this version. Thank you for reading, I hope it will help you
new content
17. Background subtraction
18. Funny mirror using OpenCV
Content
- Sharpening
- Thresholding, Binarization & Adaptive Thresholding
- Dilation, Erosion, Opening and Closing
- Edge Detection & Image Gradients
- Perpsective Transform
- Scaling, re-sizing and interpolations
- Image Pyramids
- Cropping
- Blurring
- Contours
- Approximating Contours and Convex Hull
- Identifiy Contours by Shape
- Line Detection - Using Hough Lines
- Counting Circles and Ellipses
- Finding Corners
- Finding Waldo
- Background Subtraction Methods
- Funny Mirrors Using OpenCV
Background Subtraction Methods Output
Funny Mirrors Using OpenCV Output
Some pictures from content
Import package:
import numpy as np
import matplotlib.pyplot as plt
import cv2
1. Sharpen
By varying our kernel, we can achieve sharpening, which has the effect of enhancing or emphasizing the edges of an image.
image = cv2.imread('/kaggle/input/opencv-samples-images/data/building.jpg')
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
plt.figure(figsize=(20, 20))
plt.subplot(1, 2, 1)
plt.title("Original")
plt.imshow(image)
# Create our shapening kernel, we don't normalize since the
# the values in the matrix sum to 1
kernel_sharpening = np.array([[-1,-1,-1],
[-1,9,-1],
[-1,-1,-1]])
# applying different kernels to the input image
sharpened = cv2.filter2D(image, -1, kernel_sharpening)
plt.subplot(1, 2, 2)
plt.title("Image Sharpening")
plt.imshow(sharpened)
plt.show()
2. Thresholding, Binarization and Adaptive Thresholding
import numpy as np
import cv2
from matplotlib import pyplot as plt
# Load our new images
image = cv2.imread('images/Origin_of_Species.jpg', 0)
plt.figure(figsize=(30, 30))
plt.subplot(3, 2, 1)
plt.title("Original")
plt.imshow(image)
# Values below 127 goes to 0 (black, everything above goes to 255 (white)
ret,thresh1 = cv2.threshold(image, 127, 255, cv2.THRESH_BINARY)
plt.subplot(3, 2, 2)
plt.title("Threshold Binary")
plt.imshow(thresh1)
# It's good practice to blur images as it removes noise
image = cv2.GaussianBlur(image, (3, 3), 0)
# Using adaptiveThreshold
thresh = cv2.adaptiveThreshold(image, 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY, 3, 5)
plt.subplot(3, 2, 3)
plt.title("Adaptive Mean Thresholding")
plt.imshow(thresh)
_, th2 = cv2.threshold(image, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)
plt.subplot(3, 2, 4)
plt.title("Otsu's Thresholding")
plt.imshow(th2)
plt.subplot(3, 2, 5)
# Otsu's thresholding after Gaussian filtering
blur = cv2.GaussianBlur(image, (5,5), 0)
_, th3 = cv2.threshold(blur, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)
plt.title("Guassian Otsu's Thresholding")
plt.imshow(th3)
plt.show()
Continually updated...
Reference: Learn OpenCV by Examples - with Python | Kaggle