Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/opencv/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/apache-spark/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python ValueError:从零大小数组到最大缩减操作_Python_Opencv_Image Processing - Fatal编程技术网

Python ValueError:从零大小数组到最大缩减操作

Python ValueError:从零大小数组到最大缩减操作,python,opencv,image-processing,Python,Opencv,Image Processing,我不熟悉Python和OpenCV。但我已经成功地编写了一段代码,它在开始时运行良好。但现在同样的代码不起作用了。任务是,必须从带有液滴的摄像头中捕获图像,并从校准图像中减去,然后必须从图像中提取液滴,以计算有多少液滴,然后计算相应的直径。代码可能有点大,有点傻,因为我是新来的,想仔细地完成所有的步骤。我附上下面的代码和错误。请看一下,我希望很快能找到帮助 import numpy as np import sys import os import cv2 import scipy impor

我不熟悉Python和OpenCV。但我已经成功地编写了一段代码,它在开始时运行良好。但现在同样的代码不起作用了。任务是,必须从带有液滴的摄像头中捕获图像,并从校准图像中减去,然后必须从图像中提取液滴,以计算有多少液滴,然后计算相应的直径。代码可能有点大,有点傻,因为我是新来的,想仔细地完成所有的步骤。我附上下面的代码和错误。请看一下,我希望很快能找到帮助

import numpy as np
import sys
import os
import cv2
import scipy

import TIS #For the Camera

from scipy import misc
from scipy import ndimage

import matplotlib.pyplot as plt

from skimage.filters import threshold_otsu
from skimage import measure
from skimage import feature
from skimage import color
from skimage import io


import mahotas as mh

#Droplet Recognition
def DropletRecognition(image_orig,gray_image, line_lowerNozzleEdge):
showImages = 1
image_orig = misc.imread(image_name)
image = image_orig
if showImages == 1:
    print('Measured Image:')
    print(image_orig)
    print('Calibration Image:')
    print(gray_image)
    fig2 = plt.figure(2)
    fig2.suptitle(image_name+'image')
    plt.imshow(image_orig, cmap='gray')
    plt.draw()
    plt.waitforbuttonpress()
cv2.imwrite('/home/Captured_Image.jpg',image_orig) #Saving the Calibration Image
cv2.waitKey(1000)
image_2 = cv2.imread('Captured_Image.jpg')
gray_image_2 = cv2.cvtColor(image_2, cv2.COLOR_BGR2GRAY)

#Subtracting the Image:
image_subtracted = image-image_calibration
if showImages == 1:
    print('Subtracted Image:')
    print(image_subtracted)
    fig3 = plt.figure(3)
    fig3.suptitle('Subtracted Image')
    plt.imshow(image_subtracted, cmap='gray')
    plt.draw()
    plt.waitforbuttonpress()

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))
if showImages == 1:
    print('max(image_subtracted) = '+str(np.max(image_subtracted))) 




image_subtracted=image_tmp

thresh_rc = mh.thresholding.rc(image_subtracted) 

thresh_median = np.median(image_subtracted)

thresh=thresh_rc

image_binary = image_subtracted > thresh

image_bin_int=image_binary.astype('uint8')

