簡體   English   中英

使用 Python 中的 OpenCV 輪廓分析熒光細胞圖像

[英]Using OpenCV Contours in Python to Analyze fluorescent cell image

我正在嘗試使用 OpenCV 中的輪廓來計數熒光細胞並計算總熒光面積。 在探索了 Scikit Image 中的選項並嘗試了 blob 檢測之后,這似乎是我的圖像類型最簡單的方法。 不幸的是,我似乎無法在細胞周圍繪制輪廓。 知道我做錯了什么嗎?

import cv2
import numpy as np

#import image
image = cv2.imread('Microcystis1.png')
cv2.imshow('image',image)
cv2.waitKey(0)

#Convert image to Grayscale
gray = cv2.cvtColor(image,cv2.COLOR_BGR2GRAY)
cv2.imshow('grayscale',gray)
cv2.waitKey(0)

#Threshold Binary
ret,thresh1 = cv2.threshold(gray,45,255,cv2.THRESH_BINARY)
cv2.imshow('binary',thresh1)
cv2.waitKey(0)

#Detect contours
contours, hierarchy = cv2.findContours(thresh1,cv2.RETR_TREE,cv2.CHAIN_APPROX_NONE)

#Draw contours
img = cv2.drawContours(thresh1, contours, -1, (0,255,0), 3)
cv2.imshow('contours',img)
cv2.waitKey(0)

#Analyze and Report Contours
cnt = contours[0]
area = cv2.contourArea(cnt)
print("Total Area: ", area)
print("Cell Count: ", len(cnt))

以下是圖像的處理方式...

在此處輸入圖像描述

還有我的 output:

Total Area:  16.0
Cell Count:  14

你不需要得到輪廓。 您可以在 Python/OpenCV 中簡單地計算閾值圖像中非零像素(白色像素)的數量。

area = cv2.countNonZero(thresh)

https://docs.opencv.org/4.1.1/d2/de8/group__core__array.html#gaa4b89393263bb4d604e0fe5986723914

您正在灰度圖像上繪制輪廓。 如果您仔細放大,您會看到它們正在被繪制。 要完全查看它們,您可以使用兩個選項:

  1. 如果您只想按照問題所示在thresh1上繪制它們,則將顏色從(0, 255, 0)更改為 (255, 255, 255)。 這將確保輪廓為白色。
  2. 如果您希望它們具有其他顏色,例如使用(0, 255, 0)獲得的綠色,那么您可以創建一個 NumPy 零數組並在其上繪制輪廓。
h, w = thresh1.shape
img = np.zeros((w, h), dtype=np.uint8)
cv2.drawContours(img, contours, -1, (0,255,0), 3)

現在顯示img ,它應該有綠色輪廓。

為了找到輪廓的總數及其各自的區域,您需要獲取輪廓的平面列表,而不是分層列表: https://docs.opencv.org/4.3.0/d3/dc0/group__imgproc__shape.html# ga819779b9857cc2f8601e6526a3a5bc71

cv2.RETR_TREE 告訴算法將事物組合在一起。

cv2.RETR_LIST 告訴 aglo 給你一個包含所有單個點的平面列表。

我相信這就是為什么您看到 14 個單元的總面積為 16.0。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM