资源简介
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
- 上一篇:在我的世界Minecraft 中用Python搭建剑球游戏
- 下一篇:音乐爬虫
相关资源
- 在我的世界Minecraft 中用Python搭建剑球
- python图片爬取.rar
- 人工免疫算法python
- Python scrapy爬取豆瓣电影top250
- Python员工信息管理系统
- cohesive_COH2D4 for Quad.py
- 新浪微博爬虫代码+结果
- Python-RNNoiseRNN音频噪声抑制学习
- Python-Keras实现实时语义分割的深层神
- Python-通过百度语音API实现文本转语音
- Python-Binance虚拟货币交易机器人
- Python-2019年百度的三元组抽取比赛一个
- Python-微信公众号历史文章爬取api
- Python-手势识别使用在TensorFlow中卷积神
- Python-python识别字符验证码
- Python-利用pandas将excel中数据抽取以三
- Python-基于pygame和tkinter本地音乐播放器
- HTMLTestRunner.py python3.6
- Python+Django+MySQL实现基于Web版的增删改
- python 使用while循环输出*组成的菱形
- python实现人机五子棋
- python tkinter实现界面切换的
- 使用Python串口实时显示数据并绘图的
- python 决策树算法的实现
- 520使用Python实现“我爱你”表白
- Python_LDA实现方法详解
- python实现宿舍管理系统
- Python学习教程哈工大、嵩天.txt
- Python 写了个新型冠状病毒疫情传播模
- python 爬虫一键爬取 淘宝天猫宝贝页面
评论
共有 条评论