Python实现数字图像处理染色体计数示例
作者:悲恋花丶无心之人 发布时间:2022-06-15 03:32:36
标签:Python,数字图像处理,染色体计数
一、实验内容
对于下面这幅图像,编程实现染色体计数,并附简要处理流程说明。
二、实验步骤
1.中值滤波
2.图像二值化
3.膨胀图像
4.腐蚀图像
5.计算光影背景
6.移除背景
7.检测染色体
三、代码
import cv2
import numpy as np
# 计算光影背景
def calculateLightPattern(img4):
h, w = img4.shape[0], img4.shape[1]
img5 = cv2.blur(img4, (int(w/3), int(w/3)))
return img5
# 移除背景
def removeLight(img4, img5, method):
if method == 1:
img4_32 = np.float32(img4)
img5_32 = np.float32(img5)
ratio = img4_32 / img5_32
ratio[ratio > 1] = 1
aux = 1 - ratio
# 按比例转换为8bit格式
aux = aux * 255
aux = np.uint8(aux)
else:
aux = img5 - img4
return aux
def ConnectedComponents(aux):
num_objects, labels = cv2.connectedComponents(aux)
if num_objects < 2:
print("connectedComponents未检测到染色体")
return
else:
print("connectedComponents检测到染色体数量为:", num_objects - 1)
output = np.zeros((aux.shape[0], aux.shape[1], 3), np.uint8)
for i in range(1, num_objects):
mask = labels == i
output[:, :, 0][mask] = np.random.randint(0, 255)
output[:, :, 1][mask] = np.random.randint(0, 255)
output[:, :, 2][mask] = np.random.randint(0, 255)
return output
def ConnectedComponentsStats(aux):
num_objects, labels, status, centroids = cv2.connectedComponentsWithStats(aux)
if num_objects < 2:
print("connectedComponentsWithStats未检测到染色体")
return
else:
print("connectedComponentsWithStats检测到染色体数量为:", num_objects - 1)
output = np.zeros((aux.shape[0], aux.shape[1], 3), np.uint8)
for i in range(1, num_objects):
mask = labels == i
output[:, :, 0][mask] = np.random.randint(0, 255)
output[:, :, 1][mask] = np.random.randint(0, 255)
output[:, :, 2][mask] = np.random.randint(0, 255)
return output
def FindContours(aux):
contours, hierarchy = cv2.findContours(aux, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
if len(contours) == 0:
print("findContours未检测到染色体")
return
else:
print("findContours检测到染色体数量为:", len(contours))
output = np.zeros((aux.shape[0], aux.shape[1], 3), np.uint8)
for i in range(len(contours)):
cv2.drawContours(
output,
contours,
i,
(np.random.randint(0, 255),
np.random.randint(0, 255),
np.random.randint(0, 255)), 2)
return output
# 读取图片
img = cv2.imread('img.png', 0)
pre_img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) # 二值化函数
# 第一步:中值滤波
# 中值滤波
img1 = cv2.medianBlur(img, 3)
# 显示并保存图片
cv2.imshow('gray', img)
cv2.imshow('medianBlur', img1)
cv2.imwrite('medianBlur.jpg', img1)
# 第二步:图像二值化
# 图像二值化
ret, img2 = cv2.threshold(img1, 140, 255, 0, img1) # 二值化函数
# 显示并保存图片
cv2.imshow('threshold', img2)
cv2.imwrite('threshold.jpg', img2)
# 第三步:膨胀图像
dilate_kernel = np.ones((3, 3), np.uint8)
img3 = cv2.dilate(img2, dilate_kernel)
# 显示并保存图片
cv2.imshow('dilate', img3)
cv2.imwrite('dilate.jpg', img3)
# 第四步:腐蚀图像
erode_kernel = np.ones((7, 7), np.uint8)
img4 = cv2.erode(img3, erode_kernel)
# 显示并保存图片
cv2.imshow('erode', img4)
cv2.imwrite('erode.jpg', img4)
# 第五步:计算光影背景
img5 = calculateLightPattern(img4)
# 显示并保存图片
cv2.imshow('LightPattern', img5)
cv2.imwrite('LightPattern.jpg', img5)
# 第六步:移除背景
aux = removeLight(img4, img5, 1)
# 显示并保存图片
cv2.imshow('removeLight', aux)
cv2.imwrite('removeLight.jpg', aux)
# 第七步:检测轮廓
output1 = ConnectedComponents(aux)
output2 = ConnectedComponentsStats(aux)
output3 = FindContours(aux)
# 显示并保存图片
cv2.imshow('connectedComponents', output1)
cv2.imwrite('connectedComponents.jpg', output1)
cv2.imshow('connectedComponentsWithStats', output2)
cv2.imwrite('connectedComponentsWithStats.jpg', output2)
cv2.imshow('findContours', output3)
cv2.imwrite('findContours.jpg', output3)
cv2.waitKey(0)
四、结果
1.中值滤波
2.图像二值化
3.膨胀图像
4.腐蚀图像
5.计算光影背景
6.移除背景
7.检测染色体
(1)connectedComponents.jpg
(2)connectedComponentsWithStats.jpg
(3)findContours.jpg
染色体个数为46
来源:https://blog.csdn.net/qq_36556893/article/details/104497567


