Python数字图像处理操作及实例是什么
Admin 2022-08-11 群英技术资讯 414 次浏览
对于下面这幅图像,编程实现染色体计数,并附简要处理流程说明。
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
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:mmqy2019@163.com进行举报,并提供相关证据,查实之后,将立刻删除涉嫌侵权内容。
猜你喜欢
通过Matplotlib,开发者可以仅需要几行代码,便可以生成绘图,直方图,功率谱,条形图,错误图,散点图等,下面这篇文章主要给大家介绍了关于Python matplotlib如何简单绘制不同类型表格的相关资料,需要的朋友可以参考下
这篇文章主要介绍了Flask交互基础(GET、 POST 、PUT、 DELETE)的使用,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
这篇文章主要为大家介绍了python数据类型可变与不可变深入分析,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
这篇文章给大家分享的是有关python实现webp图片格式转化的内容。小编觉得挺实用的,因此分享给大家做个参考,下文有具体的实例,接下来一起跟随小编看看吧。
本篇文章给大家带来了关于Python的相关知识,其中主要介绍了关于实现定时任务的相关问题,可以使用第三方包来管理定时任务,相对来说apscheduler使用起来更简单,下面一起来看一下使用的方法,希望对大家有帮助。
成为群英会员,开启智能安全云计算之旅
立即注册Copyright © QY Network Company Ltd. All Rights Reserved. 2003-2020 群英 版权所有
增值电信经营许可证 : B1.B2-20140078 粤ICP备09006778号 域名注册商资质 粤 D3.1-20240008