繁体   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