猜你喜欢
- 本文实例讲述了Python微信企业号文本消息推送功能。分享给大家供大家参考,具体如下:企业号的创建、企业号应用的创建、组、tag、part就
- 当我们想指定每一层的学习率时:optim.SGD([ &
- 实例如下所示:import matplotlib as mtimport numpy as np y=[7,0,0,0,0,0,1,25,9
- 概述web项目,经常需要热启动各种各样的配置信息,一旦这些服务发生变更,我们需要重新启动web server,以使配置生效,实现配置热加载。
- 问题问题1:如何解决事务提交时flush redo log带来的性能损失WAL是实现事务持久性(D)的一个常用技术,基本原理是将事务的修改记
- 本文实例讲述了Python列表解析操作。分享给大家供大家参考,具体如下:列表解析Python 的强大特性之一是其对 list 的解析,它提供
- 1、简单的代码from matplotlib import pyplot as pltimport seaborn as snsimport
- 1、在Asp页面首部加入Response.Buffer = True Response.ExpiresAbso
- 毫无疑问,我们生活在编辑器的最好年代,Vim是仅在Vi之下的神级编辑器,而脱胎于Vim的NeoVim则是这个时代最好的编辑器,没有之一。异步
- 网络连接与通信是我们学习任何编程语言都绕不过的知识点。 Python 也不例外,本文就介绍因特网的核心协议 TCP ,以及如何用 Pytho
- 一、 软件配置安装必备爬虫环境软件:python 3.8pip install requestspip install beautifuls
- 本文实例讲述了Python多线程通信queue队列用法。分享给大家供大家参考,具体如下:queue:什么是队列:是一种特殊的结构,类似于列表
- 一、并行复制的背景首先,为什么会有并行复制这个概念呢?1. DBA都应该知道,MySQL的复制是基于binlog的。 2. My
- 近日在学习C++,看到函数指针,由于之前一直搞ASP,所以想ASP里面是否也有这个函数指针的东西,于是翻了翻VBScript手册,没让我失望
- 在图像裁剪操作中,opencv和pillow两个库都具有相应的函数,但是这两个库中的函数仅仅能对与图片平行的矩形进行裁剪操作,如果想要对目标
- 基于python opencv人脸识别的签到系统前言先看下效果实现的功能开始准备页面的构建功能实现代码部分总结前言一个基于opencv人脸识
- 在SQL Server 中插入一条数据使用Insert语句,但是如果想要批量插入一堆数据的话,循环使用Insert不仅效率低,而且会导致SQ
- 练手项目,先上图先实现一个简单的串口工具,为之后的上位机做准备代码如下:github 下载地址pyserial_demo.pyimport
- 视频本教程的视频选择图形我们谈到了 Opengameart.org,这是免费游戏艺术的重要来源,也是我们最喜欢的艺术家之一&ldqu
- 1.Cuda的下载安装及配置 首先我们要确定本机是否有独立显卡。在计算机-管理-设备管