简体   繁体   English

使用 Python 中的 OpenCV 轮廓分析荧光细胞图像

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

I am trying to use Contours in OpenCV to count fluorescent cells and calculate the total fluorescent area.我正在尝试使用 OpenCV 中的轮廓来计数荧光细胞并计算总荧光面积。 After exploring options in Scikit Image and after trying blob detection, this seemed the simplest method for my image type.在探索了 Scikit Image 中的选项并尝试了 blob 检测之后,这似乎是我的图像类型最简单的方法。 Unfortunately, I cannot seem to get the contours to draw around the cells.不幸的是,我似乎无法在细胞周围绘制轮廓。 Any idea what I'm doing wrong?知道我做错了什么吗?

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))

Here is how the images are being processed...以下是图像的处理方式...

在此处输入图像描述

And my output:还有我的 output:

Total Area:  16.0
Cell Count:  14

You do not need to get contours.你不需要得到轮廓。 You can simply count the number of non-zero pixels (white pixels) in your thresholded image in Python/OpenCV.您可以在 Python/OpenCV 中简单地计算阈值图像中非零像素(白色像素)的数量。 See

area = cv2.countNonZero(thresh)

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

You are drawing contours on a grayscale image.您正在灰度图像上绘制轮廓。 If you zoom closely you will see that they are being drawn.如果您仔细放大,您会看到它们正在被绘制。 To view them completely you can two options:要完全查看它们,您可以使用两个选项:

  1. If you want to draw them on thresh1 only as shown in the question, then change the color from (0, 255, 0) to (255, 255, 255).如果您只想按照问题所示在thresh1上绘制它们,则将颜色从(0, 255, 0)更改为 (255, 255, 255)。 This will ensure contours in white color.这将确保轮廓为白色。
  2. If you want them in some other color like green as you would have obtained using (0, 255, 0) then you can create a NumPy array of zeros and draw the contours on it.如果您希望它们具有其他颜色,例如使用(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)

Now display img and it should have green contours.现在显示img ,它应该有绿色轮廓。

In order to find the total number of contours and their respective area you need to get a flat list of contours, not a hierarchical one: https://docs.opencv.org/4.3.0/d3/dc0/group__imgproc__shape.html#ga819779b9857cc2f8601e6526a3a5bc71为了找到轮廓的总数及其各自的区域,您需要获取轮廓的平面列表,而不是分层列表: https://docs.opencv.org/4.3.0/d3/dc0/group__imgproc__shape.html# ga819779b9857cc2f8601e6526a3a5bc71

cv2.RETR_TREE tells the algo to group things together. cv2.RETR_TREE 告诉算法将事物组合在一起。

cv2.RETR_LIST tells the aglo to give you a flat list of all the individual points. cv2.RETR_LIST 告诉 aglo 给你一个包含所有单个点的平面列表。

I believe that's why you're seeing the total area of 16.0 for 14 cells.我相信这就是为什么您看到 14 个单元的总面积为 16.0。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM