我正在努力检测显微镜图像中的细胞,如下图所示。由于显微镜载玻片上的瑕疵,通常会绘制出虚假的轮廓,例如下图中图例下方的轮廓。
我目前正在使用this solution进行清理。这是基本思想。
# Create image of background
blank = np.zeros(image.shape[0:2])
background_image = cv2.drawContours(blank.copy(), background_contour, 0, 1, -1)
for i, c in enumerate(contours):
# Create image of contour
contour_image = cv2.drawContours(blank.copy(), contours, i, 1, -1)
# Create image of focal contour + background
total_image = np.where(background_image+contour_image>0, 1, 0)
# Check if contour is outside postive space
if total_image.sum() > background_image.sum():
continue
这按预期工作;如果total_image
的面积大于background_image
的面积,则c
必须在目标区域之外。但是绘制所有这些轮廓非常慢,并且检查数千个轮廓需要花费数小时。 是否有更有效的方法来检查轮廓是否重叠,而无需绘制轮廓?
我假设目标是从进一步分析中排除外部轮廓?如果是这样,最简单的方法是使用红色背景轮廓作为遮罩。然后使用蒙版图像检测蓝色单元格。
# Create image of background
blank = np.zeros(image.shape[0:2], dtype=np.uint8)
background_image = cv2.drawContours(blank.copy(), background_contour, 0, (255), -1)
# mask input image (leaves only the area inside the red background contour)
res = cv2.bitwise_and(image,image,mask=background_image )
#[detect blue cells]
假设您尝试在重叠的不同轮廓上找到点
将轮廓视为
vector<vector<Point> > contours;
..... //obtain you contrours.
vector<Point> non_repeating_points;
for(int i=0;i<contours.size();i++)
{
for(int j=0;j<contours[i].size();j++)
{
Point this_point= countour[i][j];
for(int k=0;k<non_repeating_points.size();k++)
{//check this list for previous record
if(non_repeating_points[k] == this_point)
{
std::cout<< "found repeat points at "<< std::endl;
std::cout<< this_point << std::endl;
break;
}
}
//if not seen before just add it in the list
non_repeating_points.push_back(this_point);
}
}
我只是编写而没有编译。但我认为您可以理解这个想法。
您提供的信息还不够。如果您想找到最近的连接边界。而且没有重叠。
您可以在点non_repeating_points [k]附近声明一个本地集群。称它为round_non_repeating_points [k]您可以控制可以视为截距的距离,并将所有这些距离都推入此Surround_non_repeating_points [k];
然后只需在循环中检查if(surround_non_repeating_points [k] == this_point)