Advertisement
FancyKing

闭包面积简单计算

May 22nd, 2021
1,107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.26 KB | None | 0 0
  1. # Maintain by FancyKing
  2. # Created 1621712715
  3.  
  4. # Assuming that the position of the image (0,0) is black,
  5. # solve for the data of the percentage of black blocks in the white closed area.
  6.  
  7. from collections import deque
  8.  
  9. import cv2
  10. import numpy as np
  11. import threading
  12.  
  13. # global pool START
  14. blk_size_t = 0
  15. blk_size_flag = False
  16. wit_size_t = 0
  17. wit_size_flag = False
  18. # global pool END
  19.  
  20.  
  21. def black_square(img):
  22.     # use global var
  23.     global blk_size_t, blk_size_flag
  24.     neighbors = deque([(0, 0)])
  25.     flag = np.zeros((img.shape[0], img.shape[1]), dtype=np.int)
  26.     blk_area = 0
  27.     while neighbors:
  28.         row, col = neighbors.popleft()
  29.         flag[row][col] = 1
  30.         blk_area = blk_area + 1
  31.         for x, y in [
  32.             (row - 1, col), (row, col - 1), (row + 1, col), (row, col + 1),
  33.             (row + 1, col + 1), (row + 1, col - 1),
  34.             (row - 1, col + 1), (row - 1, col - 1)
  35.         ]:
  36.             if 0 <= x < img.shape[0] and 0 <= y < img.shape[1] \
  37.                     and flag[x][y] == 0 and img[x][y][0] == 0:
  38.                 neighbors.append((x, y))
  39.                 flag[x][y] = 1
  40.     blk_size_t = blk_area
  41.     blk_size_flag = True
  42.     return blk_area
  43.  
  44.  
  45. def get_white_pix_num(img):
  46.     # use global var
  47.     global wit_size_t, wit_size_flag
  48.     nums = 0
  49.     for line in img:
  50.         for cow in line:
  51.             if cow[0] == 255:
  52.                 nums = nums + 1
  53.     wit_size_t = nums
  54.     wit_size_flag = True
  55.     return nums
  56.  
  57.  
  58. def get_pure_img(img):
  59.     return cv2.threshold(img, 128, 255, cv2.THRESH_BINARY)[1]
  60.  
  61.  
  62. def get_img(dir):
  63.     return cv2.imread(dir, cv2.COLOR_BGR2GRAY)
  64.  
  65.  
  66. def show_img(img):
  67.     cv2.namedWindow('pure')
  68.     cv2.imshow('pure', img)
  69.     cv2.waitKey(0)
  70.  
  71.  
  72. def scan_lines(img):
  73.     into_white = False
  74.     all_line_black_num = 0
  75.     sub_line_black_num = 0
  76.     for line in img:
  77.         for pix in line:
  78.             if pix[0] == 0:
  79.                 into_white = True
  80.             elif pix[0] == 255:
  81.                 if into_white:
  82.                     sub_line_black_num = sub_line_black_num + 1
  83.         if into_white:
  84.             all_line_black_num = all_line_black_num + sub_line_black_num
  85.         sub_line_black_num = 0
  86.         into_white = False
  87.     return all_line_black_num
  88.  
  89.  
  90. if __name__ == '__main__':
  91.     # use global var
  92.     imgs = ['T.jpg']
  93.     for item in imgs:
  94.         origin_img = get_img(item)
  95.         pure_img = get_pure_img(origin_img)
  96.         pure_img = cv2.resize(pure_img, [pure_img.shape[1] * 2, pure_img.shape[0] * 2])
  97.         t_b = threading.Thread(target=black_square, args=(pure_img,))
  98.         t_b.setDaemon(True)
  99.         t_b.start()
  100.         t_w = threading.Thread(target=get_white_pix_num, args=(pure_img,))
  101.         t_w.setDaemon(True)
  102.         t_w.start()
  103.         t_b.join()
  104.         t_w.join()
  105.         image_size = pure_img.shape[0] * pure_img.shape[1]
  106.         black_size, white_size = 0, 0
  107.         while blk_size_flag:
  108.             black_size = blk_size_t
  109.             break
  110.         while wit_size_flag:
  111.             white_size = wit_size_t
  112.             break
  113.         print('image size:', image_size)
  114.         print('black areas:', black_size)
  115.         print('white areas:', white_size)
  116.         print('Answer:', (image_size - black_size - white_size) / white_size)
  117.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement