網站首頁 編程語言 正文
一、實驗內容?
對于下面這幅圖像,編程實現染色體計數,并附簡要處理流程說明。
二、實驗步驟
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
相關推薦
- 2022-12-05 TensorFlow中關于tf.app.flags命令行參數解析模塊_python
- 2023-01-09 python自動化測試中裝飾器@ddt與@data源碼深入解析_python
- 2022-04-24 基于Python制作一個文件去重小工具_python
- 2023-12-07 redis key
- 2022-12-26 flouting?ui定位組件完美替代ant?deisgn使用詳解_React
- 2023-03-27 Android?Framework原理Binder驅動源碼解析_Android
- 2022-09-15 Python淺析生成器generator的使用_python
- 2022-02-17 解決 Uncaught SyntaxError: Unexpected token ‘<‘ 錯誤解決
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支