No account yet ?
Tested in Anaconda and Python 3.7
import cv2 from matplotlib import pyplot as plt def showimage(myimage, figsize=[10,10]): if (myimage.ndim>2): #This only applies to RGB or RGBA images (e.g. not to Black and White images) myimage = myimage[:,:,::-1] #OpenCV follows BGR order, while matplotlib likely follows RGB order fig, ax = plt.subplots(figsize=figsize) ax.imshow(myimage, cmap = 'gray', interpolation = 'bicubic') plt.xticks([]), plt.yticks([]) # to hide tick values on X and Y axis plt.show() img_originale = cv2.imread('inputCoins.jpg', 1) showimage(img_originale) img_gray = cv2.imread('inputCoins.jpg', 0) showimage(img_gray) (retVal, newImg) = cv2.threshold(img_gray, 130, 255, cv2.THRESH_BINARY) showimage(newImg) newImg = cv2.adaptiveThreshold(img_gray, 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY, 9, 15) showimage(newImg)
Original image
Grayscale
Simple thresholding
Adaptive thresholding
Tested in Anaconda and Python 3.7
# Python program to illustrate # Otsu thresholding type on an image # organizing imports import cv2 from matplotlib import pyplot as plt def showimage(myimage, figsize=[10,10]): if (myimage.ndim>2): #This only applies to RGB or RGBA images (e.g. not to Black and White images) myimage = myimage[:,:,::-1] #OpenCV follows BGR order, while matplotlib likely follows RGB order fig, ax = plt.subplots(figsize=figsize) ax.imshow(myimage, cmap = 'gray', interpolation = 'bicubic') plt.xticks([]), plt.yticks([]) # to hide tick values on X and Y axis plt.show() # path to input image is specified and # image is loaded with imread command image_originale = cv2.imread('inputCoins.jpg') showimage(image_originale) # cv2.cvtColor is applied over the # image input with applied parameters # to convert the image in grayscale img = cv2.cvtColor(image_originale, cv2.COLOR_BGR2GRAY) showimage(img) # applying Otsu thresholding # as an extra flag in binary # thresholding ret, thresh1 = cv2.threshold(img, 120, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU) # the window showing output image # with the corresponding thresholding # techniques applied to the input image showimage(thresh1)
Original image
Grayscale
Otsu thresholding
Tested in Anaconda and Python 3.7
# Python program to illustrate # adaptive thresholding type on an image # organizing imports import cv2 from matplotlib import pyplot as plt def showimage(myimage, figsize=[10,10]): if (myimage.ndim>2): #This only applies to RGB or RGBA images (e.g. not to Black and White images) myimage = myimage[:,:,::-1] #OpenCV follows BGR order, while matplotlib likely follows RGB order fig, ax = plt.subplots(figsize=figsize) ax.imshow(myimage, cmap = 'gray', interpolation = 'bicubic') plt.xticks([]), plt.yticks([]) # to hide tick values on X and Y axis plt.show() # path to input image is specified and # image is loaded with imread command image_originale = cv2.imread('inputCoins.jpg') showimage(image_originale) # cv2.cvtColor is applied over the # image input with applied parameters # to convert the image in grayscale img = cv2.cvtColor(image_originale, cv2.COLOR_BGR2GRAY) # applying different thresholding # techniques on the input image thresh1 = cv2.adaptiveThreshold(img, 255, cv2.ADAPTIVE_THRESH_MEAN_C,cv2.THRESH_BINARY, 199, 5) thresh2 = cv2.adaptiveThreshold(img, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, 199, 5) # the window showing output images # with the corresponding thresholding # techniques applied to the input image showimage(thresh1) showimage(thresh2)
Original image
Grayscale
Mean adaptive thresholding
Gaussian adaptive thresholding
Tested in Anaconda and Python 3.7
# Python program to illustrate # Otsu thresholding type on an image # organizing imports import cv2 from matplotlib import pyplot as plt def showimage(myimage, figsize=[10,10]): if (myimage.ndim>2): #This only applies to RGB or RGBA images (e.g. not to Black and White images) myimage = myimage[:,:,::-1] #OpenCV follows BGR order, while matplotlib likely follows RGB order fig, ax = plt.subplots(figsize=figsize) ax.imshow(myimage, cmap = 'gray', interpolation = 'bicubic') plt.xticks([]), plt.yticks([]) # to hide tick values on X and Y axis plt.show() # path to input image is specified and # image is loaded with imread command image_originale = cv2.imread('inputCoins.jpg') showimage(image_originale) # cv2.cvtColor is applied over the # image input with applied parameters # to convert the image in grayscale img = cv2.cvtColor(image_originale, cv2.COLOR_BGR2GRAY) showimage(img) # applying Otsu thresholding # as an extra flag in binary # thresholding th, thresh1 = cv2.threshold(img, 50, 255, cv2.THRESH_TRUNC) # the window showing output image # with the corresponding thresholding # techniques applied to the input image showimage(thresh1) # applying Otsu thresholding # as an extra flag in binary # thresholding th, thresh1 = cv2.threshold(img, 100, 255, cv2.THRESH_TRUNC) # the window showing output image # with the corresponding thresholding # techniques applied to the input image showimage(thresh1) # applying Otsu thresholding # as an extra flag in binary # thresholding th, thresh1 = cv2.threshold(img, 150, 255, cv2.THRESH_TRUNC) # the window showing output image # with the corresponding thresholding # techniques applied to the input image showimage(thresh1) # applying Otsu thresholding # as an extra flag in binary # thresholding th, thresh1 = cv2.threshold(img, 200, 255, cv2.THRESH_TRUNC) # the window showing output image # with the corresponding thresholding # techniques applied to the input image showimage(thresh1)
Original image
Grayscale
Truncated thresholding - Threshold = 50
Truncated thresholding - Threshold = 100
Truncated thresholding - Threshold = 150
Truncated thresholding - Threshold = 200
Welcome, my name is Eric Soupet and I am the administrator of the site elodees.com. elodees.com is a state of the art of Artificial Intelligence and aims to be collaborative, you can now offer content such as articles, events, tutorials, ... so don't hesitate !
Platform images credit : Pixabay - Pixabay License | Pexels - Pexels License