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.中值滤波

Python实现数字图像处理染色体计数示例

2.图像二值化

Python实现数字图像处理染色体计数示例

3.膨胀图像

Python实现数字图像处理染色体计数示例

4.腐蚀图像

Python实现数字图像处理染色体计数示例

5.计算光影背景

Python实现数字图像处理染色体计数示例

6.移除背景

Python实现数字图像处理染色体计数示例

7.检测染色体

(1)connectedComponents.jpg

Python实现数字图像处理染色体计数示例

(2)connectedComponentsWithStats.jpg

Python实现数字图像处理染色体计数示例

(3)findContours.jpg

Python实现数字图像处理染色体计数示例

染色体个数为46

来源:https://blog.csdn.net/qq_36556893/article/details/104497567

标签:Python,数字图像处理,染色体计数
0
投稿

猜你喜欢

  • SQL指令植入式攻击的危害及其防范措施

    2008-12-19 14:07:00
  • 求英文字母或数字或下划线或汉字的js正则表达式

    2009-08-04 17:59:00
  • Python将字典转换为XML的方法

    2023-08-04 16:13:22
  • 怎样在不同版本SQL Server中存储数据

    2009-01-20 13:11:00
  • Python Pandas学习之基本数据操作详解

    2021-11-07 17:46:56
  • golang中sync.Map并发创建、读取问题实战记录

    2023-07-16 12:23:27
  • 利用PHP自动生成印有用户信息的名片

    2023-09-10 22:30:56
  • python实现单张图像拼接与批量图片拼接

    2023-07-28 12:33:36
  • Python使用pickle进行序列化和反序列化的示例代码

    2022-11-17 10:46:22
  • 中文段首不需要空两格

    2010-04-23 20:31:00
  • 实例讲解Access数据库在线压缩的实现方法

    2008-11-28 14:29:00
  • Python的Flask框架中Flask-Admin库的简单入门指引

    2021-11-06 03:30:19
  • asp如何制作一个文本文件编辑器?

    2010-07-07 12:26:00
  • python openssl模块安装及用法

    2023-07-03 12:01:04
  • Dreamweaver制作网页幻灯片效果

    2010-07-06 13:48:00
  • python编程控制Android手机操作技巧示例

    2021-12-01 07:59:05
  • php小技巧之过滤ascii控制字符

    2023-10-03 05:13:15
  • 使用python把xmind转换成excel测试用例的实现代码

    2022-12-21 06:21:34
  • list视图方式设计浅析

    2008-12-21 16:04:00
  • Go web入门Go pongo2模板引擎

    2023-07-22 22:16:43
  • asp之家 网络编程 m.aspxhome.com