我有一个功能,可以从相机中获取图像并处理图像,最后计算图像中检测到的物体数量以及直径。如果对象的数量是1,我想检查函数外部,并相应地编程来改变产生液滴(对象)的信号的值。
def DropletRecognition(image_orig,image_calibration,Mean_section_brightness,line_lowerNozzleEdge):
showImages=1
#To maintain the brightness
mean_brightness=np.mean(np.mean(image_orig[0:line_lowerNozzleEdge][:]))
factor=Mean_section_brightness/mean_brightness
image_orig = misc.imread(image_name)
image=image_orig*factor
image=image_orig
if showImages == 1:
print('measured image')
print(image_orig)
print('image_calibration')
print(image_calibration)
fig1 = plt.figure(1)
fig1.suptitle(image_name)
plt.imshow(image, cmap='gray')
plt.draw()
plt.waitforbuttonpress()
#To subtract the calibration image from the measured image
image_subtracted = image-image_calibration
#Calculating the lower nozzle edge and doing further resizing
image_subtracted[0:line_lowerNozzleEdge][:]=0
#To delete the nozzle lines
image_subtracted=image_subtracted[line_lowerNozzleEdge+10:][:]
image_subtracted=image_subtracted.astype('uint32')
#Erosion and Dilation to condition the image
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
#To convert the image to Binary
thresh_rc = mh.thresholding.rc(image_subtracted)
thresh=thresh_rc
image_binary = image_subtracted > thresh
image_bin_int=image_binary.astype('uint8')
#To extract the features from the Binary Image
image_tmp=image_bin_int
im_floodfill = image_tmp.copy()
h, w = image_tmp.shape[:2]
mask = np.zeros((h+2, w+2), np.uint8)
cv2.floodFill(im_floodfill, mask, (0,0), 255);
im_floodfill_inv = cv2.bitwise_not(im_floodfill)
im_out = image_tmp | im_floodfill_inv
image_extracted=im_out
#To count the number of objects found in the image
T = mh.thresholding.otsu(image_extracted.astype('uint8'))
labeled,nr_objects = mh.label(image_extracted.astype('uint8') > T)
print(nr_objects)
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)
#To calculate the Object Diameter using pixel sum and area
ObjectArea=pixel_sum*pixelArea
#Assuming that the object is a circle
Radius=np.sqrt(ObjectArea/np.pi)
Diameter=2*Radius
print(' ')
print('object diameters in um =',Diameter/1e-8)
print(' ')
print(' ')
if showImages == 1:
fig2 = plt.figure(2)
plt.clf()
plt.imshow(labeled)
plt.draw()
plt.waitforbuttonpress()
return nr_objects
这是函数,我必须在函数之外检查
if nr_objects == 1:
break
elif nr_objects > 1:
voltage +=80
else:
voltage-=80
但这不起作用。该程序根本不检查这些循环。我也经常检查过 如果nr_objects == 1: 打印('是') 即便这样也行不通。所以我可以看到该参数未被访问。谁能指导我怎么做?
提前致谢!
答案 0 :(得分:1)
您需要测试的变量不一定是nr_objects
。这是函数调用左边出现的任何内容。即
test_me = DropletRecognition(... your arguments here)
if test_me == 1:
break
elif test_me > 1:
voltage +=80
else:
voltage-=80