我试图计数树突状棘(小突起)在通过荧光显微镜获得的小鼠树突,使用Python和OpenCV。
这是最初的图像,我从这里开始:
原始图片:

经过一些预处理(下面的代码),我得到了以下轮廓:
带有轮廓的原始图片(白色):

我需要做的是识别所有突起,得到这样的东西:
带有白色轮廓和红色预期计数的原始图片:

在对图像进行预处理(二值化、阈值化和降低噪声)之后,我打算做的是绘制轮廓并试图在其中找出凸起的缺陷。问题的产生是因为一些“刺”(这些凸起的技术名称)没有被承认,因为它们在相同的凸性缺陷中一起膨胀,低估了结果。在标记凸性缺陷时,有没有更精确的方法?
原始图像,轮廓标记为白色。用我的代码识别的红色点标记刺。绿点标记刺,我仍然认不出:

我的Python代码:
import cv2
import numpy as np
from matplotlib import pyplot as plt
#Image loading and preprocessing:
img = cv2.imread('Prueba.jpg')
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
img = cv2.pyrMeanShiftFiltering(img,5,11)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
ret,thresh1 = cv2.threshold(gray,5,255,0)
kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE,(5,5))
img1 = cv2.morphologyEx(thresh1, cv2.MORPH_OPEN, kernel)
img1 = cv2.morphologyEx(img1, cv2.MORPH_OPEN, kernel)
img1 = cv2.dilate(img1,kernel,iterations = 5)
#Drawing of contours. Some spines were dettached of the main shaft due to
#image bad quality. The main idea of the code below is to identify the shaft
#as the biggest contour, and count any smaller as a spine too.
_, contours,_ = cv2.findContours(img1,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
print("Number of contours detected: "+str(len(contours)))
cv2.drawContours(img,contours,-1,(255,255,255),6)
plt.imshow(img)
plt.show()
lengths = [len(i) for i in contours]
cnt = lengths.index(max(lengths))
#The contour of the main shaft is stored in cnt
cnt = contours.pop(cnt)
#Finding convexity points with hull:
hull = cv2.convexHull(cnt)
#The next lines are just for visualization. All centroids of smaller contours
#are marked as spines.
for i in contours:
M = cv2.moments(i)
centroid_x = int(M['m10']/M['m00'])
centroid_y = int(M['m01']/M['m00'])
centroid = np.array([[[centroid_x, centroid_y]]])
print(centroid)
cv2.drawContours(img,centroid,-1,(0,255,0),25)
cv2.drawContours(img,centroid,-1,(255,0,0),10)
cv2.drawContours(img,hull,-1,(0,255,0),25)
cv2.drawContours(img,hull,-1,(255,0,0),10)
plt.imshow(img)
plt.show()
#Finally, the number of spines is computed as the sum between smaller contours
#and protuberances in the main shaft.
spines = len(contours)+len(hull)
print("Number of identified spines: " + str(spines))我知道我的代码还有许多较小的问题要解决,但我认为最大的问题就是这里介绍的问题。
谢谢你的帮忙!并且有一个好的
发布于 2018-01-24 21:59:16
我会把轮廓近似成一个多边形,就像消音器建议的那样(不要使用凸包)。也许你应该简化轮廓,保持形状的大部分细节。
这样,你就有了很多需要过滤的顶点:看看每个顶点的角度,你可以知道它是凹的还是凸的。每个脊柱是凹点之间的一个或多个凸点(如果你有几个连续的凸顶点,你只保留一个更尖的顶点)。
编辑:为了计算角度,可以这样做:假设a、b和c是连续的三个顶点
angle1 = arctan((by-ay)/(bx-ax))
angle2 = arctan((cy-by)/(cx-bx))
angleDiff=angle2-angle1
if(angleDiff<-PI) angleDiff=angleDiff+2PI
if(angleDiff>0) concave
Else convex反之亦然,取决于你的轮廓是顺时针方向还是逆时针方向,黑色还是白色。如果你把任意多边形的所有angleDiff加起来,结果应该是2PI。如果是-2PI,那么最后一个" If“应该换掉。
https://stackoverflow.com/questions/48411742
复制相似问题