资源简介
PY语言对大米进行识别计数,基础的数字图像处理
![](http://www.nz998.com/pic/71770.jpg)
代码片段和文件信息
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
- 上一篇:在我的世界Minecraft 中用Python搭建剑球游戏
- 下一篇:音乐爬虫
相关资源
- pywin32_python3.6_64位
- python+ selenium教程
- PycURL(Windows7/Win32)Python2.7安装包 P
- 英文原版-Scientific Computing with Python
- 7.图像风格迁移 基于深度学习 pyt
- 基于Python的学生管理系统
- A Byte of Python(简明Python教程)(第
- Python实例174946
- Python 人脸识别
- Python 人事管理系统
- 基于python-flask的个人博客系统
- 计算机视觉应用开发流程
- python 调用sftp断点续传文件
- python socket游戏
- 基于Python爬虫爬取天气预报信息
- python函数编程和讲解
- Python开发的个人博客
- 基于python的三层神经网络模型搭建
- python实现自动操作windows应用
- python人脸识别(opencv)
- python 绘图(方形、线条、圆形)
- python疫情卡UN管控
- python 连连看小游戏源码
- 基于PyQt5的视频播放器设计
- 一个简单的python爬虫
- csv文件行列转换python实现代码
- Python操作Mysql教程手册
- Python Machine Learning Case Studies
- python获取硬件信息
- 量化交易(附python常见函数的使用方
评论
共有 条评论