การทำ segmentation บนภาพโดยใช้ Threshold ด้วย opencv python

Dr. Pathasu Doungmala
2 min readAug 11, 2021

Python program to read image using OpenCV

############################################

import cv2

# Save image in set directory

# Read RGB image

img1 = cv2.imread(‘Original3/00000001_1.bmp’)

img = cv2.cvtColor(img1, cv2.COLOR_BGR2GRAY)

# applying different thresholding

# techniques on the input image

# all pixels value above 120 will

# be set to 255

ret, thresh1 = cv2.threshold(img, 100, 255, cv2.THRESH_BINARY)

ret, thresh2 = cv2.threshold(img, 120, 255, cv2.THRESH_BINARY_INV)

ret, thresh3 = cv2.threshold(img, 120, 255, cv2.THRESH_TRUNC)

ret, thresh4 = cv2.threshold(img, 120, 255, cv2.THRESH_TOZERO)

ret, thresh5 = cv2.threshold(img, 120, 255, cv2.THRESH_TOZERO_INV)

# the window showing output images

# with the corresponding thresholding

# techniques applied to the input images

cv2.imshow(‘Binary Threshold’, thresh1)

cv2.imshow(‘Binary Threshold Inverted’, thresh2)

cv2.imshow(‘Truncated Threshold’, thresh3)

cv2.imshow(‘Set to 0’, thresh4)

cv2.imshow(‘Set to 0 Inverted’, thresh5)

# Output img with window name as ‘image’

cv2.imshow(‘image’, img)

# Maintain output window utill

# user presses a key

cv2.waitKey(0)

# Destroying present windows on screen

cv2.destroyAllWindows()

Threshold ด้วย opencv python

หมายเหตุท้าย:
หากคุณชอบบทความนี้อย่าลืมคลิก❤ด้านล่างเพื่อแนะนำและถ้าคุณมีคำถามใด ๆ แสดงความคิดเห็นและฉันจะพยายามอย่างดีที่สุดที่จะตอบ คุณสามารถติดตามฉันบน
facebook page (https://www.facebook.com/nextsoftwarehousethailand/)
website : www.nextsoftwarethailand.com
และสามารถส่งอีเมลถึงฉัน

ขาย source code งานด้าน image processing ด้วย python, matlab
เหล้าเตยหอม — ไร่ฟ้าเปลี่ยนสี

--

--

Dr. Pathasu Doungmala

Founder of Next Software — I am working on image processing, pattern recognition and AI to help reduce working in an industry.