猿问

使用 Python 图像处理分割生物样本的照片以提取圆形感兴趣区域

我在完成一些生物样本照片的分割时遇到了问题,我正在尝试通过图像处理来分析细菌的生长,因为理论上它应该可以工作。这是我拥有的原始图像之一:

我试图分割圆圈内的区域,看看像素的值如何随着时间的推移而变化。我一直在尝试很多技术,因为我对分析这类样本还比较陌生。最初我使用的是 opencv,但没有得到我想要的结果,所以现在我对所有人都使用 scikit-image图像处理和分割技术。这是我到目前为止的代码:


from skimage import morphology, exposure, io, filters

from scipy import ndimage as ndi

from skimage.color import rgb2gray, label2rgb

from skimage.filters import sobel, rank

import matplotlib.pyplot as plt

y1=400

y2=1600

x1=700

x2=1900

test_img = io.imread(folders_path+hour_tested[0]+'5.jpg')

roi_test = test_img[y1:y2, x1:x2,:]

gray_img = rgb2gray(roi_test)

denoised_img = rank.median(gray_img, morphology.disk(5))

val = filters.threshold_otsu(denoised_img)

mask = denoised_img > val

elevation_map=sobel(denoised_img)

segmentation = morphology.watershed(elevation_map, mask=mask)

labeled_bio, num_seg = ndi.label(segmentation)

image_label_overlay = label2rgb(labeled_bio, image=gray_img)

plt.imshow(image_label_overlay)

plt.show()

在最后一行,我用不同颜色分割样本区域并在一个标签中获得我想要分析的部分,现在我不知道如何继续或至少如何只看到该标签然后创建一个面具。


我还分享了标记的图像供任何人查看,并可能在接下来的步骤中帮助我,我觉得或者我真的很接近分割我感兴趣的区域,或者真的很远很困惑。

好吧,这是样本的标记图像:


http://img3.mukewang.com/62c4256d0001daca04760434.jpg

杨__羊羊
浏览 188回答 2
2回答

一只萌萌小番薯

修复代码后,这是生物膜分割的正确答案:import cv2import numpy as npimport osdef resize_image(image, percentage):&nbsp; &nbsp;scale_percent=percentage&nbsp; &nbsp;width = int(image.shape[1] * scale_percent/100)&nbsp; &nbsp;height= int(image.shape[0] * scale_percent/100)&nbsp; &nbsp;dimensions = (width, height)&nbsp; &nbsp;resized = cv2.resize(image, dimensions, interpolation = cv2.INTER_AREA)&nbsp; &nbsp;return resized#this path is changed for each image in the DBpath=folders_path+hour_tested[0]+'1.jpg'image = cv2.imread(path)s_image = resize_image(image,50)original = s_image.copy()mask = np.zeros(s_image.shape, dtype=np.uint8)gray = cv2.cvtColor(s_image, cv2.COLOR_BGR2GRAY)thresh = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)[1]# Morph closekernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (7,7))close = cv2.morphologyEx(thresh, cv2.MORPH_CLOSE, kernel, iterations=3)# Find contours and filter using contour area + contour approximation# Determine perfect circle contour then draw onto blank maskim,cnts,hierarchy = cv2.findContours(close, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)for c in cnts:&nbsp; &nbsp; peri = cv2.arcLength(c, True)&nbsp; &nbsp; approx = cv2.approxPolyDP(c, 0.04*peri, True)&nbsp; &nbsp; area = cv2.contourArea(c)&nbsp; &nbsp; if len(approx) > 4 and (area > 8000 and area < 250000) and (peri<2000 and peri>1000):&nbsp; &nbsp; &nbsp; &nbsp; ((x, y), r) = cv2.minEnclosingCircle(c)&nbsp; &nbsp; &nbsp; &nbsp; x,y,r = int(x),int(y),int(r)&nbsp; &nbsp; &nbsp; &nbsp; blank_circle=cv2.circle(mask, (x, y), r, (255, 255, 255), -1)&nbsp; &nbsp; &nbsp; &nbsp; filled_circle=cv2.circle(s_image, (x, y), r, (36, 255, 12), 3)&nbsp; &nbsp; &nbsp; &nbsp; # Extract ROI&nbsp; &nbsp; &nbsp; &nbsp; mask_ROI = blank_circle[y-r:y+r, x-r:x+r]&nbsp; &nbsp; &nbsp; &nbsp; mask_ROI = cv2.cvtColor(mask_ROI, cv2.COLOR_BGR2GRAY)&nbsp; &nbsp; &nbsp; &nbsp; image_ROI = filled_circle[y-r:y+r, x-r:x+r]&nbsp; &nbsp; &nbsp; &nbsp; result = cv2.bitwise_and(image_ROI, image_ROI, mask=mask_ROI)&nbsp; &nbsp; &nbsp; &nbsp; cv2.imwrite('result.png',result)