image_tmp=image_bin_int
im_floodfill = image_tmp.copy()
a, b = image_tmp.shape[:2]
mask = np.zeros((a+2, b+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  
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
Radius=np.sqrt(ObjectArea/np.pi)
Diameter=2*Radius
print(' ')
print('object diameters in um ='+str(Diameter/1e-8))
print(' ')
print(' ')
if showImages == 1:
    fig4 = plt.figure(4)
    plt.clf()
    plt.imshow(labeled, interpolation='none')
    plt.draw()
    plt.waitforbuttonpress()

return Diameter
return nr_objects

 #Defining the Basic Parameters

#number of calibration images (typ. '1'):
N_EichBilderDuese=1    

#capture calibration image ('1'=yes):
captureCalibrationImage=0

#How many lines used for brightness adaption of measured images, required        for the calibration
#image subtraction:
AusschnittCal_y=230

#Calculate the scale:
rightNozzleEdge=1140
leftNozzleEdge=160
pixelSize=1e-3/(rightNozzleEdge-leftNozzleEdge)
pixelArea=pixelSize**2

#Camera Settings
data = np.loadtxt('KameraEinstellungen.dat',dtype='uint16',skiprows=1)                    
Tis = TIS.TIS(data[0],data[1],data[2],data[3],data[4])                          


Tis.Create_pipeline() #Creating a pipeline to start the camera
cv2.waitKey(500)

Tis.Start_pipeline()
cv2.waitKey(500)

cv2.namedWindow('Window', cv2.cv.CV_WINDOW_NORMAL) 
cv2.waitKey(1000)

#To check if we get an image from the camera or not
counter = 0
error = 0
print ('Press ctrl-c to proceed')
try:
  while ( True and error < 5):
  image = Tis.Get_image()
  if image != None:
  error = 0
  counter = counter + 1
  cv2.imshow('Window', image)
else:
  print ("No image reveived ")
  error = error + 1
 cv2.waitKey(1000) #wait time in ms
except KeyboardInterrupt:
  print ('fertig gekuckt')
  print (' ')



#To check for calibration image
if captureCalibrationImage == 1:
  print ('JETZT TROPFENGENERATOR AUSSCHALTEN!')
   raw_input("proced with return")
  print (' ')

counter = 0
while counter != N_EichBilderDuese:
image = Tis.Get_image()
if image != None:
  counter = counter + 1
  error = 0
  tmp='%003.0f' %(counter)
  image_name='Calibration_Image_'+str(tmp)+'.jpg'
  cv2.imwrite(image_name , image)
  cv2.imshow('Window', image)
else:
  print ("No image revived ")
  error = error + 1
 cv2.waitKey(500) #wait time in ms


#Read Calibration Image:
for ii in range(1,N_EichBilderDuese+1,1):
   tmp='%003.0f' %(ii)
  image_name='Calibration_Image_'+str(tmp)+'.jpg'
  image = misc.imread(image_name)
  if ii == 1:
    image_sum=image
  else:
    image_sum=image_sum+image
  print(image_name)

#Process the Calibration Image
image_calibration=image_sum/N_EichBilderDuese
fig1 = plt.figure(1)
fig1.suptitle('calibration image')
plt.imshow(image_calibration, cmap='gray')
plt.draw()
plt.waitforbuttonpress()
cv2.imwrite('/home/CalibrationImage.jpg',image_calibration) #Saving the Calibration Image
cv2.waitKey(1000) #wait time in ms
image_1 = cv2.imread('CalibrationImage.jpg')
gray_image = cv2.cvtColor(image_1, cv2.COLOR_BGR2GRAY)
   cv2.imwrite('/home/gray_image.jpg',gray_image) #Converting the Calibration Image to Greyscale and storing it

    ret, thresh = cv2.threshold(gray_image, 127, 255, cv2.THRESH_BINARY)
    contours,hierarchy=cv2.findContours(thresh,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)      


# Draw Contour
contour_image = cv2.drawContours(thresh,contours,-1,(255,255,255),3)
#Bounding Rectangle to find the lowest nozzle edge
cnt = contours[0]
x,y,w,h = cv2.boundingRect(cnt)
contour_image = cv2.rectangle(contour_image,(x,y),(x+w,y+h),(0,255,0),2)
print(x,y,w,h)
line_lowerNozzleEdge = y+h #y coordinate is the top left point, hence adding the height would give the lower edge
print('Lowest Line Edge=', line_lowerNozzleEdge) #Lowest Nozzle Edge
length_lowerNozzleLine = line_lowerNozzleEdge+x
print('Length of the Lower Nozzle Line=', length_lowerNozzleLine)

#CAPTURE DROPLETS
print('Now capture droplets;')
error = 0
print(' ')
print('Press ctrl-c to stop')
print(' ')
try:
  counter = 0
  while ( True and error < 5):
    image = Tis.Get_image()
    if image != None:
      cv2.imshow('Window',image)
      image_name='ActualImage.jpg'
      cv2.imwrite(image_name , image)
      error = 0
      counter = counter + 1
      image=np.squeeze(image)
  Diameter=DropletRecognition(image,gray_image, line_lowerNozzleEdge)
else:
  print ("No image revived ")
  error = error + 1
cv2.waitKey(1000) #wait time in ms

except KeyboardInterrupt:
  cv2.destroyWindow('Window')


Tis.Stop_pipeline()
cv2.destroyAllWindows()
print ('Program ended')
将numpy导入为np
导入系统
导入操作系统
进口cv2
进口西皮
为相机导入TIS#
从scipy导入杂项
从scipy导入ndimage
将matplotlib.pyplot作为plt导入
从skimage.filters导入阈值\u otsu
从脱脂进口措施
从浏览导入功能
从脱脂进口颜色
从撇渣进口io
作为mh导入mahotas
#液滴识别
def液滴识别(图像原始、灰度图像、线条较低):
showImages=1
image\u orig=misc.imread(image\u name)
图像=图像源
如果showImages==1:
打印('测量图像:')
打印(图像源)
打印('校准图像:')
打印(灰度图像)
图2=plt.图(2)
图2.suptitle(图像名称+图像)
plt.imshow(图片来源,cmap='gray')
plt.draw()
plt.waitforbuttonpress()
cv2.imwrite('/home/Captured_Image.jpg',Image_orig)#保存校准图像
cv2.等待键(1000)
image_2=cv2.imread('Captured_image.jpg'))
gray_image_2=cv2.CVT颜色(image_2,cv2.COLOR_BGR2GRAY)
#减去图像:
图像减去=图像-图像校准
如果showImages==1:
打印('减去的图像:')
打印(减去图像)
图3=plt.图(3)
图3.suptitle(“减去的图像”)
plt.imshow(图像减去,cmap='gray')
plt.draw()
plt.waitforbuttonpress()
图像\u减去[0:line\u lowerNozzleEdge][:]=0
图像减去=图像减去[line\u lowerNozzleEdge+1:][:]
image\u subtracted=image\u subtracted.astype('uint32')
图像\u tmp=减去图像
内核=np.ones((5,5),np.uint8)
图像\u tmp=图像。灰色\u侵蚀(图像\u tmp,尺寸=(6,6))
图像\u tmp=nImage.grey\u膨胀(图像\u tmp,大小=(6,6))
如果showImages==1:
打印('max(减去图像)='+str(np.max(减去图像)))
图像减去=图像tmp
thresh_rc=mh.thresholding.rc(减去图像_)
阈值中值=np.中值(减去图像)
thresh=thresh\u rc
图像\二进制=图像\减去>阈值
image\u bin\u int=image\u binary.astype('uint8')
image\u tmp=image\u bin\u int
im\u floodfill=image\u tmp.copy()
a、 b=图像形状[:2]
掩码=np.0((a+2,b+2),np.uint8)
cv2.泛光填充(im_泛光填充,遮罩,(0,0),255);
im\U泛光填充\U inv=cv2。按位\U not(im\U泛光填充)
im_out=图像tmp|im_洪水填充_inv
图像提取=输入输出
T=mh.thresholding.otsu(图像提取.astype('uint8'))
已标记,nr_objects=mh.label(图像提取.astype('uint8')>T)
打印('检测到的对象数='+str(nr_对象))
label_array=np.array(已标记).ravel()
label\u array=np.sort(label\u array)
像素总和=np.零(nr_对象+1)
对于范围内的ii(1,nr_对象+1,1):
n_tmp=np.where(label_array==ii)[0]
像素和[ii]=len(n\u tmp)
ObjectArea=像素总和*像素面积
半径=np.sqrt(ObjectArea/np.pi)
直径=2*半径
打印(“”)
打印(“对象直径单位为um=”+str(直径/1e-8))
打印(“”)
打印(“”)
如果showImages==1:
图4=plt.图(4)
plt.clf()
plt.imshow(带标签,插值='none')
plt.draw()
plt.waitforbuttonpress()
回流直径
返回nr_对象
#定义基本参数
#校准图像的数量(典型“1”):
N_EichBilderDuese=1
#捕获校准图像('1'=是):
captureCalibrationImage=0
#校准需要多少行用于测量图像的亮度自适应
#图像减法:
Ausschnitcal_y=230
#计算比例:
右喷嘴边缘=1140
左喷嘴边缘=160
像素大小=1e-3/(右喷嘴边缘左喷嘴边缘)
像素面积=像素大小**2
#摄像机设置
data=np.loadtxt('KameraEinstellungen.dat',dtype='uint16',skiprows=1)
Tis=Tis.Tis(数据[0]、数据[1]、数据[2]、数据[3]、数据[4])
Tis.Create_pipeline()#创建管道以启动相机
cv2.等待键(500)
Tis.启动_管道()
cv2.等待键(500)
cv2.namedWindow('Window',cv2.cv.cv\u Window\u NORMAL)
cv2.等待键(1000)
#来检查我们是否从相机上得到图像
计数器=0
错误=0
打印('按ctrl-c继续')
尝试:
而(正确且错误<5):
image=Tis.Get_image()
如果图像!=无:
错误=0
计数器=计数器+1
cv2.imshow(“窗口”,图像)
其他:
打印(“未收到图像”)
错误=错误+1
cv2.等待键(1000)#等待时间(毫秒)
除键盘中断外:
印刷品('fertig gekuckt')
打印(“”)
#检查校准图像
如果captureCalibrationImage==1:
打印('JETZT TROPFENGENATOR AUSSCHALTEN!')
原始输入(“返回处理”)
打印(“”)
计数器=0
而柜台!=N_EichBilderDuese:
image=Tis.Get_image()
如果图像!=无:
计数器=计数器+1
错误=0
tmp=“%003.0f%”(计数器)
图像\u name='Calibration\u图像\uu'+str(tmp)+'.jpg'
cv2.imwrite(图像\名称,图像)
cv2.imshow(“窗口”,图像)
其他:
打印(“无图像恢复”)
错误=错误+1
cv2.等待键(500)#等待时间(毫秒)
#读取校准图像:
对于范围内的ii(1,N_EichBilderDuese+1,1):
tmp=“%003.0f%”(ii)
图像\u name='Calibration\u图像\uu'+str(tmp)+'.jpg'
图像=杂项imread(图像名称)
如果ii==1:
image_sum=image
其他:
图像总和=图像总和+图像
打印(图像名称)
#处理校准图像
图像校准=图像总和/N\u EICHBILDERDUE
图1=plt.图(1)
图1.suptitle(“校准图像”)
plt.imshow(图像校准,cmap='gray')
plt.draw()
plt.waitforbuttonpress()
cv2.imwrite('/home/CalibrationImage.jpg',image_calibration)#保存校准图像
cv2.等待键(1000)#等待时间(毫秒)
image_1=cv2.imread('CalibrationImage.jpg'))
gr
 Press ctrl-c to proceed
