rkantos

Untitled

Jan 27th, 2019
809
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.79 KB | None | 0 0
  1. #===========================================================================#
  2. # #
  3. # Copyright (C) 2006 - 2018 #
  4. # IDS Imaging Development Systems GmbH #
  5. # Dimbacher Str. 6-8 #
  6. # D-74182 Obersulm, Germany #
  7. # #
  8. # The information in this document is subject to change without notice #
  9. # and should not be construed as a commitment by IDS Imaging Development #
  10. # Systems GmbH. IDS Imaging Development Systems GmbH does not assume any #
  11. # responsibility for any errors that may appear in this document. #
  12. # #
  13. # This document, or source code, is provided solely as an example #
  14. # of how to utilize IDS software libraries in a sample application. #
  15. # IDS Imaging Development Systems GmbH does not assume any responsibility #
  16. # for the use or reliability of any portion of this document or the #
  17. # described software. #
  18. # #
  19. # General permission to copy or modify, but not for profit, is hereby #
  20. # granted, provided that the above copyright notice is included and #
  21. # reference made to the fact that reproduction privileges were granted #
  22. # by IDS Imaging Development Systems GmbH. #
  23. # #
  24. # IDS Imaging Development Systems GmbH cannot assume any responsibility #
  25. # for the use or misuse of any portion of this software for other than #
  26. # its intended diagnostic purpose in calibrating and testing IDS #
  27. # manufactured cameras and software. #
  28. # #
  29. #===========================================================================#
  30.  
  31. # Developer Note: I tried to let it as simple as possible.
  32. # Therefore there are no functions asking for the newest driver software or freeing memory beforehand, etc.
  33. # The sole purpose of this program is to show one of the simplest ways to interact with an IDS camera via the uEye API.
  34. # (XS cameras are not supported)
  35. #---------------------------------------------------------------------------------------------------------------------------------------
  36.  
  37. #Libraries
  38. from pyueye import ueye
  39. import numpy as np
  40. import cv2
  41. import sys
  42.  
  43. #---------------------------------------------------------------------------------------------------------------------------------------
  44.  
  45. #Variables
  46. hCam = ueye.HIDS(0) #0: first available camera; 1-254: The camera with the specified camera ID
  47. sInfo = ueye.SENSORINFO()
  48. cInfo = ueye.CAMINFO()
  49. pcImageMemory = ueye.c_mem_p()
  50. MemID = ueye.int()
  51. rectAOI = ueye.IS_RECT()
  52. pitch = ueye.INT()
  53. nBitsPerPixel = ueye.INT(24) #24: bits per pixel for color mode; take 8 bits per pixel for monochrome
  54. channels = 3 #3: channels for color mode(RGB); take 1 channel for monochrome
  55. m_nColorMode = ueye.INT() # Y8/RGB16/RGB24/REG32
  56. bytes_per_pixel = int(nBitsPerPixel / 8)
  57. #---------------------------------------------------------------------------------------------------------------------------------------
  58. print("START")
  59. print()
  60.  
  61.  
  62. # Starts the driver and establishes the connection to the camera
  63. nRet = ueye.is_InitCamera(hCam, None)
  64. if nRet != ueye.IS_SUCCESS:
  65. print("is_InitCamera ERROR")
  66.  
  67. # Reads out the data hard-coded in the non-volatile camera memory and writes it to the data structure that cInfo points to
  68. nRet = ueye.is_GetCameraInfo(hCam, cInfo)
  69. if nRet != ueye.IS_SUCCESS:
  70. print("is_GetCameraInfo ERROR")
  71.  
  72. # You can query additional information about the sensor type used in the camera
  73. nRet = ueye.is_GetSensorInfo(hCam, sInfo)
  74. if nRet != ueye.IS_SUCCESS:
  75. print("is_GetSensorInfo ERROR")
  76.  
  77. nRet = ueye.is_ResetToDefault( hCam)
  78. if nRet != ueye.IS_SUCCESS:
  79. print("is_ResetToDefault ERROR")
  80.  
  81. # Set display mode to DIB
  82. nRet = ueye.is_SetDisplayMode(hCam, ueye.IS_SET_DM_DIB)
  83.  
  84.  
  85.  
  86. # Can be used to set the size and position of an "area of interest"(AOI) within an image
  87. nRet = ueye.is_AOI(hCam, ueye.IS_AOI_IMAGE_GET_AOI, rectAOI, ueye.sizeof(rectAOI))
  88. if nRet != ueye.IS_SUCCESS:
  89. print("is_AOI ERROR")
  90.  
  91. width = rectAOI.s32Width
  92. height = rectAOI.s32Height
  93.  
  94. # Prints out some information about the camera and the sensor
  95. print("Camera model:\t\t", sInfo.strSensorName.decode('utf-8'))
  96. print("Camera serial no.:\t", cInfo.SerNo.decode('utf-8'))
  97. print("Maximum image width:\t", width)
  98. print("Maximum image height:\t", height)
  99. print()
  100.  
  101.  
  102.  
  103. #########
  104. nRet = ueye.is_SetAutoParameter(hCam, ueye.IS_SET_ENABLE_AUTO_GAIN, ueye.double(1), ueye.double(0))
  105. print("is_SetAutoParameter returns " + str(nRet))
  106.  
  107. #fps
  108.  
  109. targetFPS = ueye.double(160) # insert here which FPS you want
  110. actualFPS = ueye.double(0)
  111. nRet = ueye.is_SetFrameRate(hCam,targetFPS,actualFPS)
  112. print("is_SetFrameRate returns " + str(nRet) + ", Actual FPS is: " + str(actualFPS) + "Target FPS is: " + str(targetFPS))
  113.  
  114. #get pixelclock
  115.  
  116. #var
  117.  
  118. nPixelClock = ueye.UINT()
  119. nRet = ueye.is_PixelClock(hCam, ueye.IS_PIXELCLOCK_CMD_GET, nPixelClock, ueye.sizeof(nPixelClock))
  120. print("Current PixelClock", nPixelClock)
  121.  
  122.  
  123. #set pixelclock
  124. #nPixelClock = ueye.UINT()
  125.  
  126. #set AutoExposure
  127. nRet = ueye.is_SetAutoParameter(hCam, ueye.IS_SET_ENABLE_AUTO_SHUTTER, ueye.double(1), ueye.double(0))
  128. print("is_SetAutoParameter returns " + str(nRet))
  129.  
  130.  
  131. ## set color
  132.  
  133. # nRet = ueye.is_SetColorMode(hCam, ueye.IS_CM_BGR8_PACKED)
  134.  
  135. # print("SetColorMode IS_CM_BGR8_PACKED returns " + str(nRet))
  136. m_nColorMode = ueye.IS_CM_BGRA8_PACKED
  137. nBitsPerPixel = ueye.INT(32)
  138. bytes_per_pixel = int(nBitsPerPixel / 8)
  139. print("IS_COLORMODE_CBYCRY: ", )
  140. print("\tm_nColorMode: \t\t", m_nColorMode)
  141. print("\tnBitsPerPixel: \t\t", nBitsPerPixel)
  142. print("\tbytes_per_pixel: \t\t", bytes_per_pixel)
  143. print()
  144.  
  145.  
  146. ## AOI Merge mode
  147.  
  148. nVerticalAoiMergeMode = ueye.int(0)
  149. nVerticalAoiMergePosition = ueye.int(0)
  150.  
  151. # /* Read current vertical AOI merge mode */
  152.  
  153. nRet = ueye.is_DeviceFeature(hCam, ueye.IS_DEVICE_FEATURE_CMD_GET_VERTICAL_AOI_MERGE_MODE, nVerticalAoiMergeMode, ueye.sizeof(nVerticalAoiMergeMode))
  154. if nRet == ueye.IS_SUCCESS:
  155. print("AOI Merge MODE:", nVerticalAoiMergeMode)
  156. if nVerticalAoiMergeMode == ueye.IS_VERTICAL_AOI_MERGE_MODE_FREERUN:
  157. # /* Read current AOI merge position */
  158. nRet = ueye.is_DeviceFeature(hCam, ueye.IS_DEVICE_FEATURE_CMD_GET_VERTICAL_AOI_MERGE_POSITION, nVerticalAoiMergePosition, ueye.sizeof(nVerticalAoiMergePosition))
  159. print("AOI Merge Position:", nVerticalAoiMergePosition)
  160. # /* Set vertical AOI merge mode */
  161. nVerticalAoiMergeMode = ueye.IS_VERTICAL_AOI_MERGE_MODE_FREERUN
  162. nRet = ueye.is_DeviceFeature(hCam, ueye.IS_DEVICE_FEATURE_CMD_SET_VERTICAL_AOI_MERGE_MODE, nVerticalAoiMergeMode, ueye.sizeof(nVerticalAoiMergeMode))
  163.  
  164. if nRet == ueye.IS_SUCCESS:
  165. # /* Read and set new maximum AOI */
  166. pInfo = ueye.SENSORINFO()
  167.  
  168. nRet = ueye.is_GetSensorInfo(hCam, pInfo)
  169.  
  170. maxWidth = ueye.int(pInfo.nMaxWidth)
  171. maxHeight = ueye.int(pInfo.nmaxHeight)
  172.  
  173.  
  174. # rectAOI = ueye.IS_RECT()
  175.  
  176. # C
  177. # rectAOI.s32X = 0;
  178. # rectAOI.s32Y = 0;
  179.  
  180. rectAOI.s32X = ueye.INT(0)
  181. rectAOI.s32Y = ueye.INT(0)
  182.  
  183. # C
  184. # rectAOI.s32Width = maxWidth;
  185. # rectAOI.s32Height = maxHeight;
  186.  
  187. maxWidth = rectAOI.s32Width
  188. maxHeight = rectAOI.s32Height
  189.  
  190. # C
  191. # nRet = is_AOI(hCam, IS_AOI_IMAGE_SET_AOI, (void*)&rectAOI, sizeof(rectAOI));
  192.  
  193. nRet = ueye.is_AOI(hCam, ueye.IS_AOI_IMAGE_SET_AOI, rectAOI, ueye.sizeof(rectAOI))
  194.  
  195.  
  196. # /*Set vertical AOI merge position */
  197.  
  198. # C
  199. # INT nVerticalAoiMergePosition = 100;
  200. # nRet = is_DeviceFeature(hCam, IS_DEVICE_FEATURE_CMD_SET_VERTICAL_AOI_MERGE_POSITION,
  201. # (void*)&nVerticalAoiMergePosition, sizeof(nVerticalAoiMergePosition));
  202. # }
  203.  
  204. nVerticalAoiMergePosition = ueye.INT(height/2)
  205.  
  206. nRet = is_DeviceFeature(hCam, ueye.IS_DEVICEFEATURE_CMD_SET_VERTICAL_AOI_MERGE_POSITION, nVerticalAoiMergePosition, ueye.sizeof(nVerticalAoiMergePosition))
  207.  
  208. # /* Read current vertical AOI merge mode */
  209.  
  210. nRet = ueye.is_DeviceFeature(hCam, ueye.IS_DEVICE_FEATURE_CMD_GET_VERTICAL_AOI_MERGE_MODE, nVerticalAoiMergeMode, ueye.sizeof(nVerticalAoiMergeMode))
  211. if nRet == ueye.IS_SUCCESS:
  212. print("AOI Merge MODE")
  213. print(nVerticalAoiMergeMode)
  214.  
  215.  
  216. #---------------------------------------------------------------------------------------------------------------------------------------
  217.  
  218. # Allocates an image memory for an image having its dimensions defined by width and height and its color depth defined by nBitsPerPixel
  219. nRet = ueye.is_AllocImageMem(hCam, width, height, nBitsPerPixel, pcImageMemory, MemID)
  220. if nRet != ueye.IS_SUCCESS:
  221. print("is_AllocImageMem ERROR")
  222. else:
  223. # Makes the specified image memory the active memory
  224. nRet = ueye.is_SetImageMem(hCam, pcImageMemory, MemID)
  225. if nRet != ueye.IS_SUCCESS:
  226. print("is_SetImageMem ERROR")
  227. else:
  228. # Set the desired color mode
  229. nRet = ueye.is_SetColorMode(hCam, m_nColorMode)
  230.  
  231.  
  232.  
  233. # Activates the camera's live video mode (free run mode)
  234. nRet = ueye.is_CaptureVideo(hCam, ueye.IS_DONT_WAIT)
  235. if nRet != ueye.IS_SUCCESS:
  236. print("is_CaptureVideo ERROR")
  237.  
  238. # Enables the queue mode for existing image memory sequences
  239. nRet = ueye.is_InquireImageMem(hCam, pcImageMemory, MemID, width, height, nBitsPerPixel, pitch)
  240. if nRet != ueye.IS_SUCCESS:
  241. print("is_InquireImageMem ERROR")
  242. else:
  243. print("Press q to leave the programm")
  244.  
  245. #---------------------------------------------------------------------------------------------------------------------------------------
  246.  
  247.  
  248. # Continuous image display
  249. while(nRet == ueye.IS_SUCCESS):
  250.  
  251. # In order to display the image in an OpenCV window we need to...
  252. # ...extract the data of our image memory
  253. array = ueye.get_data(pcImageMemory, width, height, nBitsPerPixel, pitch, copy=False)
  254.  
  255. # bytes_per_pixel = int(nBitsPerPixel / 8)
  256.  
  257. # ...reshape it in an numpy array...
  258. frame = np.reshape(array,(height.value, width.value, bytes_per_pixel))
  259.  
  260. # ...resize the image by a half
  261.  
  262. cv2.rectangle(frame,(100,200),(200,300),(0,0,255),5)
  263.  
  264. cv2width = int(width)
  265. cv2height = int(height/2)
  266.  
  267.  
  268. cv2.line(frame,(0,cv2height),(cv2width,cv2height),(255,255,255),5)
  269.  
  270. frame = cv2.resize(frame,(0,0),fx=0.25, fy=0.25)
  271.  
  272. frame = cv2.rotate(frame, cv2.ROTATE_90_CLOCKWISE)
  273.  
  274.  
  275. #---------------------------------------------------------------------------------------------------------------------------------------
  276. #Include image data processing here
  277.  
  278. #---------------------------------------------------------------------------------------------------------------------------------------
  279.  
  280. #...and finally display it
  281. cv2.imshow("SimpleLive_Python_uEye_OpenCV", frame)
  282.  
  283. # Press q if you want to end the loop
  284. if cv2.waitKey(1) & 0xFF == ord('q'):
  285. break
  286. #---------------------------------------------------------------------------------------------------------------------------------------
  287.  
  288. # Releases an image memory that was allocated using is_AllocImageMem() and removes it from the driver management
  289. ueye.is_FreeImageMem(hCam, pcImageMemory, MemID)
  290.  
  291. # Disables the hCam camera handle and releases the data structures and memory areas taken up by the uEye camera
  292. ueye.is_ExitCamera(hCam)
  293.  
  294. # Destroys the OpenCv windows
  295. cv2.destroyAllWindows()
  296.  
  297. print()
  298. print("END")
Advertisement
Add Comment
Please, Sign In to add comment