Python實(shí)現(xiàn)數(shù)字圖像處理染色體計(jì)數(shù)示例
一、實(shí)驗(yàn)內(nèi)容
對(duì)于下面這幅圖像,編程實(shí)現(xiàn)染色體計(jì)數(shù),并附簡(jiǎn)要處理流程說(shuō)明。

二、實(shí)驗(yàn)步驟
1.中值濾波
2.圖像二值化
3.膨脹圖像
4.腐蝕圖像
5.計(jì)算光影背景
6.移除背景
7.檢測(cè)染色體
三、代碼
import cv2
import numpy as np
# 計(jì)算光影背景
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
# 按比例轉(zhuǎn)換為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未檢測(cè)到染色體")
return
else:
print("connectedComponents檢測(cè)到染色體數(shù)量為:", 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未檢測(cè)到染色體")
return
else:
print("connectedComponentsWithStats檢測(cè)到染色體數(shù)量為:", 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未檢測(cè)到染色體")
return
else:
print("findContours檢測(cè)到染色體數(shù)量為:", 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) # 二值化函數(shù)
# 第一步:中值濾波
# 中值濾波
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) # 二值化函數(shù)
# 顯示并保存圖片
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)
# 第五步:計(jì)算光影背景
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)
# 第七步:檢測(cè)輪廓
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)四、結(jié)果
1.中值濾波

2.圖像二值化

3.膨脹圖像

4.腐蝕圖像

5.計(jì)算光影背景

6.移除背景

7.檢測(cè)染色體
(1)connectedComponents.jpg

(2)connectedComponentsWithStats.jpg

(3)findContours.jpg

染色體個(gè)數(shù)為46
以上就是Python實(shí)現(xiàn)數(shù)字圖像處理染色體計(jì)數(shù)示例的詳細(xì)內(nèi)容,更多關(guān)于Python數(shù)字圖像處理染色體計(jì)數(shù)的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
python函數(shù)缺省值與引用學(xué)習(xí)筆記分享
有關(guān)一個(gè)在函數(shù)參數(shù)設(shè)置缺省值與引用的問(wèn)題,這個(gè)問(wèn)題是大多數(shù)Pythoner可能會(huì)忽視的問(wèn)題,作個(gè)筆記,以備后閱,同時(shí)供需要的朋友參考2013-02-02
Python中報(bào)錯(cuò) “TypeError: ‘list‘ object is&n
這篇文章主要介紹了Python中報(bào)錯(cuò) “TypeError: ‘list‘ object is not callable”問(wèn)題及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-09-09
django rest framework serializer返回時(shí)間自動(dòng)格式化方法
這篇文章主要介紹了django rest framework serializer返回時(shí)間自動(dòng)格式化方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-03-03
對(duì)Python中g(shù)ensim庫(kù)word2vec的使用詳解
今天小編就為大家分享一篇對(duì)Python中g(shù)ensim庫(kù)word2vec的使用詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-05-05
Python實(shí)現(xiàn)操作Redis所有類型的方法詳解
Redis作為一款高性能的NoSQL數(shù)據(jù)庫(kù),越來(lái)越受到了廣大開(kāi)發(fā)者的喜愛(ài)。本篇博客將介紹如何使用Python操作Redis的所有類型,以及一些高級(jí)用法,感興趣的可以了解一下2023-04-04
Python爬蟲(chóng)教程知識(shí)點(diǎn)總結(jié)
在本篇文章里小編給大家整理的是一篇關(guān)于Python爬蟲(chóng)教程知識(shí)點(diǎn)總結(jié),有興趣的朋友們可以學(xué)習(xí)參考下。2020-10-10
解決Tensorflow安裝成功,但在導(dǎo)入時(shí)報(bào)錯(cuò)的問(wèn)題
今天小編就為大家分享一篇解決Tensorflow安裝成功,但在導(dǎo)入時(shí)報(bào)錯(cuò)的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-06-06
Python 類中引用其他類的實(shí)現(xiàn)示例
在Python中,類的引用是通過(guò)屬性或方法與其他類實(shí)例關(guān)聯(lián),實(shí)現(xiàn)復(fù)雜邏輯,本文介紹了關(guān)聯(lián)、組合等類之間的引用方式,具有一定的參考價(jià)值,感興趣的可以了解一下2024-09-09
Python設(shè)置Word頁(yè)面紙張方向?yàn)闄M向
這篇文章主要為大家詳細(xì)介紹了Python設(shè)置Word頁(yè)面紙張方向?yàn)闄M向的相關(guān)知識(shí),文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起2024-02-02

