test_threshold.py
Go to the documentation of this file.
1 import cv2 as cv
2 import numpy as np
3 from matplotlib import pyplot as plt
4 
5 def test_random():
6  min = 50
7  max = 200
8  img = np.random.randint(0,200, size=(172,224), dtype=np.uint16)
9  ret,thresh1 = cv.threshold(img,min,max,cv.THRESH_BINARY)
10  ret,thresh2 = cv.threshold(img,min,max,cv.THRESH_BINARY_INV)
11  ret,thresh3 = cv.threshold(img,min,max,cv.THRESH_TRUNC)
12  ret,thresh4 = cv.threshold(img,min,max,cv.THRESH_TOZERO)
13  ret,thresh5 = cv.threshold(img,min,max,cv.THRESH_TOZERO_INV)
14 
15  titles = ['Original Image','BINARY','BINARY_INV','TRUNC','TOZERO','TOZERO_INV']
16  images = [img, thresh1, thresh2, thresh3, thresh4, thresh5]
17  for i in range(6):
18  plt.subplot(2,3,i+1),plt.imshow(images[i],'gray',vmin=0,vmax=255)
19  plt.title(titles[i])
20  plt.xticks([]),plt.yticks([])
21  plt.show()
22 
23 
24 def test_image():
25  img = cv.imread('gradient.jpg',0)
26  ret,thresh1 = cv.threshold(img,127,255,cv.THRESH_BINARY)
27  ret,thresh2 = cv.threshold(img,127,255,cv.THRESH_BINARY_INV)
28  ret,thresh3 = cv.threshold(img,127,255,cv.THRESH_TRUNC)
29  ret,thresh4 = cv.threshold(img,127,255,cv.THRESH_TOZERO)
30  ret,thresh5 = cv.threshold(img,127,255,cv.THRESH_TOZERO_INV)
31 
32  titles = ['Original Image','BINARY','BINARY_INV','TRUNC','TOZERO','TOZERO_INV']
33  images = [img, thresh1, thresh2, thresh3, thresh4, thresh5]
34  for i in range(6):
35  plt.subplot(2,3,i+1),plt.imshow(images[i],'gray',vmin=0,vmax=255)
36  plt.title(titles[i])
37  plt.xticks([]),plt.yticks([])
38  plt.show()
39 
40 if __name__ == '__main__':
41  test_random()


ifm3d_ros_driver
Author(s): CSR ifm sytron
autogenerated on Tue Feb 21 2023 03:13:25