In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-22 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly shows you "how to achieve python object logo", the content is easy to understand, clear, hope to help you solve your doubts, the following let the editor lead you to study and learn "how to achieve python object logo" this article.
1. Read the color image for graying and binarization.
Def get_binary_img (img): # gray img to bin image bin_img = np.zeros (shape= (img.shape), dtype=np.uint8) h = img.shape [0] w = img.shape [1] for i in range (h): for j in range (w): bin_ IMG [I] [j] = 255if img [I] [j]
< 255 else 0 return bin_img# 调用file_name = "./test.bmp"img = cv2.imread(file_name)# 灰度化gray_img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)# 二值化bin_img = get_binary_img(gray_img) 2、目标标志,每个物体的像素值是该物体的标志,为计算面积打下基础。 # 标记目标def label_region(bin_img,width,height): visited = np.zeros(shape=bin_img.shape,dtype=np.uint8) label_img = np.zeros(shape=bin_img.shape, dtype=np.uint8) label = 0 for i in range(height): for j in range(width): if bin_img[i][j] == 255 and visited[i][j]==0 : //找到种子点 # visit visited[i][j] = 1 label += 1 label_img[i][j] = label # label label_from_seed(bin_img, visited, i, j, label, label_img) return label_img# 区域增长法进行标记def label_from_seed(bin_img,visited,i,j,label,out_img): directs = [(-1, -1), (0, -1), (1, -1), (1, 0), (1, 1), (0, 1), (-1, 1), (-1, 0)] seeds = [(i,j)] height = bin_img.shape[0] width = bin_img.shape[1] while len(seeds): seed = seeds.pop(0) i = seed[0] j = seed[1] if visited[i][j] == 0: visited[i][j] = 1 out_img[i][j] = label # 以(i,j)为起点进行标记 for direct in directs: cur_i = i + direct[0] cur_j = j + direct[1] # 非法 if cur_i < 0 or cur_j < 0 or cur_i >= height or cur_j > = width: continue # has not visited if visited [cur _ I] [cur_j] = = 0 and bin_ IMG [cur _ I] [cur_j] = = 255: Visited [cur _ I] [cur_j] = 1 out_ IMG [cur _ I] [cur_j] = label seeds.append ((cur_i,cur_j))
3. By traversing the marked images and counting the number of pixels in each number, the area of different regions can be obtained.
Def get_region_area (label_img Label): count = {key: 0 for key in range (label + 1)} start_pt = {key: (0label_img.shape 0) for key in range (label + 1)} height = label_img.shape [0] width = label_img.shape [1] for i in range (height): for j in range (width): key = label_ IMG [I] [j] count [key] + = 1 If count [key] = = 1: start_ PTT [key] = (j I) the above return count,start_pt is all the contents of the article "how to implement python object identification" Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.