资源简介

PY语言对大米进行识别计数,基础的数字图像处理

资源截图

代码片段和文件信息

import cv2
import numpy as np

# 4邻域的连通域和 8邻域的连通域
# [row col]
NEIGHBOR_HOODS_4 = True
OFFSETS_4 = [[0 -1] [-1 0] [0 0] [1 0] [0 1]]

NEIGHBOR_HOODS_8 = False
OFFSETS_8 = [[-1 -1] [0 -1] [1 -1]
             [-1  0] [0  0] [1  0]
             [-1  1] [0  1] [1  1]]



def reorganize(binary_img: np.array):
    index_map = []
    points = []
    index = -1
    rows cols = binary_img.shape
    for row in range(rows):
        for col in range(cols):
            var = binary_img[row][col]
            if var < 0.5:
                continue
            if var in index_map:
                index = index_map.index(var)
                num = index + 1
            else:
                index = len(index_map)
                num = index + 1
                index_map.append(var)
                points.append([])
            binary_img[row][col] = num
            points[index].append([row col])
    return binary_img points



def neighbor_value(binary_img: np.array offsets reverse=False):
    rows cols = binary_img.shape
    label_idx = 0
    rows_ = [0 rows 1] if reverse == False else [rows-1 -1 -1]
    cols_ = [0 cols 1] if reverse == False else [cols-1 -1 -1]
    for row in range(rows_[0] rows_[1] rows_[2]):
        for col in range(cols_[0] cols_[1] cols_[2]):
            label = 256
            if binary_img[row][col] < 0.5:
                continue
            for offset in offsets:
                neighbor_row = min(max(0 row+offset[0]) rows-1)
                neighbor_col = min(max(0 col+offset[1]) cols-1)
                neighbor_val = binary_img[neighbor_row neighbor_col]
                if neighbor_val < 0.5:
                    continue
                label = neighbor_val if neighbor_val < label else label
            if label == 255:
                label_idx += 1
                label = label_idx
            binary_img[row][col] = label
    return binary_img

def recursive_seed(binary_img: np.array seed_row seed_col offsets num max_num=100):
    rows cols = binary_img.shape
    binary_img[seed_row][seed_col] = num
    for offset in offsets:
        neighbor_row = min(max(0 seed_row+offset[0]) rows-1)
        neighbor_col = min(max(0 seed_col+offset[1]) cols-1)
        var = binary_img[neighbor_row][neighbor_col]
        if var < max_num:
            continue
        binary_img = recursive_seed(binary_img neighbor_row neighbor_col offsets num max_num)
    return binary_img

# binary_img: bg-0 object-255; int
def Two_Pass(binary_img neighbor_hoods max_num=100):
    if neighbor_hoods == NEIGHBOR_HOODS_4:
        offsets = OFFSETS_4
    elif neighbor_hoods == NEIGHBOR_HOODS_8:
        offsets = OFFSETS_8
    else:
        raise ValueError

    num = 1
    rows cols = binary_img.shape
    for row in range(rows):
        for col in range(cols):
            var = binary_img[row][col]
            if v

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----
     文件        7898  2020-10-05 15:45  代码\task1.png
     文件        3680  2020-10-05 16:42  代码\test.py

评论

共有 条评论