Advertisement
acclivity

Simple Black/White picture analysis

Jun 30th, 2021
284
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.76 KB | None | 0 0
  1. import cv2
  2. import numpy as np
  3. img = cv2.imread("9.jpg")
  4.  
  5. lc = 0          # Line counter
  6.  
  7. for line in img:
  8.     last = (sum(line[0]) > 382)        # Get colour of first pixel on the line (Black or White)
  9.     lc += 1
  10.     ctr = 0
  11.     pos = 0
  12.     print("Line:", lc, end=": ")
  13.     for y in line:
  14.         v = (sum(y) > 382)          # Black = 0, White = 1
  15.         if v == last:               # Same colour as last pixel?
  16.             ctr += 1
  17.         else:
  18.             print(last, ctr, end="  ")
  19.             if last:
  20.                 print("mid:", pos + ctr // 2, end="  ")
  21.             pos += ctr
  22.             ctr = 1
  23.             last = v
  24.  
  25.     print(last, ctr)
  26.     # print("Line", lc, "White width", ctr)
  27.  
  28. #show image
  29. cv2.imshow("img", img)
  30. cv2.waitKey(0)
  31.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement