Learn Python

Learn Data Structure & Algorithm

Learn Numpy

Learn Pandas

Learn Matplotlib

Learn Seaborn

Learn Statistics

Learn Math

Learn MATLAB

Learn Machine learning

Learn Github

OpenCV Introduction

OpenCV Image Read & Write

OpenCV Image Channels

OpenCV Color Conversion

OpenCV Rotation & Crop

OpenCV Text & Shape

OpenCV Video

OpenCV Arithmetics Operation

OpenCV Thresholding

OpenCV Blur & Blending

OpenCV Edge Detection

OpenCV Contours

OpenCV Hough Transformation Lines

OpenCV Moments & Convexhull

OpenCV Morphological Transformation

OpenCV Template Matching

Learn Deep Learning

Learn MySQL

Learn MongoDB

Learn Web scraping

Learn Excel

Learn Power BI

Learn Tableau

Learn Docker

Learn Hadoop

Image thresholding in opencv

What is thresholding?

Image thresholding is a simple binary form of colored image. Using thresholding we can create binary image from colored or grayscale image. We do this to separate object or foreground pixels from background pixels to aid in image processing.

How to perform adaptive thresholding?

Simple thresholding will make the pixel value low or high. If it happens then the chance of losing the image information will increase. We can also say that a simple thresholding technique can't handle the low luminous pixels but adaptive thresholding can. Simple threshold will find only one value threshold of the image but adaptive threshold finds multiple values threshold of the image. That's why adaptive threshold is mostly used and gives better result.

There are common two types of adaptive thresholding techniques:
1. cv2.ADAPTIVE_THRESH_MEAN_C:1
This method works by depending on the neighbors pixel mean values.
2. cv2.ADAPTIVE_THRESH_GAUSSIAN_C:
This method works by depending on the neighbors pixel average values.

Input
import cv2
import numpy as np
path=r"E:\picture\prac_deep\dd.jpg"
img=cv2.imread(path,0)
img=cv2.resize(img,(800,700))

#ADAPTIVE_THRESH_MEAN_C Thresholding:
aDAPTIVE_tHRESH_mEAN_C=cv2.adaptiveThreshold(img,255,cv2.ADAPTIVE_THRESH_MEAN_C,cv2.THRESH_BINARY,11,3)
'''
The first parameter is the image, the second parameter is the maximum pixel value, the third parameter is the method. We have two methods. You can use any and the fourth parameter is the type.
There are three types:
1. cv2.THRESH_BINARY
2. THRESH_TRUNC
3. THRESH_TOZERO
The fifth parameter is the number of pixels. If you pass 12 then this function will make groups of pixels and each group will contain 12 pixels. You can change the number. And the last parameter is the mean value.
'''
cv2.imshow("aDAPTIVE_tHRESH_mEAN_C",aDAPTIVE_tHRESH_mEAN_C)

#cv2.ADAPTIVE_THRESH_GAUSSIAN_C Thresholding
aDAPTIVE_tHRESH_gAUSSIAN_C=cv2.adaptiveThreshold(img,255,cv2.ADAPTIVE_THRESH_GAUSSIAN_C,cv2.THRESH_BINARY,11,2)
cv2.imshow("aDAPTIVE_tHRESH_gAUSSIAN_C",aDAPTIVE_tHRESH_gAUSSIAN_C)

cv2.waitKey(0)
cv2.destroyAllWindows()
Output

CodersAim is created for learning and training a self learner to become a professional from beginner. While using CodersAim, you agree to have read and accepted our terms of use, privacy policy, Contact Us

© Copyright All rights reserved www.CodersAim.com. Developed by CodersAim.