python + opencv image projection, horizontal projection, a vertical projection

The image projection python + opencv

An image projection

Horizontal projection: y-axis projection axis

Vertical projection: the x-axis projection axis

Original:

Second, the horizontal projection

Code and explain:

1  # horizontal projection 
2  Import numpy AS NP
 . 3  Import CV2 AS CV 
 . 4 IMG = cv.imread ( " 123.jpg " , 0)
 . 5 RET, IMG1 = cv.threshold (IMG, 80,255 , cv.THRESH_BINARY)
 . 6  
. 7  # Returns height and width of images 
. 8 (H, W) = img1.shape
 . 9  
10  # initialize an array length, like high image for recording the number of black dots in each row 
. 11 a = [0 for Z in Range (0, H)]
 12 is  
13 is  for I in Range (0, H):           # through each row 
14      forJ in Range (0, W):       # through each column 
15          IF IMG1 [I, J] == 0:       # determines whether the point is a black dot, indicated by the black 0 
16              A [I] +. 1 =            # of the line a counter is incremented 
. 17              IMG1 [I, J] = 255      # to change it to white point, which is equal to 255 
18 is  for I in Range (0, H):           # through each row 
. 19      for J in Range (0, a [I] ):    # from the line to be blackened leftmost point to a black point to begin the rightmost point 
20 is          IMG1 [I, J] = 0            # a black dot 
21 is cv.imshow ( " IMG " , IMG1) 
22 cv.waitKey(0) 
23 cv.destroyAllWindows() 

Renderings:

Third, the vertical projection

Code and explain:

1  # vertical projection 
2  Import numpy AS NP
 . 3  Import CV2 AS CV 
 . 4 IMG = cv.imread ( " 123.jpg " , 0)
 . 5 RET, IMG1 = cv.threshold (IMG, 80,255 , cv.THRESH_BINARY)
 . 6  
. 7  # Returns height and width of images 
. 8 (H, W) = img1.shape
 . 9  
10  # initialize image with a wide array of the same length, the number of black dot recording for each column 
. 11 a = [0 for Z in Range (0, W)]
 12 is  
13 is  for I in Range (0, W):            # through each column   
14     for J in Range (0, H):        # through each row 
15          IF IMG2 [J, I] == 0:        # determines whether the point is a black dot, 0 represents a black dot 
16              A [I] +. 1 =             # of the column counter is incremented. 1 
. 17              IMG2 [J, I] = 255       # record after it becomes white, that is equal to 255 
18 is  for I in Range (0, W):            # through each column 
. 19      for J in Range (HA [ I], H):   # is set to the lowermost black dot start with the top of the column to be blackened 
20 is          IMG2 [J, I] = 0             # set black dot 
21 is cv.imshow ( " IMG",img2) 
22 cv.waitKey(0) 
23 cv.destroyAllWindows()

Renderings:

Fourth, experience sharing

(1) direct calls to the image width will complain

Solution: height and width of the image to a return value, the return value call

(2) use cv.THRESH_BINARY_INV binarization process functions, the actual effect will lead to the desired effect with the schema of FIG.

Solutions; the function of the binarization processing to cv.THRESH_BINARY

 

Guess you like

Origin www.cnblogs.com/cyt99/p/12466512.html