在扫描图像中检测疟疾细胞
Detecting Malaria Cell in scan image
我使用疟疾扫描图像来分类图像是否患有疟疾。数据集是从kaggle下载的
我达到了 96% 以上的准确率。
现在,我想知道如何检测扫描图像中的细胞。我需要指出图像中的疟疾细胞或画出疟疾细胞的轮廓。
包含疟疾细胞的样本图像
这个问题如何实现检测?
如果我假设您想找到图像中的深紫色,那么这是使用 Python/OpenCV/Numpy/Sklearn 的一种方法。
- 读取不带 alpha 通道的输入图像以移除文本参考线
- 使用 3 种颜色进行 kmeans 颜色分割(至 return:黑色、浅紫色、深紫色)。我使用 Sklearn,因为它对我来说更简单。但是你也可以用 OpenCV 来做。
- 对深紫色进行彩色图像阈值处理
- (如果需要添加一些形态,虽然我没有在这里使用它)
- 获取所有轮廓和最大轮廓(分别)
- 保存生成的图像
输入:
import cv2
import numpy as np
from sklearn import cluster
# read image
image = cv2.imread("purple_cell.png")
h, w, c = image.shape
# convert image to float in range 0-1 for sklearn kmeans
img = image.astype(np.float64)/255.0
# reshape image to 1D
image_1d = img.reshape(h*w, c)
# compute kmeans for 3 colors
kmeans_cluster = cluster.KMeans(n_clusters=3)
kmeans_cluster.fit(image_1d)
cluster_centers = kmeans_cluster.cluster_centers_
cluster_labels = kmeans_cluster.labels_
# need to scale back to range 0-255
newimage = (255*cluster_centers[cluster_labels].reshape(h, w, c)).clip(0,255).astype(np.uint8)
# Set BGR color ranges
lowerBound = np.array([170,90,120]);
upperBound = np.array([195,110,140]);
# Compute mask (roi) from ranges in dst
thresh = cv2.inRange(newimage, lowerBound, upperBound);
# get largest contour and all contours
contours = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
contours = contours[0] if len(contours) == 2 else contours[1]
area_thresh = 0
result1 = image.copy()
for c in contours:
cv2.drawContours(result1, [c], -1, (0, 255, 0), 1)
area = cv2.contourArea(c)
if area > area_thresh:
area_thresh=area
big_contour = c
# draw largest contour only
result2 = image.copy()
cv2.drawContours(result2, [big_contour], -1, (0, 255, 0), 1)
cv2.imshow('image', image)
cv2.imshow('newimage', newimage)
cv2.imshow('thresh', thresh)
cv2.imshow('result1', result1)
cv2.imshow('result2', result2)
cv2.waitKey()
cv2.imwrite('purple_cell_kmeans_3.png', newimage)
cv2.imwrite('purple_cell_thresh.png', thresh)
cv2.imwrite('purple_cell_extracted1.png', result1)
cv2.imwrite('purple_cell_extracted2.png', result2)
Kmeans 图片:
阈值图像:
所有轮廓图像:
最大的轮廓图:
我使用疟疾扫描图像来分类图像是否患有疟疾。数据集是从kaggle下载的
我达到了 96% 以上的准确率。
现在,我想知道如何检测扫描图像中的细胞。我需要指出图像中的疟疾细胞或画出疟疾细胞的轮廓。
包含疟疾细胞的样本图像
这个问题如何实现检测?
如果我假设您想找到图像中的深紫色,那么这是使用 Python/OpenCV/Numpy/Sklearn 的一种方法。
- 读取不带 alpha 通道的输入图像以移除文本参考线
- 使用 3 种颜色进行 kmeans 颜色分割(至 return:黑色、浅紫色、深紫色)。我使用 Sklearn,因为它对我来说更简单。但是你也可以用 OpenCV 来做。
- 对深紫色进行彩色图像阈值处理
- (如果需要添加一些形态,虽然我没有在这里使用它)
- 获取所有轮廓和最大轮廓(分别)
- 保存生成的图像
输入:
import cv2
import numpy as np
from sklearn import cluster
# read image
image = cv2.imread("purple_cell.png")
h, w, c = image.shape
# convert image to float in range 0-1 for sklearn kmeans
img = image.astype(np.float64)/255.0
# reshape image to 1D
image_1d = img.reshape(h*w, c)
# compute kmeans for 3 colors
kmeans_cluster = cluster.KMeans(n_clusters=3)
kmeans_cluster.fit(image_1d)
cluster_centers = kmeans_cluster.cluster_centers_
cluster_labels = kmeans_cluster.labels_
# need to scale back to range 0-255
newimage = (255*cluster_centers[cluster_labels].reshape(h, w, c)).clip(0,255).astype(np.uint8)
# Set BGR color ranges
lowerBound = np.array([170,90,120]);
upperBound = np.array([195,110,140]);
# Compute mask (roi) from ranges in dst
thresh = cv2.inRange(newimage, lowerBound, upperBound);
# get largest contour and all contours
contours = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
contours = contours[0] if len(contours) == 2 else contours[1]
area_thresh = 0
result1 = image.copy()
for c in contours:
cv2.drawContours(result1, [c], -1, (0, 255, 0), 1)
area = cv2.contourArea(c)
if area > area_thresh:
area_thresh=area
big_contour = c
# draw largest contour only
result2 = image.copy()
cv2.drawContours(result2, [big_contour], -1, (0, 255, 0), 1)
cv2.imshow('image', image)
cv2.imshow('newimage', newimage)
cv2.imshow('thresh', thresh)
cv2.imshow('result1', result1)
cv2.imshow('result2', result2)
cv2.waitKey()
cv2.imwrite('purple_cell_kmeans_3.png', newimage)
cv2.imwrite('purple_cell_thresh.png', thresh)
cv2.imwrite('purple_cell_extracted1.png', result1)
cv2.imwrite('purple_cell_extracted2.png', result2)
Kmeans 图片:
阈值图像:
所有轮廓图像:
最大的轮廓图: