IDS_trigger.py 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  1. from pyueye import ueye
  2. import numpy as np
  3. import time
  4. import cv2
  5. import os
  6. from datetime import datetime
  7. import datetime
  8. hCam = ueye.HIDS(0)
  9. sensor_info = ueye.SENSORINFO()
  10. camera_info = ueye.CAMINFO()
  11. pcImageMemory = ueye.c_mem_p()
  12. MemID = ueye.int()
  13. #rectAOI = ueye.IS_RECT()
  14. #rectAOI.s32X = 0
  15. #rectAOI.s32Y = 216#348 #664
  16. #rectAOI.s32Width = 1936
  17. #rectAOI.s32Height = 372 #334
  18. pitch = ueye.INT()
  19. nBitsPerPixel = ueye.INT(24) #24: bits per pixel for color mode; take 8 bits per pixel for monochrome
  20. channels = 3 #3: channels for color mode(RGB); take 1 channel for monochrome
  21. m_nColorMode = ueye.INT() # Y8/RGB16/RGB24/REG32
  22. bytes_per_pixel = int(nBitsPerPixel / 8)
  23. dZoomValue = ueye.DOUBLE()
  24. fps = ueye.DOUBLE(60) # set you want fps
  25. Real_FPS =ueye.DOUBLE() # get real fps
  26. exposure_value = ueye.DOUBLE() # set exposure value
  27. cbSizeOfParam = 8
  28. #total_image = ueye.UINT(40)
  29. nSizeOfParam = 4
  30. delay_time = ueye.UINT(10000) #ms
  31. delay_time_nSizeOfParam = 4
  32. BurstSize = ueye.UINT(20)
  33. status_new=0
  34. status_old=1
  35. print("START")
  36. # Starts the driver and establishes the connection to the camera
  37. nRet = ueye.is_InitCamera(hCam, None)
  38. if nRet != ueye.IS_SUCCESS:
  39. print("is_InitCamera ERROR")
  40. nRet = ueye.is_SetFrameRate(hCam,fps,Real_FPS)
  41. if nRet != ueye.IS_SUCCESS:
  42. print("is_SetFrameRate ERROR")
  43. nRet = ueye.is_Exposure(hCam,ueye.IS_EXPOSURE_CMD_SET_EXPOSURE,exposure_value,cbSizeOfParam)
  44. if nRet != ueye.IS_SUCCESS:
  45. print("is_Exposure ERROR")
  46. nRet = ueye.is_SetExternalTrigger(hCam, ueye.IS_SET_TRIGGER_HI_LO)
  47. if nRet != ueye.IS_SUCCESS:
  48. print("is_SetExternalTrigger ERROR")
  49. nRet = ueye.is_Trigger(hCam, ueye.IS_TRIGGER_CMD_SET_BURST_SIZE,BurstSize,ueye.sizeof(BurstSize))
  50. if nRet != ueye.IS_SUCCESS:
  51. print("is_Trigger ERROR")
  52. # Reads out the data hard-coded in the non-volatile camera memory and writes it to the data structure that cInfo points to
  53. nRet = ueye.is_GetCameraInfo(hCam, camera_info)
  54. if nRet != ueye.IS_SUCCESS:
  55. print("is_GetCameraInfo ERROR")
  56. # You can query additional information about the sensor type used in the camera
  57. nRet = ueye.is_GetSensorInfo(hCam, sensor_info)
  58. if nRet != ueye.IS_SUCCESS:
  59. print("is_GetSensorInfo ERROR")
  60. '''
  61. nRet = ueye.is_ResetToDefault(hCam)
  62. if nRet != ueye.IS_SUCCESS:
  63. print("is_ResetToDefault ERROR")
  64. '''
  65. # Set display mode to DIB
  66. nRet = ueye.is_SetDisplayMode(hCam, ueye.IS_SET_DM_DIB)
  67. nRet = ueye.is_SetColorMode(hCam,ueye.IS_CM_BGR8_PACKED)
  68. if nRet != ueye.IS_SUCCESS:
  69. print("is_SetColorMode ERROR")
  70. # Set the right color mode
  71. if int.from_bytes(sensor_info.nColorMode.value, byteorder='big') == ueye.IS_COLORMODE_BAYER:
  72. # setup the color depth to the current windows setting
  73. ueye.is_GetColorDepth(hCam, nBitsPerPixel, m_nColorMode)
  74. bytes_per_pixel = int(nBitsPerPixel / 8)
  75. print("IS_COLORMODE_BAYER: ", )
  76. print("\tm_nColorMode:", m_nColorMode)
  77. print("\tnBitsPerPixel:", nBitsPerPixel)
  78. print("\tbytes_per_pixel:", bytes_per_pixel)
  79. elif int.from_bytes(sensor_info.nColorMode.value, byteorder='big') == ueye.IS_COLORMODE_CBYCRY:
  80. # for color camera models use RGB32 mode
  81. m_nColorMode = ueye.IS_CM_BGRA8_PACKED
  82. nBitsPerPixel = ueye.INT(32)
  83. bytes_per_pixel = int(nBitsPerPixel / 8)
  84. print("IS_COLORMODE_CBYCRY: ", )
  85. print("\tm_nColorMode: \t\t", m_nColorMode)
  86. print("\tnBitsPerPixel: \t\t", nBitsPerPixel)
  87. print("\tbytes_per_pixel: \t\t", bytes_per_pixel)
  88. elif int.from_bytes(sensor_info.nColorMode.value, byteorder='big') == ueye.IS_COLORMODE_MONOCHROME:
  89. # for color camera models use RGB32 mode
  90. m_nColorMode = ueye.IS_CM_MONO8
  91. nBitsPerPixel = ueye.INT(8)
  92. bytes_per_pixel = int(nBitsPerPixel / 8)
  93. print("IS_COLORMODE_MONOCHROME: ", )
  94. print("\tm_nColorMode: \t\t", m_nColorMode)
  95. print("\tnBitsPerPixel: \t\t", nBitsPerPixel)
  96. print("\tbytes_per_pixel: \t\t", bytes_per_pixel)
  97. else:
  98. # for monochrome camera models use Y8 mode
  99. m_nColorMode = ueye.IS_CM_MONO8
  100. nBitsPerPixel = ueye.INT(8)
  101. bytes_per_pixel = int(nBitsPerPixel / 8)
  102. print("else")
  103. '''
  104. nRet = ueye.is_AOI(hCam, ueye.IS_AOI_IMAGE_SET_AOI, rectAOI, ueye.sizeof(rectAOI))
  105. if nRet != ueye.IS_SUCCESS:
  106. print("is_AOI ERROR")
  107. '''
  108. # Can be used to set the size and position of an "area of interest"(AOI) within an image
  109. nRet = ueye.is_AOI(hCam, ueye.IS_AOI_IMAGE_GET_AOI, rectAOI, ueye.sizeof(rectAOI))
  110. if nRet != ueye.IS_SUCCESS:
  111. print("is_AOI ERROR")
  112. width = rectAOI.s32Width
  113. height = rectAOI.s32Height
  114. # Prints out some information about the camera and the sensor
  115. print("Exposure value : {}".format(exposure_value))
  116. print("Maximum image width:", width)
  117. print("Maximum image height:", height)
  118. # ---------------------------------------------------------------------------------------------------------------------------------------
  119. # Allocates an image memory for an image having its dimensions defined by width and height and its color depth defined by nBitsPerPixel
  120. nRet = ueye.is_AllocImageMem(hCam, width, height, nBitsPerPixel, pcImageMemory, MemID)
  121. if nRet != ueye.IS_SUCCESS:
  122. print("is_AllocImageMem ERROR")
  123. else:
  124. # Makes the specified image memory the active memory
  125. nRet = ueye.is_SetImageMem(hCam, pcImageMemory, MemID)
  126. if nRet != ueye.IS_SUCCESS:
  127. print("is_SetImageMem ERROR")
  128. else:
  129. # Set the desired color mode
  130. nRet = ueye.is_SetColorMode(hCam, m_nColorMode)
  131. # Activates the camera's live video mode (free run mode)
  132. nRet = ueye.is_CaptureVideo(hCam, ueye.IS_DONT_WAIT)
  133. if nRet != ueye.IS_SUCCESS:
  134. print("is_CaptureVideo ERROR")
  135. # Enables the queue mode for existing image memory sequences
  136. nRet = ueye.is_InquireImageMem(hCam, pcImageMemory, MemID, width, height, nBitsPerPixel, pitch)
  137. if nRet != ueye.IS_SUCCESS:
  138. print("is_InquireImageMem ERROR")
  139. else:
  140. print("Press q to leave the programm")
  141. # ---------------------------------------------------------------------------------------------------------------------------------------
  142. i = 0
  143. f = 0
  144. # Continuous image display
  145. while True:
  146. point_color = (255,255,255)#BGR
  147. # In order to display the image in an OpenCV window we need to...
  148. # ...extract the data of our image memory
  149. array = ueye.get_data(pcImageMemory, width, height, nBitsPerPixel, pitch, copy=False)
  150. # bytes_per_pixel = int(nBitsPerPixel / 8)
  151. # ...reshape it in an numpy array...
  152. frame = np.reshape(array, (height.value, width.value, bytes_per_pixel))
  153. #print(frame.shape)
  154. #cv2.rectangle(frame, (1072, 92), (1213, 226), point_color, 1)
  155. roi5 = frame[527:601, 1247:1330]
  156. cv2.imshow('roi5', roi5)
  157. print("roi1 Height&width:", str(roi5.shape))
  158. w = roi5.shape[0]
  159. h = roi5.shape[1]
  160. x = 0
  161. y = 0
  162. i = 0
  163. while True:
  164. i = i + 1
  165. (b, g, r,a) = roi5[x, y] # 读取(0,0)像素,Python中图像像素是按B,G,R顺序存储的
  166. print(i,"當前位置像素 -- 红:%d,绿:%d,蓝:%d" % (r, g, b)) # 显示像素值
  167. # print(r)
  168. # print(x, y)
  169. x = x + 1
  170. if x == w:
  171. y = y + 1
  172. x = 0
  173. if y == h:
  174. break
  175. #roi1 = frame[92:226, 1072:1213]
  176. #cv2.imshow('roi1', roi1)
  177. #cv2.rectangle(frame, (347, 234), (497, 363), point_color, 1)
  178. #roi2 = frame[234:363, 347:497]
  179. #cv2.imshow('roi2', roi2)
  180. #cv2.rectangle(frame, (835, 216), (1011, 362), point_color, 1)
  181. #roi3 = frame[216:362, 835:1011]
  182. #cv2.imshow('roi3', roi3)
  183. #cv2.rectangle(frame, (1311, 205), (1438, 356), point_color, 1)
  184. #roi4 = frame[205:356, 1311:1438]
  185. #cv2.imshow('roi4', roi4)
  186. #cv2.imshow("SimpleLive_Python_uEye_OpenCV", frame)
  187. trigger_status = ueye.is_SetExternalTrigger(hCam,ueye.IS_GET_TRIGGER_STATUS)
  188. status_new = trigger_status
  189. print(status_old, status_new)
  190. # start = datetime.datetime.now()
  191. if status_old == 1 and status_new == 0:
  192. start = datetime.datetime.now()
  193. '''
  194. cv2.imwrite('C:\\Users\\User\\Desktop\\IDS\\test1.png', roi1)
  195. image1 = cv2.imread("C:\\Users\\User\\Desktop\\IDS\\test1.png") # 读取图像
  196. cv2.imwrite('C:\\Users\\User\\Desktop\\IDS\\test2.png', roi2)
  197. image2 = cv2.imread("C:\\Users\\User\\Desktop\\IDS\\test2.png")
  198. cv2.imwrite('C:\\Users\\User\\Desktop\\IDS\\test3.png', roi3)
  199. image3 = cv2.imread("C:\\Users\\User\\Desktop\\IDS\\test3.png")
  200. cv2.imwrite('C:\\Users\\User\\Desktop\\IDS\\test4.png', roi4)
  201. image4 = cv2.imread("C:\\Users\\User\\Desktop\\IDS\\test4.png")
  202. (B1, G1, R1) = cv2.split(image1) # 提取R、G、B分量
  203. (B2, G2, R2) = cv2.split(image2)
  204. (B3, G3, R3) = cv2.split(image3)
  205. (B4, G4, R4) = cv2.split(image4)
  206. print("image1 Height&width:", str(image1.shape))
  207. print("image2 Height&width:", str(image2.shape))
  208. print("image3 Height&width:", str(image3.shape))
  209. print("image4 Height&width:", str(image4.shape))
  210. w1 = image1.shape[0]
  211. h1 = image1.shape[1]
  212. x1 = 0
  213. y1 = 0
  214. i1 = 0
  215. while True:
  216. i1 = i1 + 1
  217. (b, g, r) = image1[x1, y1] # 读取(0,0)像素,Python中图像像素是按B,G,R顺序存储的
  218. #print(i,"當前位置像素 -- 红:%d,绿:%d,蓝:%d" % (r, g, b)) # 显示像素值
  219. #print(r)
  220. # print(x, y)
  221. x1 = x1 + 1
  222. if x1 == w1:
  223. y1 = y1 + 1
  224. x1 = 0
  225. if y1 == h1:
  226. break
  227. w2 = image2.shape[0]
  228. h2 = image2.shape[1]
  229. x2 = 0
  230. y2 = 0
  231. i2 = 0
  232. while True:
  233. i2 = i2 + 1
  234. (b, g, r) = image2[x2, y2] # 读取(0,0)像素,Python中图像像素是按B,G,R顺序存储的
  235. # print(i,"當前位置像素 -- 红:%d,绿:%d,蓝:%d" % (r, g, b)) # 显示像素值
  236. # print(r)
  237. # print(x, y)
  238. x2 = x2 + 1
  239. if x2 == w2:
  240. y2 = y2 + 1
  241. x2 = 0
  242. if y2 == h2:
  243. break
  244. w3 = image3.shape[0]
  245. h3 = image3.shape[1]
  246. x3 = 0
  247. y3 = 0
  248. i3 = 0
  249. while True:
  250. i3 = i3 + 1
  251. (b, g, r) = image3[x3, y3] # 读取(0,0)像素,Python中图像像素是按B,G,R顺序存储的
  252. # print(i,"當前位置像素 -- 红:%d,绿:%d,蓝:%d" % (r, g, b)) # 显示像素值
  253. # print(r)
  254. # print(x, y)
  255. x3 = x3 + 1
  256. if x3 == w3:
  257. y3 = y3 + 1
  258. x3 = 0
  259. if y3 == h3:
  260. break
  261. w4 = image4.shape[0]
  262. h4 = image4.shape[1]
  263. x4 = 0
  264. y4 = 0
  265. i4 = 0
  266. while True:
  267. i4 = i4 + 1
  268. (b, g, r) = image4[x4, y4] # 读取(0,0)像素,Python中图像像素是按B,G,R顺序存储的
  269. # print(i,"當前位置像素 -- 红:%d,绿:%d,蓝:%d" % (r, g, b)) # 显示像素值
  270. # print(r)
  271. # print(x, y)
  272. x4 = x4 + 1
  273. if x4 == w4:
  274. y4 = y4 + 1
  275. x4 = 0
  276. if y4 == h4:
  277. break
  278. '''
  279. end = datetime.datetime.now()
  280. print("執行時間:", end - start)
  281. '''
  282. for i in range(25):
  283. array = ueye.get_data(pcImageMemory, width, height, nBitsPerPixel, pitch, copy=False)
  284. global frame1
  285. frame1 = np.reshape(array, (height.value, width.value, bytes_per_pixel))
  286. for i in range(25):
  287. f = f+1
  288. FileParams = ueye.IMAGE_FILE_PARAMS()
  289. FileParams.pwchFileName = "C:/Users/User/Desktop/IDS/p/get_bean3/" + str(f) + ".png"
  290. FileParams.nFileType = ueye.IS_IMG_PNG
  291. FileParams.ppcImageMem = None
  292. FileParams.pnImageID = None
  293. FileParams.nQuality = 75
  294. nRet = ueye.is_ImageFile(hCam, ueye.IS_IMAGE_FILE_CMD_SAVE, FileParams, ueye.sizeof(FileParams))
  295. print('take photo')
  296. '''
  297. '''
  298. start1 = datetime.datetime.now()
  299. array = ueye.get_data(pcImageMemory, width, height, nBitsPerPixel, pitch, copy=False)
  300. global frame1
  301. frame1= np.reshape(array, (height.value, width.value, bytes_per_pixel))
  302. i = i+1
  303. FileParams = ueye.IMAGE_FILE_PARAMS()
  304. FileParams.pwchFileName = "C:/Users/User/Desktop/IDS/p/20210304-1/" + str(i)+ ".png"
  305. FileParams.nFileType = ueye.IS_IMG_PNG
  306. FileParams.ppcImageMem = None
  307. FileParams.pnImageID = None
  308. FileParams.nQuality = 10
  309. nRet = ueye.is_ImageFile(hCam, ueye.IS_IMAGE_FILE_CMD_SAVE, FileParams, ueye.sizeof(FileParams))
  310. print('take photo')
  311. end = datetime.datetime.now()
  312. print("執行時間:", end - start)
  313. end1 = datetime.datetime.now()
  314. print("執行時間:", end1 - start1)
  315. if f == 1000:
  316. break
  317. status_old = status_new
  318. '''
  319. # ...resize the image by a half #frame後面的(800,600)可以自行調整成想要的大小
  320. #frame = cv2.resize(frame, (800, 600), fx=0.5, fy=0.5)
  321. #nRet = ueye.is_IO(hCam, ueye.IS_IO_CMD_GPIOS_GET_CONFIGURATION, gpio, ueye.sizeof(gpio))
  322. #cv2.imshow("SimpleLive_Python_uEye_OpenCV", frame)
  323. # Press q if you want to end the loop
  324. if cv2.waitKey(1) & 0xFF == ord('q'):
  325. break
  326. # ---------------------------------------------------------------------------------------------------------------------------------------
  327. # Releases an image memory that was allocated using is_AllocImageMem() and removes it from the driver management
  328. ueye.is_FreeImageMem(hCam, pcImageMemory, MemID)
  329. # Disables the hCam camera handle and releases the data structures and memory areas taken up by the uEye camera
  330. ueye.is_ExitCamera(hCam)
  331. # Destroys the OpenCv windows
  332. cv2.destroyAllWindows()