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
0
投稿
猜你喜欢
- execjs 使用有了selenium+Chrome Headless 加载页面为什么还要用execjs来运行js?selenium+Chr
- 语法:CREATE TRIGGER trigger_name trigger_time trigger_eventON tbl_name F
- 引言在观察OpenCV中某个函数在不同参数的情况下,所得到的效果的时候,我之前是改一次参数运行一次,这样做起来操作麻烦,效率低下。为了更便捷
- 在MySQL中,使用auto_increment类型的id字段作为表的主键,并用它作为其他表的外键,形成“主从表结构”,这是数据库设计中常见
- 假如一个页面中的文本采用的都是同样的字体、同样的字号、同样的颜色,做为读者的你能轻易的区分出哪里是标题,哪里是正文内容吗?所以通常情况下,设
- 在应用SA-FileUp时,必须确认用户已对目的路径有读、写、删除的权力。在多文件上传中,由于浏览器不支持SIZE= 属性,所以对多文件的情
- Flask-Admin是一个功能齐全、简单易用的Flask扩展,让你可以为Flask应用程序增加管理界面。它受django-ad
- 本文给出了几个表单常用的js验证函数,有检查、\等特殊字符的,有检查是否含有空格,检查是否为Email 地址,也有检查是否是小数或负数的,检
- 准备篇:1、配置防火墙,开启80端口、3306端口vi /etc/sysconfig/iptables-A INPUT -m state -
- 用过软件的朋友都知道,进度条是一个优秀软件的重要组成部分。它的存在能够使用户及时掌握程序的运行进度,确认应用程序正常工作。可是ASP中似乎没
- 本文实例讲述了Python正则表达式实现截取成对括号的方法。分享给大家供大家参考,具体如下:strs = '1(2(3(4(5(67
- 作为收费应用方面的数据库管理员(DBA),公司首席信息官(CIO)经常邀请我与Sarbanes-Oxley审查员开会讨 * 司数据的安全与整合
- 询问度娘搭好appium和python环境,开启移动app自动化的探索(基于Android),首先来记录下如何启动待测的app吧!如何启动A
- python3的编码问题。打开python开发工具IDLE,新建‘codetest.py'文件,并写代码如下:import sysp
- 前言数据处理过程中,经常会遇到数据有缺失值的情况,本文介绍如何用Pandas处理数据中的缺失值。一、什么是缺失值对数据而言,缺失值分为两种,
- 我们可以利用err对象来判断。当程序没有出现错误就说明已经执行了sql操作: sql="insert into
- 概述在我们使用内置打印函数print时,打印出的Python数据结构对象总是一行的输出的方式,这样对数据结构较复杂或数据较多的对象的显示并不
- 有一个网站A想找别的网站作为代理商,在代理商的页面上插入自己的广告。它提供一个注册页面给代理商,代理商注册后,会得到一个ID,
- 前言今天,在网上发现一款很棒的python画图工具库。很简单的api调用就能生成漂亮的图表。并且可以进行一些互动。pyecharts 是一个
- 原文地址:30 Days of Mootools 1.2 Tutorials - Day 13 - Regular ExpressionsM