数染色体 算法 python源码

效果图如下:

原图:

完整代码:

python 复制代码
import cv2
import numpy as np
from skimage import measure
import random


image = cv2.imread('113.jpg', cv2.IMREAD_GRAYSCALE)

blurred_img = cv2.GaussianBlur(image, (5, 5), 0)

_, binary_image = cv2.threshold(blurred_img, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)

kernel = np.ones((3, 3), np.uint8)
img2 = cv2.morphologyEx(binary_image, cv2.MORPH_OPEN, kernel, iterations=2)

labels = measure.label(img2, connectivity=2, background=0)
props = measure.regionprops(labels)

output_image = cv2.cvtColor(image, cv2.COLOR_GRAY2BGR)

colors = []
for _ in range(len(props)):
    colors.append((random.randint(0, 255), random.randint(0, 255), random.randint(0, 255)))

chromosome_count = len(props)
chromosome_sizes = []

for i, prop in enumerate(props):
    y, x = prop.centroid
    center = (int(x), int(y))

    cv2.circle(output_image, center, 5, colors[i], -1)

    cv2.putText(output_image, f'{i+1}', (center[0] + 10, center[1]), cv2.FONT_HERSHEY_SIMPLEX, 0.5, colors[i], 1)

    chromosome_sizes.append(prop.area)

print(f"染色体数量: {chromosome_count}")
print(f"每个染色体的尺寸(像素数量): {chromosome_sizes}")

cv2.imshow('img2', img2)
cv2.imshow('output_image', output_image)
cv2.waitKey(0)
cv2.destroyAllWindows()
相关推荐
小O的算法实验室2 小时前
IEEE TASE,基于MPC的多无人机协同搜索竞争群体优化方法
算法
Tbisnic2 小时前
BGE-M3 算法详解:从模型架构到三种检索方式的数学原理
算法·自然语言处理·大模型·bert·transformer·注意力机制
聪明蛋子哟3 小时前
Stagehand v3多语言SDK:Python/Go/Rust/Java下的浏览器自动化统一方案
python·golang·rust
Brilliantwxx3 小时前
【算法从零到千】【55-58】哈希位图+常见数学运算 接口
算法
AI的探索之旅3 小时前
97 个 OpenCV 实例(七):SIMD 向量化,给像素处理装上涡轮增压
人工智能·opencv·计算机视觉
今天AI了吗3 小时前
Python 基础语法(一):常量、变量、输入输出与运算符
开发语言·数据库·人工智能·python·sql·深度学习·机器学习
卷无止境3 小时前
Windows 上丝滑开发 Python,并稳定构建 Docker 镜像
后端·python·docker
空堂与归4 小时前
用户分群找不到规律?用K-Means聚类算法自动发现数据模式
算法·机器学习·kmeans·聚类
TELL5214 小时前
selenium webdriver 第二次初始化的异常
开发语言·python
Csvn4 小时前
🐍 Day 4: Python 控制流 — 条件、循环与推导式的艺术
后端·python