Cara menggunakan pillow threshold python

Thresholding adalah proses mengubah citra berderajat keabuan menjadi citra biner atau hitam putih sehingga dapat diketahui daerah mana yang termasuk obyek dan background dari citra secara jelas. Citra hasil thresholding biasanya digunakan lebih lanjut untuk proses pengenalan obyek serta ekstraksi fitur.




Fungsi yang digunakan adalah cv2.threshold . Yang pertama kita lakukan adalah sumber gambar yang akan diubah menjadi gambar grayscale. Kedua adalah nilai ambang (threshold) yang digunakan untuk mengklasifikasikan nilai-nilai pixel. Ketiga adalah maxVal yang mewakili nilai yang akan diberikan jika nilai piksel lebih dari atau kurang dari nilai ambang.

Python | Thresholding techniques using OpenCV | Set-1 (Simple Thresholding)

Improve Article

Save Article

Like Article

  • Difficulty Level : Medium
  • Last Updated : 04 Jan, 2023

  • Read
  • Discuss
  • Courses
  • Practice
  • Video
  • Improve Article

    Save Article

    Thresholding is a technique in OpenCV, which is the assignment of pixel values in relation to the threshold value provided. In thresholding, each pixel value is compared with the threshold value. If the pixel value is smaller than the threshold, it is set to 0, otherwise, it is set to a maximum value (generally 255). Thresholding is a very popular segmentation technique, used for separating an object considered as a foreground from its background. A threshold is a value which has two regions on its either side i.e. below the threshold or above the threshold. 
    In Computer Vision, this technique of thresholding is done on grayscale images. So initially, the image has to be converted in grayscale color space. 
     

    If f (x, y) < T 
       then f (x, y) = 0 
    else 
       f (x, y) = 255
    
    where 
    f (x, y) = Coordinate Pixel Value
    T = Threshold Value.

    In OpenCV with Python, the function cv2.threshold is used for thresholding. 
     

    Syntax: cv2.threshold(source, thresholdValue, maxVal, thresholdingTechnique) 
    Parameters: 
    -> source: Input Image array (must be in Grayscale). 
    -> thresholdValue: Value of Threshold below and above which pixel values will change accordingly. 
    -> maxVal: Maximum value that can be assigned to a pixel. 
    -> thresholdingTechnique: The type of thresholding to be applied. 
     

     

    Simple Thresholding

    The basic Thresholding technique is Binary Thresholding. For every pixel, the same threshold value is applied. If the pixel value is smaller than the threshold, it is set to 0, otherwise, it is set to a maximum value.
    The different Simple Thresholding Techniques are: 
     

    • cv2.THRESH_BINARY: If pixel intensity is greater than the set threshold, value set to 255, else set to 0 (black).
    • cv2.THRESH_BINARY_INV: Inverted or Opposite case of cv2.THRESH_BINARY.
    • cv.THRESH_TRUNC: If pixel intensity value is greater than threshold, it is truncated to the threshold. The pixel values are set to be the same as the threshold. All other values remain the same.
    • cv.THRESH_TOZERO: Pixel intensity is set to 0, for all the pixels intensity, less than the threshold value.
    • cv.THRESH_TOZERO_INV: Inverted or Opposite case of cv2.THRESH_TOZERO.

     

    Cara menggunakan pillow threshold python

    Below is the Python code explaining different Simple Thresholding Techniques – 
     

    Python3




    # Python program to illustrate 

    # simple thresholding type on an image

          

    # organizing imports 

    import cv2 

    import numpy as np 

      

    # Python program to illustrate 0

    # Python program to illustrate 1

    # Python program to illustrate 2# Python program to illustrate 3 # Python program to illustrate 4# Python program to illustrate 5# Python program to illustrate 6

      

    # Python program to illustrate 8

    # Python program to illustrate 9

    # simple thresholding type on an image0

    # simple thresholding type on an image1# Python program to illustrate 3 # simple thresholding type on an image3

      

    # simple thresholding type on an image5

    # simple thresholding type on an image6

    # simple thresholding type on an image7

    # simple thresholding type on an image8

    # simple thresholding type on an image9# Python program to illustrate 3      1     2     3     4     5

         6# Python program to illustrate 3      1     2     3     4# organizing imports 2

    # organizing imports 3# Python program to illustrate 3      1     2     3     4# organizing imports 9

    import0# Python program to illustrate 3      1     2     3     4import6

    import7# Python program to illustrate 3      1     2     3     4cv2 3

      

    cv2 5

    cv2 6

    cv2 7

    cv2 8cv2 9import0

    cv2 8import2import3

    cv2 8import5import6

    cv2 8import8import9

    cv2 8numpy as np 1numpy as np 2

    numpy as np 

    numpy as np 4

    numpy as np 5 numpy as np 6numpy as np 7numpy as np 8numpy as np 9 # Python program to illustrate 3# Python program to illustrate 3  2 3

     4 5

    Input: 
     

    Cara menggunakan pillow threshold python

    Output: 
     

    Cara menggunakan pillow threshold python

     


    My Personal Notes arrow_drop_up

    Save

    Please Login to comment...