Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

simple thresholding with OpenCV

import cv2
from matplotlib import pyplot as plt
'''Read your image and convert it to graycale as follows:'''
img = cv2.imread("myimage.jpg")
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
'''Apply a Low-pass filter such as guassian blur to reduce high frequency
components as follows:'''
img_blur = cv2.GaussianBlur(gray, (3,3), 0) 
'''The (3,3) is filter size (must be odd) and zero is the standard
deviation parameter of a guassian function which tells GaussianBlur to
calculate standard diviation automatically. Assuming myimage.jpg is of a
bi-modal distribution (its histogram plot contains two peaks) then 
threshold is applied as follows:'''
threshdimage = cv2.threshold(img_blur,100,255,cv2.THRESH_BINARY)[1]
plt.imshow(threshdimage, cmap = 'gray', interpolation = 'bicubic')# plot  
plt.xticks([]), plt.yticks([])  # to hide tick values on X and Y axis
'''Where 100 is threshold that divides the pixel space , hence all pixel
values smaller than 100 are set to 0 and all above 100 are set to 255.
The THRESH_BINARY specifies method used for thresholding. See the 
following: https://docs.opencv.org/4.x/d7/d4d/tutorial_py_thresholding.html
for more info'''
plt.show()
Comment

thresholding with OpenCV

#...........................Additional notes............................
'''In addition to cv2.GaussianBlur() filter, other commonly used filters 
are: Average blur, cv2.blur(); Median blur, cv2.medianBlur(); and 
and Bilateral filter, cv2.bilateralFilter. For example, Bilateral filter
can be used to filter high frequecy components while maintaining the
edges of the image, this is the strength of this filter compared to others.
The example below implements the Bilateral filter:'''
import cv2
from matplotlib import pyplot as plt

img = cv2.imread("myimage.jpg")
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)

diameter = 30# diameter of the pixels included in the neighborhood.
sigmaColor = 20 # color standard deviation  in the neighborhood.
sigmaSpace = 20 # space standard deviation in the neighborhood
blurred = cv2.bilateralFilter(img, diameter, sigmaColor, sigmaSpace)
plt.imshow(threshdimage, cmap = 'gray', interpolation = 'bicubic')# plot  
plt.xticks([]), plt.yticks([])  # to hide tick values on X and Y axis
plt.show()

Comment

PREVIOUS NEXT
Code Example
Python :: python how to sort by date 
Python :: python multiply list bt number 
Python :: from .cv2 import * ImportError: /home/pi/.local/lib/python3.7/site-packages/cv2/cv2.cpython-37m-arm-linux-gnueabihf.so: undefined symbol: __atomic_fetch_add_8 
Python :: python print without space 
Python :: requests post with headers python 
Python :: matplotlib set number of decimal places 
Python :: python discord input 
Python :: django session expire time 
Python :: encoding read_csv 
Python :: Why do we use graphs? 
Python :: fstring number format python 
Python :: python regex remove digits from string 
Python :: what is a module computer science 
Python :: Import "dj_database_url" could not be resolved Pylance 
Python :: matplotlib boxplot remove outliers 
Python :: pyqt latex 
Python :: python check variable is tuple 
Python :: convert array to dataframe python 
Python :: printing a range of no one line in python 
Python :: python get object attribute by string 
Python :: fiel to base64 python 
Python :: pandas read csv unnamed 0 
Python :: sqlalchemy if a value in list of values 
Python :: freq count in python 
Python :: python loop through list 
Python :: check if number in range 
Python :: extract link from text python 
Python :: sklearn rmse 
Python :: get certain columns pandas with string 
Python :: openpyxl delete column by name 
ADD CONTENT
Topic
Content
Source link
Name
9+2 =