Control_Program_Trial1.py:159: FutureWarning: comparison to `None` will result in an elementwise object comparison in the future.
  if image != None:
 ^Cfertig gekuckt

Calibration_Image_001.jpg
/usr/local/lib/python2.7/dist-packages/matplotlib/backend_bases.py:2437: MatplotlibDeprecationWarning: Using default event loop until function specific to this GUI is implemented
  warnings.warn(str, mplDeprecation)
(110, 958, 1, 1)
('Lowest Line Edge=', 959)
('Length of the Lower Nozzle Line=', 1069)
Now capture droplets;

Press ctrl-c to stop

Control_Program_Trial1.py:246: FutureWarning: comparison to `None` will      result in an elementwise object comparison in the future.
  if image != None:
Measured Image:
[[ 70  63  57 ..., 210 197 203]
 [ 64  69  67 ..., 201 213 207]
 [ 64  65  64 ..., 211 205 218]
 ..., 
 [130 113 100 ..., 255 255 255]
 [120 118 113 ..., 255 255 255]
 [116 125 114 ..., 255 255 255]]
Calibration Image:
[[ 62  56  69 ..., 208 204 207]
 [ 64  71  60 ..., 205 213 210]
 [ 66  70  74 ..., 204 220 208]
 ..., 
 [118 124 120 ..., 255 255 255]
 [123 117 113 ..., 255 255 255]
 [122 120 118 ..., 255 255 255]]