幕布斯6054654

这是一种使用简单图像处理技术的方法获取二值图像。加载图像,转换为灰度,然后Otsu的阈值得到二值图像执行形态学操作。我们创建一个椭圆形的内核,然后执行变形来填充轮廓隔离感兴趣的区域。我们使用轮廓近似+轮廓区域找到轮廓和过滤器。一旦我们隔离了轮廓,找到一个最小的封闭圆以获得一个完美的圆,然后将它绘制到一个空白蒙版上。获得正圆的想法是从如何修改蒙版来制作正圆隔离投资回报率。我们在掩码上找到边界矩形 ROI,然后使用 Numpy 切片进行裁剪按位与获得结果。最后我们按位和提取的两个 ROI这是每个步骤的可视化:输入图像二进制图像变形关闭以绿色突出显示的孤立感兴趣区域,并在空白蒙版上绘制填充轮廓&nbsp;孤立的 ROI&nbsp;按位与结果(两个版本,一个黑色背景,一个白色背景,取决于你想要的)&nbsp;代码import cv2import numpy as np# Load image, create blank mask, grayscale, Otsu's thresholdimage = cv2.imread('1.jpg')original = image.copy()mask = np.zeros(image.shape, dtype=np.uint8)gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)thresh = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)[1]# Morph closekernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (5,5))close = cv2.morphologyEx(thresh, cv2.MORPH_CLOSE, kernel, iterations=5)# Find contours and filter using contour area + contour approximation# Determine perfect circle contour then draw onto blank maskcnts = cv2.findContours(close, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)cnts = cnts[0] if len(cnts) == 2 else cnts[1]for c in cnts:&nbsp; &nbsp; peri = cv2.arcLength(c, True)&nbsp; &nbsp; approx = cv2.approxPolyDP(c, 0.04 * peri, True)&nbsp; &nbsp; area = cv2.contourArea(c)&nbsp; &nbsp; if len(approx) > 4 and area > 10000 and area < 500000:&nbsp; &nbsp; &nbsp; &nbsp; ((x, y), r) = cv2.minEnclosingCircle(c)&nbsp; &nbsp; &nbsp; &nbsp; cv2.circle(mask, (int(x), int(y)), int(r), (255, 255, 255), -1)&nbsp; &nbsp; &nbsp; &nbsp; cv2.circle(image, (int(x), int(y)), int(r), (36, 255, 12), 3)# Extract ROImask = cv2.cvtColor(mask, cv2.COLOR_BGR2GRAY)x,y,w,h = cv2.boundingRect(mask)mask_ROI = mask[y:y+h, x:x+w]image_ROI = original[y:y+h, x:x+w]# Bitwise-and for resultresult = cv2.bitwise_and(image_ROI, image_ROI, mask=mask_ROI)result[mask_ROI==0] = (255,255,255) # Color background whitecv2.imwrite('close.png', close)cv2.imwrite('thresh.png', thresh)cv2.imwrite('image.png', image)cv2.imwrite('mask.png', mask)cv2.imwrite('result.png', result)cv2.waitKey()注意:确定感兴趣的圆形区域的另一种方法是使用已经实现的霍夫圆变换,cv2.HoughCircles()但参数很多,因此它可能不是最实用的方法。
随时随地看视频慕课网APP

相关分类

Python
我要回答