查找多个连接对象的每个质心 [英] Finding each centroid of multiple connected objects

查看:111
本文介绍了查找多个连接对象的每个质心的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是python编码的超级新手,希望获得一些帮助。我能够对生物组织内的每个细胞轮廓进行分割(超酷!),现在我正尝试使用以下方法找到组织内每个细胞的质心:



我正在使用此代码:

  img = cv2。 imread('/ Users / kate / Desktop / SegmenterTest / SegmentedCells / Seg1.png')
image = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
thresh = cv2.threshold(image,60,255) ,cv2.THRESH_BINARY)[1]

cnts = cv2.findContours(thresh.copy(),cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)
cnts = imutils.grab_contours(cnts)

#以c为单位在轮廓
上循环:
#计算轮廓的中心
M = cv2.moments(c)
cX = int (M [ m10] / M [ m00])
cY = int(M [ m01] / M [ m00])
#绘制轮廓并C在图像上输入形状
cv2.drawContours(图像,[c],-1,(0,255,0),2)
cv2.circle(图像,(cX,cY), 7,(255,255,255),-1)
cv2.putText(image, center,(cX-20,cY-20),
cv2.FONT_HERSHEY_SIMPLEX,0.5,(255 ,255,255),2)
#显示图片
cv2.imshow(图片,图片)
cv2.waitKey(0)

但是,当我使用此代码时,它是给我整个对象的质心,而不是每个对象都给它。



我不知道从这里去哪里,所以向正确方向微移将不胜感激!

解决方案

您可以使用函数


这是我使用的代码。

  import cv2 
进口matplotlib.pyplot as plt
从skimage进口量度
进口numpy as np

单元格= cv2.imread('cells.png',0)

ret,thresh = cv2.threshold(cells,20,255,cv2.THRESH_BINARY_INV)


标签= measure.label(阈值,背景= 0)
bg_label =标签[0,0]
标签[labels == bg_label] = 0#将背景标签分配给0

props = measure.regionprops(labels)

图,ax = plt.subplots(1,1)
plt.axis('off')
ax。 imshow(cells,cmap ='gray')
质心= np.zeros(shape =(len(np.unique(labels)),2))#访问i,prop质心
的坐标枚举(props):
my_centroid = prop.centroid
centroids [i,:] = my_centroid
ax.plot(my_centroid [1] ,my_centroid [0],'r。')

#print(centroids)
#fig.savefig('out.png',bbox_inches ='tight',pad_inches = 0)
plt.show()

祝您研究顺利!


I am SUPER new to python coding and would like some help. I was able to segment each cell outline within a biological tissue (super cool!) and now I am trying to find the centroid of each cell within a tissue using this:

I am using this code:

img = cv2.imread('/Users/kate/Desktop/SegmenterTest/SegmentedCells/Seg1.png')
image = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
thresh = cv2.threshold(image, 60, 255, cv2.THRESH_BINARY)[1]

cnts = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cnts = imutils.grab_contours(cnts)

# loop over the contours
for c in cnts:
    # compute the center of the contour
    M = cv2.moments(c)
    cX = int(M["m10"] / M["m00"])
    cY = int(M["m01"] / M["m00"])
    # draw the contour and center of the shape on the image
    cv2.drawContours(image, [c], -1, (0, 255, 0), 2)
    cv2.circle(image, (cX, cY), 7, (255, 255, 255), -1)
    cv2.putText(image, "center", (cX - 20, cY - 20),
        cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 255, 255), 2)
    # show the image
    cv2.imshow("Image", image)
    cv2.waitKey(0)

However, when I use this code, it is giving me the centroid of the ENTIRE object, and not each individual object to give this.

I have no idea where to go from here, so a nudge in the right direction would be greatly appreciated!

解决方案

You can use the function regionprops from the module scikit-image in your case. Here is what I got.

This is the code I used.

import cv2
import matplotlib.pyplot as plt
from skimage import measure
import numpy as np

cells = cv2.imread('cells.png',0)

ret,thresh = cv2.threshold(cells,20,255,cv2.THRESH_BINARY_INV)


labels= measure.label(thresh, background=0)
bg_label = labels[0,0] 
labels[labels==bg_label] = 0 # Assign background label to 0

props = measure.regionprops(labels)

fig,ax = plt.subplots(1,1)
plt.axis('off')
ax.imshow(cells,cmap='gray')
centroids = np.zeros(shape=(len(np.unique(labels)),2)) # Access the coordinates of centroids
for i,prop in enumerate(props):
    my_centroid = prop.centroid
    centroids[i,:]= my_centroid
    ax.plot(my_centroid[1],my_centroid[0],'r.')

# print(centroids)
# fig.savefig('out.png', bbox_inches='tight', pad_inches=0)
plt.show()

Good luck with your research!

这篇关于查找多个连接对象的每个质心的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