Subtracted Image:
[[  8   7 244 ...,   2 249 252]
 [  0 254   7 ..., 252   0 253]
 [254 251 246 ...,   7 241  10]
 ..., 
 [ 12 245 236 ...,   0   0   0]
 [253   1   0 ...,   0   0   0]
 [250   5 252 ...,   0   0   0]]
Traceback (most recent call last):
  File "Control_Program_Trial1.py", line 253, in <module>
Diameter=DropletRecognition(image,gray_image, line_lowerNozzleEdge)
 File "Control_Program_Trial1.py", line 68, in DropletRecognition
  print('max(image_subtracted) = '+str(np.max(image_subtracted)))   
  File "/usr/local/lib/python2.7/dist-packages/numpy/core/fromnumeric.py", line 2268, in amax
out=out, keepdims=keepdims)
  File "/usr/local/lib/python2.7/dist-packages/numpy/core/_methods.py", line 26, in _amax
return umr_maximum(a, axis, None, out, keepdims)
ValueError: zero-size array to reduction operation maximum which has no identity
import cv2
import numpy as np 


k= np.ones((5,5), np.uint8)
img1 = cv2.imread("calib.jpg",0)
img2 = cv2.imread("drop.jpg",0)

diff = cv2.absdiff(img1 ,img2)


d = cv2.erode(diff,k, iterations = 2)
img = cv2.dilate(d,k, iterations = 2)


cimg = cv2.cvtColor(img,cv2.COLOR_GRAY2BGR)

circles = cv2.HoughCircles(img,cv2.HOUGH_GRADIENT,1,20,
                            param1=50,param2=30,minRadius=0,maxRadius=0)

circles = np.uint16(np.around(circles))
for i in circles[0,:]:

    cv2.circle(cimg,(i[0],i[1]),i[2],(0,255,0),2)

    cv2.circle(cimg,(i[0],i[1]),2,(0,0,255),3)
    print "center : " + str(i[0]) + " "+ str(i[1])
    print "radius : " + str(i[2])



cv2.imwrite("ccc.jpg",cimg)
cv2.destroyAllWindows()