123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385 |
- from pyueye import ueye
- import numpy as np
- import time
- import cv2
- import os
- from datetime import datetime
- import datetime
- hCam = ueye.HIDS(0)
- sensor_info = ueye.SENSORINFO()
- camera_info = ueye.CAMINFO()
- pcImageMemory = ueye.c_mem_p()
- MemID = ueye.int()
- #rectAOI = ueye.IS_RECT()
- #rectAOI.s32X = 0
- #rectAOI.s32Y = 216#348 #664
- #rectAOI.s32Width = 1936
- #rectAOI.s32Height = 372 #334
- pitch = ueye.INT()
- nBitsPerPixel = ueye.INT(24) #24: bits per pixel for color mode; take 8 bits per pixel for monochrome
- channels = 3 #3: channels for color mode(RGB); take 1 channel for monochrome
- m_nColorMode = ueye.INT() # Y8/RGB16/RGB24/REG32
- bytes_per_pixel = int(nBitsPerPixel / 8)
- dZoomValue = ueye.DOUBLE()
- fps = ueye.DOUBLE(60) # set you want fps
- Real_FPS =ueye.DOUBLE() # get real fps
- exposure_value = ueye.DOUBLE() # set exposure value
- cbSizeOfParam = 8
- #total_image = ueye.UINT(40)
- nSizeOfParam = 4
- delay_time = ueye.UINT(10000) #ms
- delay_time_nSizeOfParam = 4
- BurstSize = ueye.UINT(20)
- status_new=0
- status_old=1
- print("START")
- # Starts the driver and establishes the connection to the camera
- nRet = ueye.is_InitCamera(hCam, None)
- if nRet != ueye.IS_SUCCESS:
- print("is_InitCamera ERROR")
- nRet = ueye.is_SetFrameRate(hCam,fps,Real_FPS)
- if nRet != ueye.IS_SUCCESS:
- print("is_SetFrameRate ERROR")
- nRet = ueye.is_Exposure(hCam,ueye.IS_EXPOSURE_CMD_SET_EXPOSURE,exposure_value,cbSizeOfParam)
- if nRet != ueye.IS_SUCCESS:
- print("is_Exposure ERROR")
- nRet = ueye.is_SetExternalTrigger(hCam, ueye.IS_SET_TRIGGER_HI_LO)
- if nRet != ueye.IS_SUCCESS:
- print("is_SetExternalTrigger ERROR")
- nRet = ueye.is_Trigger(hCam, ueye.IS_TRIGGER_CMD_SET_BURST_SIZE,BurstSize,ueye.sizeof(BurstSize))
- if nRet != ueye.IS_SUCCESS:
- print("is_Trigger ERROR")
- # Reads out the data hard-coded in the non-volatile camera memory and writes it to the data structure that cInfo points to
- nRet = ueye.is_GetCameraInfo(hCam, camera_info)
- if nRet != ueye.IS_SUCCESS:
- print("is_GetCameraInfo ERROR")
- # You can query additional information about the sensor type used in the camera
- nRet = ueye.is_GetSensorInfo(hCam, sensor_info)
- if nRet != ueye.IS_SUCCESS:
- print("is_GetSensorInfo ERROR")
- '''
- nRet = ueye.is_ResetToDefault(hCam)
- if nRet != ueye.IS_SUCCESS:
- print("is_ResetToDefault ERROR")
- '''
- # Set display mode to DIB
- nRet = ueye.is_SetDisplayMode(hCam, ueye.IS_SET_DM_DIB)
- nRet = ueye.is_SetColorMode(hCam,ueye.IS_CM_BGR8_PACKED)
- if nRet != ueye.IS_SUCCESS:
- print("is_SetColorMode ERROR")
- # Set the right color mode
- if int.from_bytes(sensor_info.nColorMode.value, byteorder='big') == ueye.IS_COLORMODE_BAYER:
- # setup the color depth to the current windows setting
- ueye.is_GetColorDepth(hCam, nBitsPerPixel, m_nColorMode)
- bytes_per_pixel = int(nBitsPerPixel / 8)
- print("IS_COLORMODE_BAYER: ", )
- print("\tm_nColorMode:", m_nColorMode)
- print("\tnBitsPerPixel:", nBitsPerPixel)
- print("\tbytes_per_pixel:", bytes_per_pixel)
- elif int.from_bytes(sensor_info.nColorMode.value, byteorder='big') == ueye.IS_COLORMODE_CBYCRY:
- # for color camera models use RGB32 mode
- m_nColorMode = ueye.IS_CM_BGRA8_PACKED
- nBitsPerPixel = ueye.INT(32)
- bytes_per_pixel = int(nBitsPerPixel / 8)
- print("IS_COLORMODE_CBYCRY: ", )
- print("\tm_nColorMode: \t\t", m_nColorMode)
- print("\tnBitsPerPixel: \t\t", nBitsPerPixel)
- print("\tbytes_per_pixel: \t\t", bytes_per_pixel)
- elif int.from_bytes(sensor_info.nColorMode.value, byteorder='big') == ueye.IS_COLORMODE_MONOCHROME:
- # for color camera models use RGB32 mode
- m_nColorMode = ueye.IS_CM_MONO8
- nBitsPerPixel = ueye.INT(8)
- bytes_per_pixel = int(nBitsPerPixel / 8)
- print("IS_COLORMODE_MONOCHROME: ", )
- print("\tm_nColorMode: \t\t", m_nColorMode)
- print("\tnBitsPerPixel: \t\t", nBitsPerPixel)
- print("\tbytes_per_pixel: \t\t", bytes_per_pixel)
- else:
- # for monochrome camera models use Y8 mode
- m_nColorMode = ueye.IS_CM_MONO8
- nBitsPerPixel = ueye.INT(8)
- bytes_per_pixel = int(nBitsPerPixel / 8)
- print("else")
- '''
- nRet = ueye.is_AOI(hCam, ueye.IS_AOI_IMAGE_SET_AOI, rectAOI, ueye.sizeof(rectAOI))
- if nRet != ueye.IS_SUCCESS:
- print("is_AOI ERROR")
- '''
- # Can be used to set the size and position of an "area of interest"(AOI) within an image
- nRet = ueye.is_AOI(hCam, ueye.IS_AOI_IMAGE_GET_AOI, rectAOI, ueye.sizeof(rectAOI))
- if nRet != ueye.IS_SUCCESS:
- print("is_AOI ERROR")
- width = rectAOI.s32Width
- height = rectAOI.s32Height
- # Prints out some information about the camera and the sensor
- print("Exposure value : {}".format(exposure_value))
- print("Maximum image width:", width)
- print("Maximum image height:", height)
- # ---------------------------------------------------------------------------------------------------------------------------------------
- # Allocates an image memory for an image having its dimensions defined by width and height and its color depth defined by nBitsPerPixel
- nRet = ueye.is_AllocImageMem(hCam, width, height, nBitsPerPixel, pcImageMemory, MemID)
- if nRet != ueye.IS_SUCCESS:
- print("is_AllocImageMem ERROR")
- else:
- # Makes the specified image memory the active memory
- nRet = ueye.is_SetImageMem(hCam, pcImageMemory, MemID)
- if nRet != ueye.IS_SUCCESS:
- print("is_SetImageMem ERROR")
- else:
- # Set the desired color mode
- nRet = ueye.is_SetColorMode(hCam, m_nColorMode)
- # Activates the camera's live video mode (free run mode)
- nRet = ueye.is_CaptureVideo(hCam, ueye.IS_DONT_WAIT)
- if nRet != ueye.IS_SUCCESS:
- print("is_CaptureVideo ERROR")
- # Enables the queue mode for existing image memory sequences
- nRet = ueye.is_InquireImageMem(hCam, pcImageMemory, MemID, width, height, nBitsPerPixel, pitch)
- if nRet != ueye.IS_SUCCESS:
- print("is_InquireImageMem ERROR")
- else:
- print("Press q to leave the programm")
- # ---------------------------------------------------------------------------------------------------------------------------------------
- i = 0
- f = 0
- # Continuous image display
- while True:
- point_color = (255,255,255)#BGR
- # In order to display the image in an OpenCV window we need to...
- # ...extract the data of our image memory
- array = ueye.get_data(pcImageMemory, width, height, nBitsPerPixel, pitch, copy=False)
- # bytes_per_pixel = int(nBitsPerPixel / 8)
- # ...reshape it in an numpy array...
- frame = np.reshape(array, (height.value, width.value, bytes_per_pixel))
- #print(frame.shape)
- #cv2.rectangle(frame, (1072, 92), (1213, 226), point_color, 1)
- roi5 = frame[527:601, 1247:1330]
- cv2.imshow('roi5', roi5)
- print("roi1 Height&width:", str(roi5.shape))
- w = roi5.shape[0]
- h = roi5.shape[1]
- x = 0
- y = 0
- i = 0
- while True:
- i = i + 1
- (b, g, r,a) = roi5[x, y] # 读取(0,0)像素,Python中图像像素是按B,G,R顺序存储的
- print(i,"當前位置像素 -- 红:%d,绿:%d,蓝:%d" % (r, g, b)) # 显示像素值
- # print(r)
- # print(x, y)
- x = x + 1
- if x == w:
- y = y + 1
- x = 0
- if y == h:
- break
- #roi1 = frame[92:226, 1072:1213]
- #cv2.imshow('roi1', roi1)
- #cv2.rectangle(frame, (347, 234), (497, 363), point_color, 1)
- #roi2 = frame[234:363, 347:497]
- #cv2.imshow('roi2', roi2)
- #cv2.rectangle(frame, (835, 216), (1011, 362), point_color, 1)
- #roi3 = frame[216:362, 835:1011]
- #cv2.imshow('roi3', roi3)
- #cv2.rectangle(frame, (1311, 205), (1438, 356), point_color, 1)
- #roi4 = frame[205:356, 1311:1438]
- #cv2.imshow('roi4', roi4)
- #cv2.imshow("SimpleLive_Python_uEye_OpenCV", frame)
- trigger_status = ueye.is_SetExternalTrigger(hCam,ueye.IS_GET_TRIGGER_STATUS)
- status_new = trigger_status
- print(status_old, status_new)
- # start = datetime.datetime.now()
- if status_old == 1 and status_new == 0:
- start = datetime.datetime.now()
- '''
- cv2.imwrite('C:\\Users\\User\\Desktop\\IDS\\test1.png', roi1)
- image1 = cv2.imread("C:\\Users\\User\\Desktop\\IDS\\test1.png") # 读取图像
- cv2.imwrite('C:\\Users\\User\\Desktop\\IDS\\test2.png', roi2)
- image2 = cv2.imread("C:\\Users\\User\\Desktop\\IDS\\test2.png")
- cv2.imwrite('C:\\Users\\User\\Desktop\\IDS\\test3.png', roi3)
- image3 = cv2.imread("C:\\Users\\User\\Desktop\\IDS\\test3.png")
- cv2.imwrite('C:\\Users\\User\\Desktop\\IDS\\test4.png', roi4)
- image4 = cv2.imread("C:\\Users\\User\\Desktop\\IDS\\test4.png")
- (B1, G1, R1) = cv2.split(image1) # 提取R、G、B分量
- (B2, G2, R2) = cv2.split(image2)
- (B3, G3, R3) = cv2.split(image3)
- (B4, G4, R4) = cv2.split(image4)
- print("image1 Height&width:", str(image1.shape))
- print("image2 Height&width:", str(image2.shape))
- print("image3 Height&width:", str(image3.shape))
- print("image4 Height&width:", str(image4.shape))
- w1 = image1.shape[0]
- h1 = image1.shape[1]
- x1 = 0
- y1 = 0
- i1 = 0
- while True:
- i1 = i1 + 1
- (b, g, r) = image1[x1, y1] # 读取(0,0)像素,Python中图像像素是按B,G,R顺序存储的
- #print(i,"當前位置像素 -- 红:%d,绿:%d,蓝:%d" % (r, g, b)) # 显示像素值
- #print(r)
- # print(x, y)
- x1 = x1 + 1
- if x1 == w1:
- y1 = y1 + 1
- x1 = 0
- if y1 == h1:
- break
- w2 = image2.shape[0]
- h2 = image2.shape[1]
- x2 = 0
- y2 = 0
- i2 = 0
- while True:
- i2 = i2 + 1
- (b, g, r) = image2[x2, y2] # 读取(0,0)像素,Python中图像像素是按B,G,R顺序存储的
- # print(i,"當前位置像素 -- 红:%d,绿:%d,蓝:%d" % (r, g, b)) # 显示像素值
- # print(r)
- # print(x, y)
- x2 = x2 + 1
- if x2 == w2:
- y2 = y2 + 1
- x2 = 0
- if y2 == h2:
- break
- w3 = image3.shape[0]
- h3 = image3.shape[1]
- x3 = 0
- y3 = 0
- i3 = 0
- while True:
- i3 = i3 + 1
- (b, g, r) = image3[x3, y3] # 读取(0,0)像素,Python中图像像素是按B,G,R顺序存储的
- # print(i,"當前位置像素 -- 红:%d,绿:%d,蓝:%d" % (r, g, b)) # 显示像素值
- # print(r)
- # print(x, y)
- x3 = x3 + 1
- if x3 == w3:
- y3 = y3 + 1
- x3 = 0
- if y3 == h3:
- break
- w4 = image4.shape[0]
- h4 = image4.shape[1]
- x4 = 0
- y4 = 0
- i4 = 0
- while True:
- i4 = i4 + 1
- (b, g, r) = image4[x4, y4] # 读取(0,0)像素,Python中图像像素是按B,G,R顺序存储的
- # print(i,"當前位置像素 -- 红:%d,绿:%d,蓝:%d" % (r, g, b)) # 显示像素值
- # print(r)
- # print(x, y)
- x4 = x4 + 1
- if x4 == w4:
- y4 = y4 + 1
- x4 = 0
- if y4 == h4:
- break
- '''
- end = datetime.datetime.now()
- print("執行時間:", end - start)
- '''
- for i in range(25):
- array = ueye.get_data(pcImageMemory, width, height, nBitsPerPixel, pitch, copy=False)
- global frame1
- frame1 = np.reshape(array, (height.value, width.value, bytes_per_pixel))
- for i in range(25):
- f = f+1
- FileParams = ueye.IMAGE_FILE_PARAMS()
- FileParams.pwchFileName = "C:/Users/User/Desktop/IDS/p/get_bean3/" + str(f) + ".png"
- FileParams.nFileType = ueye.IS_IMG_PNG
- FileParams.ppcImageMem = None
- FileParams.pnImageID = None
- FileParams.nQuality = 75
- nRet = ueye.is_ImageFile(hCam, ueye.IS_IMAGE_FILE_CMD_SAVE, FileParams, ueye.sizeof(FileParams))
- print('take photo')
- '''
- '''
- start1 = datetime.datetime.now()
-
- array = ueye.get_data(pcImageMemory, width, height, nBitsPerPixel, pitch, copy=False)
- global frame1
- frame1= np.reshape(array, (height.value, width.value, bytes_per_pixel))
- i = i+1
- FileParams = ueye.IMAGE_FILE_PARAMS()
- FileParams.pwchFileName = "C:/Users/User/Desktop/IDS/p/20210304-1/" + str(i)+ ".png"
- FileParams.nFileType = ueye.IS_IMG_PNG
- FileParams.ppcImageMem = None
- FileParams.pnImageID = None
- FileParams.nQuality = 10
- nRet = ueye.is_ImageFile(hCam, ueye.IS_IMAGE_FILE_CMD_SAVE, FileParams, ueye.sizeof(FileParams))
- print('take photo')
- end = datetime.datetime.now()
- print("執行時間:", end - start)
- end1 = datetime.datetime.now()
- print("執行時間:", end1 - start1)
- if f == 1000:
- break
- status_old = status_new
- '''
- # ...resize the image by a half #frame後面的(800,600)可以自行調整成想要的大小
- #frame = cv2.resize(frame, (800, 600), fx=0.5, fy=0.5)
- #nRet = ueye.is_IO(hCam, ueye.IS_IO_CMD_GPIOS_GET_CONFIGURATION, gpio, ueye.sizeof(gpio))
- #cv2.imshow("SimpleLive_Python_uEye_OpenCV", frame)
- # Press q if you want to end the loop
- if cv2.waitKey(1) & 0xFF == ord('q'):
- break
- # ---------------------------------------------------------------------------------------------------------------------------------------
- # Releases an image memory that was allocated using is_AllocImageMem() and removes it from the driver management
- ueye.is_FreeImageMem(hCam, pcImageMemory, MemID)
- # Disables the hCam camera handle and releases the data structures and memory areas taken up by the uEye camera
- ueye.is_ExitCamera(hCam)
- # Destroys the OpenCv windows
- cv2.destroyAllWindows()
|