Python - 在计算对象时删除另一个圆内的圆

时间:2017-08-30 11:05:01

标签: python image-processing

我现在正在研究液滴发生器,并想要计算它产生的液滴数量和液滴的直径。我首先将灰度图像转换为二进制图像并从中提取特征,然后开始计算液滴。我特此附上我的图像,你可以看到它的形状是一个圆圈,里面有另一个圆圈。所以,我的程序计算为两滴。但我不希望将液滴内的圆圈算作物体。由于我是python和OpenCV的新手,我尝试了很多搜索并没有找到任何有用的东西。如果有人可以提供帮助,那将是非常好的。我正在添加原始图片。看看这对帮助是否有用。

没有喷嘴的液滴图像:

enter image description here

提取的图片:

enter image description here

代码也是:

 image_subtracted = image-image_calibration
      if showImages == 1:
         print('image_subtracted')
         print(image_subtracted)
         fig3 = plt.figure(3)
         fig3.suptitle('pure subtracted image')
         plt.imshow(image_subtracted, cmap='Greys_r', interpolation='none')
        plt.draw()
        plt.waitforbuttonpress()


#1.3To precisely show only the droplet
image_subtracted[0:line_lowerNozzleEdge][:]=0

image_subtracted=image_subtracted[line_lowerNozzleEdge+1:][:]
image_subtracted=image_subtracted.astype('uint32')

image_tmp=image_subtracted
kernel = np.ones((5,5),np.uint8)


image_tmp = ndimage.grey_erosion(image_tmp, size=(6,6));
image_tmp = ndimage.grey_dilation(image_tmp, size=(6,6))

image_subtracted=image_tmp

if showImages == 1:
  print('max(image_subtracted) = '+str(np.max(image_subtracted)))
  fig4 = plt.figure(4)
  fig4.suptitle('subtracted image')
  plt.imshow(image_subtracted, cmap='Greys_r')
  plt.draw()
  plt.waitforbuttonpress()
  plt.pause(0.5)


 #2.BINARIZE THE IMAGE


 thresh_rc = mh.thresholding.rc(image_subtracted) #Hmm!

 thresh_median = np.median(image_subtracted)

 thresh=thresh_rc

 image_binary = image_subtracted > thresh

 image_bin_int=image_binary.astype('uint8')

 if showImages == 1:
    print('mh-tresholding: '+str(thresh_rc))
    print('median tresholding: '+str(thresh_median))
    print('used tresh: '+str(thresh))
    fig6 = plt.figure(6)
    fig6.suptitle('binary image')
    fig6 = plt.figure(6)
    plt.imshow(image_binary, cmap=plt.cm.gray, interpolation='none')
    plt.draw()
    plt.waitforbuttonpress()

  #3.EXTRACT THE FEATURES
  image_tmp=image_bin_int
  image_tmp = ndimage.grey_erosion(image_tmp, size=(6,6));
  image_tmp = ndimage.grey_dilation(image_tmp, size=(10,10))
  image_extracted=image_tmp
  if showImages == 1:
     fig7 = plt.figure(7)
     plt.clf()
     fig7.suptitle('image extracted')
     plt.imshow(image_extracted, cmap=plt.cm.gray, interpolation='none')
     plt.draw()
     plt.waitforbuttonpress()



T = mh.thresholding.otsu(image_extracted.astype('uint8'))
labeled,nr_objects = mh.label(image_extracted.astype('uint8') > T)
print('number of detected objects = '+str(nr_objects))
label_array=np.array(labeled).ravel() 
label_array=np.sort(label_array)      
pixel_sum=np.zeros(nr_objects+1)

for ii in range(1,nr_objects+1,1): 
    n_tmp=np.where(label_array==ii)[0]
    pixel_sum[ii]=len(n_tmp)

ObjectArea=pixel_sum*pixelArea
#assumption of a circle:
Radius=np.sqrt(ObjectArea/np.pi)
Diameter=2*Radius
print(' ')
print('object diameters in um ='+str(Diameter/1e-6))
print(' ')
print(' ')

if showImages == 1:
    fig9 = plt.figure(9)
    plt.clf()
    plt.imshow(labeled, cmap=plt.cm.gray,  interpolation='none')
    plt.draw()
    plt.waitforbuttonpress()

return Diameter

1 个答案:

答案 0 :(得分:0)

我用c ++编写,但你可以很容易地将它移植到python。

params[:user][:name]

二进制图片:

enter image description here

在原始图像上绘制的轮廓:

enter image description here