目录

一、实验内容

二、实验步骤

三、代码

四、结果


一、实验内容

对于下面这幅图像,编程实现染色体计数,并附简要处理流程说明。

二、实验步骤

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_32ratio[ratio > 1] = 1aux = 1 - ratio# 按比例转换为8bit格式aux = aux * 255aux = np.uint8(aux)else:aux = img5 - img4return auxdef ConnectedComponents(aux):num_objects, labels = cv2.connectedComponents(aux)if num_objects < 2:print("connectedComponents未检测到染色体")returnelse: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 == ioutput[:, :, 0][mask] = np.random.randint(0, 255)output[:, :, 1][mask] = np.random.randint(0, 255)output[:, :, 2][mask] = np.random.randint(0, 255)return outputdef ConnectedComponentsStats(aux):num_objects, labels, status, centroids = cv2.connectedComponentsWithStats(aux)if num_objects < 2:print("connectedComponentsWithStats未检测到染色体")returnelse: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 == ioutput[:, :, 0][mask] = np.random.randint(0, 255)output[:, :, 1][mask] = np.random.randint(0, 255)output[:, :, 2][mask] = np.random.randint(0, 255)return outputdef FindContours(aux):contours, hierarchy = cv2.findContours(aux, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)if len(contours) == 0:print("findContours未检测到染色体")returnelse: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

数字图像处理 染色体计数 Python实现相关推荐

  1. 数字图像处理——实验一 Python中数字图像处理的基本操作

    数字图像处理--实验一 Python中数字图像处理的基本操作 一.实验目的 二.实验主要仪器设备 三.实验原理 3.1 数字图像的表示和类别 3.2 opencv-python图像文件格式 四.实验内 ...

  2. 数字图像处理 python_5使用Python处理数字的高级操作

    数字图像处理 python Numbers are everywhere in our daily life - there are phone numbers, dates of birth, ag ...

  3. 基于Opencv的数字图像处理技巧(python)

    图像处理(image processing),用计算机对图像进行分析,以达到所需结果的技术.又称影像处理.图像处理一般指数字图像处理.数字图像是指用工业相机.摄像机.扫描仪等设备经过拍摄得到的一个大的 ...

  4. 数字图像处理 使用opencv+python识别七段数码显示器的数字

    一.什么是七段数码显示器 七段LCD数码显示器有很多叫法:段码液晶屏.段式液晶屏.黑白笔段屏.段码LCD液晶屏.段式显示器.TN液晶屏.段码液晶显示器.段码屏幕.笔段式液晶屏.段码液晶显示屏.段式LC ...

  5. 数字图像处理之用Python+GDAL实现BSQ格式转换为BIP格式,BIL格式转换为BSQ,BIP格式

    1.环境:PyCharm2022.2.3+Python3.9.1+GDAL(GDAL-3.4.3-cp39-cp39-win_amd64) 2.概念: (1)BSQ (band sequential) ...

  6. (附源码)python数字图像处理课程平台 毕业设计 242339

    Python数字图像处理课程平台的开发 摘 要 数字图像处理是一门新兴技术,随着计算机硬件的发展,数字图像的实时处理已经成为可能,由于数字图像处理的各种算法的出现,使得其处理速度越来越快,能更好的为人 ...

  7. (附源码)Python数字图像处理课程平台 毕业设计242339

    Python数字图像处理课程平台的开发 摘 要 数字图像处理是一门新兴技术,随着计算机硬件的发展,数字图像的实时处理已经成为可能,由于数字图像处理的各种算法的出现,使得其处理速度越来越快,能更好的为人 ...

  8. 数字图像处理-编程实现染色体计数 C语言实现

    调用的框架:http://blog.chinaunix.net/uid-20622737-id-3173056.html 实验内容: 对于下面这幅图像,编程实现染色体计数,并附简要处理流程说明. 处理 ...

  9. 基于数字图像处理的小目标计数(一)

    波点壁纸中圆点的计数 去年年末我学习了天津科技大学杨淑莹老师的<数字图像处理>课程,虽然课程有点老,但是内容还是比较经典的.课程最后有好几个数字图像处理的案例,都是基于Visual C++ ...

最新文章

  1. AI大神李沐B站走红!连博导们都在追更,还亲自带你逐段读懂论文,网友:带B站研究生吧...
  2. datagrid单选多选
  3. 用Reflector和FileDisassembler配合反编译.net Windows程序
  4. .net core2.0 Memcached.ClientLibrary
  5. TopCoder SRM502 Div1 1000 动态规划
  6. linux中级之netfilter防火墙(firewalld)
  7. 递归删除评论php,php如何递归删除文件
  8. 推荐系统实践学习笔记(一)
  9. 类似Windows开始菜单的右键菜单
  10. python抓取豆瓣妹子图片并上传到七牛
  11. Java 创建线程的三种方式总结
  12. python-第三方接口获取验证码
  13. 手机App测试的相关测试点-简单总结
  14. MDK5中F103C8T6的RCC时钟配置时指向RCC_AHB1PeriphClockCmd但报错identifier “RCC_AHB1Periph_GPIOB is undefined的解决
  15. 新零售O2O商城系统要怎么开发,这些功能都要有
  16. 9008 能 解锁BL_手机刷入面具及twrp教程(包含解bl锁教程)
  17. qq系统软件测试计划,软件测试设计报告案例——qq空间.doc
  18. MySQL数据库改名
  19. java: -source 1.6 中不支持 diamond 运算符, (请使用 -source 7 或更高版本以启用 diamond 运算符)
  20. 网络爬虫和网络数据管理的常见问题集合

热门文章

  1. c语言编译器mingw——常用编译命令
  2. Centos8上安装mysql8遇到SELinux is preventing /usr/libexec/mysqld from write access on the directory mysql
  3. 《惢客创业日记》2021.06.15(周二)凉粉儿的防骗三板斧
  4. 猎头职场:出色的职场是会变通
  5. python统计英文文章中单词出现的次数并排序_python,_关于统计某一个 单词 在 文本中 出现的次数,python - phpStudy...
  6. c++11 chrono全面解析(最高可达纳秒级别的精度)
  7. 使用python画出近30年存款率变化图
  8. 银行家算法 C语言实现
  9. Python爬虫之Js逆向案例(8)-某乎x-zst-81之webpack
  10. Query.uniqueResult()计算数据总条数