-
Notifications
You must be signed in to change notification settings - Fork 0
/
CamOperation_class.py
471 lines (423 loc) · 23.6 KB
/
CamOperation_class.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
# -- coding: utf-8 --
import sys
import threading
import msvcrt
# import _tkinter
# import tkinter.messagebox
# import tkinter as tk
import numpy as np
import cv2
import time
import sys, os
import datetime
import inspect
import ctypes
import random
from ctypes import *
# from tkinter import ttk
import queue
# sys.path.append("../MvImport")
from MvImport.MvCameraControl_class import *
# from MvCameraControl_class import *
def Async_raise(tid, exctype):
tid = ctypes.c_long(tid)
if not inspect.isclass(exctype):
exctype = type(exctype)
res = ctypes.pythonapi.PyThreadState_SetAsyncExc(tid, ctypes.py_object(exctype))
if res == 0:
raise ValueError("invalid thread id")
elif res != 1:
ctypes.pythonapi.PyThreadState_SetAsyncExc(tid, None)
raise SystemError("PyThreadState_SetAsyncExc failed")
def Stop_thread(thread):
Async_raise(thread.ident, SystemExit)
class CameraOperation():
def __init__(self,obj_cam,st_device_list,n_connect_num=0,b_open_device=False,b_start_grabbing = False,h_thread_handle=None,\
b_thread_closed=False,st_frame_info=None,buf_cache=None,b_exit=False,b_save_bmp=False,save_path=None,save_name=None,b_save_jpg=False,buf_save_image=None,\
n_save_image_size=0,n_payload_size=0,n_win_gui_id=0,frame_rate=0,exposure_time=0,gain=0):
self.obj_cam = obj_cam
self.st_device_list = st_device_list
self.n_connect_num = n_connect_num
self.b_open_device = b_open_device
self.b_start_grabbing = b_start_grabbing
self.b_thread_closed = b_thread_closed
self.st_frame_info = st_frame_info
self.buf_cache = buf_cache
self.b_exit = b_exit
self.b_save_bmp = b_save_bmp
self.save_path = save_path
self.save_name = save_name
self.b_save_jpg = b_save_jpg
self.n_payload_size = n_payload_size
self.buf_save_image = buf_save_image
self.h_thread_handle = h_thread_handle
self.n_win_gui_id = n_win_gui_id
self.n_save_image_size = n_save_image_size
self.b_thread_closed
self.frame_rate = frame_rate
self.exposure_time = exposure_time
self.gain = gain
self.q = queue.Queue(10)
def To_hex_str(self,num):
chaDic = {10: 'a', 11: 'b', 12: 'c', 13: 'd', 14: 'e', 15: 'f'}
hexStr = ""
if num < 0:
num = num + 2**32
while num >= 16:
digit = num % 16
hexStr = chaDic.get(digit, str(digit)) + hexStr
num //= 16
hexStr = chaDic.get(num, str(num)) + hexStr
return hexStr
def Open_device(self):
if False == self.b_open_device:
# ch:選擇設備並創建句柄 | en:Select device and create handle
nConnectionNum = int(self.n_connect_num)
stDeviceList = cast(self.st_device_list.pDeviceInfo[int(nConnectionNum)], POINTER(MV_CC_DEVICE_INFO)).contents
self.obj_cam = MvCamera()
ret = self.obj_cam.MV_CC_CreateHandle(stDeviceList)
if ret != 0:
self.obj_cam.MV_CC_DestroyHandle()
# tkinter.messagebox.showerror('show error','create handle fail! ret = '+ self.To_hex_str(ret))
return ret
ret = self.obj_cam.MV_CC_OpenDevice(MV_ACCESS_Exclusive, 0)
if ret != 0:
# tkinter.messagebox.showerror('show error','open device fail! ret = '+ self.To_hex_str(ret))
return ret
print ("open device successfully!")
self.b_open_device = True
self.b_thread_closed = False
# ch:探測網絡最佳包大小(隻對GigE相機有效) | en:Detection network optimal package size(It only works for the GigE camera)
if stDeviceList.nTLayerType == MV_GIGE_DEVICE:
nPacketSize = self.obj_cam.MV_CC_GetOptimalPacketSize()
if int(nPacketSize) > 0:
ret = self.obj_cam.MV_CC_SetIntValue("GevSCPSPacketSize",nPacketSize)
if ret != 0:
print ("warning: set packet size fail! ret[0x%x]" % ret)
else:
print ("warning: set packet size fail! ret[0x%x]" % nPacketSize)
stBool = c_bool(False)
ret =self.obj_cam.MV_CC_GetBoolValue("AcquisitionFrameRateEnable", byref(stBool))
if ret != 0:
print ("get acquisition frame rate enable fail! ret[0x%x]" % ret)
stParam = MVCC_INTVALUE()
memset(byref(stParam), 0, sizeof(MVCC_INTVALUE))
ret = self.obj_cam.MV_CC_GetIntValue("PayloadSize", stParam)
if ret != 0:
print ("get payload size fail! ret[0x%x]" % ret)
self.n_payload_size = stParam.nCurValue
if None == self.buf_cache:
self.buf_cache = (c_ubyte * self.n_payload_size)()
# ch:設置觸發模式為off | en:Set trigger mode as off
ret = self.obj_cam.MV_CC_SetEnumValue("TriggerMode", MV_TRIGGER_MODE_OFF)
if ret != 0:
print ("set trigger mode fail! ret[0x%x]" % ret)
return 0
def Start_grabbing(self):
if False == self.b_start_grabbing and True == self.b_open_device:
self.b_exit = False
ret = self.obj_cam.MV_CC_StartGrabbing()
if ret != 0:
print('start grabbing fail! ret = ', str(ret))
# tkinter.messagebox.showerror('show error','start grabbing fail! ret = '+ self.To_hex_str(ret))
return
self.b_start_grabbing = True
print ("start grabbing successfully!")
try:
self.n_win_gui_id = random.randint(1,10000)
self.h_thread_handle = threading.Thread(target=CameraOperation.Work_thread, args=(self,))
self.h_thread_handle.setDaemon(True)
self.h_thread_handle.start()
self.b_thread_closed = True
except:
# tkinter.messagebox.showerror('show error','error: unable to start thread')
False == self.b_start_grabbing
def get_img_nummpy(self):
if self.q.empty():
numArray = None
else:
numArray = self.q.get()
return numArray
def Stop_grabbing(self):
if True == self.b_start_grabbing and self.b_open_device == True:
# 退出線程
if True == self.b_thread_closed:
Stop_thread(self.h_thread_handle)
self.b_thread_closed = False
ret = self.obj_cam.MV_CC_StopGrabbing()
if ret != 0:
# tkinter.messagebox.showerror('show error','stop grabbing fail! ret = '+self.To_hex_str(ret))
return
print ("stop grabbing successfully!")
self.b_start_grabbing = False
self.b_exit = True
def Close_device(self):
if True == self.b_open_device:
#退出線程
if True == self.b_thread_closed:
Stop_thread(self.h_thread_handle)
self.b_thread_closed = False
ret = self.obj_cam.MV_CC_CloseDevice()
if ret != 0:
# tkinter.messagebox.showerror('show error','close deivce fail! ret = '+self.To_hex_str(ret))
return
# ch:銷毀句柄 | Destroy handle
self.obj_cam.MV_CC_DestroyHandle()
self.b_open_device = False
self.b_start_grabbing = False
self.b_exit = True
print ("close device successfully!")
def Set_trigger_mode(self,strMode):
if True == self.b_open_device:
if "continuous" == strMode:
ret = self.obj_cam.MV_CC_SetEnumValue("TriggerMode",0)
if ret != 0:
pass
# tkinter.messagebox.showerror('show error','set triggermode fail! ret = '+self.To_hex_str(ret))
if "triggermode" == strMode:
ret = self.obj_cam.MV_CC_SetEnumValue("TriggerMode",1)
if ret != 0:
pass
# tkinter.messagebox.showerror('show error','set triggermode fail! ret = '+self.To_hex_str(ret))
ret = self.obj_cam.MV_CC_SetEnumValue("TriggerSource",7)
if ret != 0:
pass
# tkinter.messagebox.showerror('show error','set triggersource fail! ret = '+self.To_hex_str(ret))
def Trigger_once(self,nCommand):
if True == self.b_open_device:
if 1 == nCommand:
ret = self.obj_cam.MV_CC_SetCommandValue("TriggerSoftware")
if ret != 0:
pass
# tkinter.messagebox.showerror('show error','set triggersoftware fail! ret = '+self.To_hex_str(ret))
def Get_parameter(self):
if True == self.b_open_device:
stFloatParam_FrameRate = MVCC_FLOATVALUE()
memset(byref(stFloatParam_FrameRate), 0, sizeof(MVCC_FLOATVALUE))
stFloatParam_exposureTime = MVCC_FLOATVALUE()
memset(byref(stFloatParam_exposureTime), 0, sizeof(MVCC_FLOATVALUE))
stFloatParam_gain = MVCC_FLOATVALUE()
memset(byref(stFloatParam_gain), 0, sizeof(MVCC_FLOATVALUE))
ret = self.obj_cam.MV_CC_GetFloatValue("AcquisitionFrameRate", stFloatParam_FrameRate)
if ret != 0:
pass
# tkinter.messagebox.showerror('show error','get acquistion frame rate fail! ret = '+self.To_hex_str(ret))
self.frame_rate = stFloatParam_FrameRate.fCurValue
ret = self.obj_cam.MV_CC_GetFloatValue("ExposureTime", stFloatParam_exposureTime)
if ret != 0:
pass
# tkinter.messagebox.showerror('show error','get exposure time fail! ret = '+self.To_hex_str(ret))
self.exposure_time = stFloatParam_exposureTime.fCurValue
ret = self.obj_cam.MV_CC_GetFloatValue("Gain", stFloatParam_gain)
if ret != 0:
pass
# tkinter.messagebox.showerror('show error','get gain fail! ret = '+self.To_hex_str(ret))
self.gain = stFloatParam_gain.fCurValue
# tkinter.messagebox.showinfo('show info','get parameter success!')
def Set_parameter(self,frameRate,exposureTime,gain):
if '' == frameRate or '' == exposureTime or '' == gain:
# tkinter.messagebox.showinfo('show info','please type in the text box !')
return
if True == self.b_open_device:
ret = self.obj_cam.MV_CC_SetFloatValue("ExposureTime",float(exposureTime))
if ret != 0:
pass
# tkinter.messagebox.showerror('show error','set exposure time fail! ret = '+self.To_hex_str(ret))
ret = self.obj_cam.MV_CC_SetFloatValue("Gain",float(gain))
if ret != 0:
pass
# tkinter.messagebox.showerror('show error','set gain fail! ret = '+self.To_hex_str(ret))
ret = self.obj_cam.MV_CC_SetFloatValue("AcquisitionFrameRate",float(frameRate))
if ret != 0:
pass
# tkinter.messagebox.showerror('show error','set acquistion frame rate fail! ret = '+self.To_hex_str(ret))
# tkinter.messagebox.showinfo('show info','set parameter success!')
def Work_thread(self):
stFrameInfo = MV_FRAME_OUT_INFO_EX()
img_buff = None
while True:
ret = self.obj_cam.MV_CC_GetOneFrameTimeout(byref(self.buf_cache), self.n_payload_size, stFrameInfo, 1000)
if ret == 0:
#獲取到圖像的時間開始節點獲取到圖像的時間開始節點
self.st_frame_info = stFrameInfo
# print ("get one frame: Width[%d], Height[%d], nFrameNum[%d]" % (self.st_frame_info.nWidth, self.st_frame_info.nHeight, self.st_frame_info.nFrameNum))
self.n_save_image_size = self.st_frame_info.nWidth * self.st_frame_info.nHeight * 3 + 2048
if img_buff is None:
img_buff = (c_ubyte * self.n_save_image_size)()
if True == self.b_save_jpg:
self.Save_jpg() #ch:保存Jpg圖片 | en:Save Jpg
if self.buf_save_image is None:
self.buf_save_image = (c_ubyte * self.n_save_image_size)()
stParam = MV_SAVE_IMAGE_PARAM_EX()
stParam.enImageType = MV_Image_Bmp; # ch:需要保存的圖像類型 | en:Image format to save
stParam.enPixelType = self.st_frame_info.enPixelType # ch:相機對應的像素格式 | en:Camera pixel type
stParam.nWidth = self.st_frame_info.nWidth # ch:相機對應的寬 | en:Width
stParam.nHeight = self.st_frame_info.nHeight # ch:相機對應的高 | en:Height
stParam.nDataLen = self.st_frame_info.nFrameLen
stParam.pData = cast(self.buf_cache, POINTER(c_ubyte))
stParam.pImageBuffer = cast(byref(self.buf_save_image), POINTER(c_ubyte))
stParam.nBufferSize = self.n_save_image_size # ch:存儲節點的大小 | en:Buffer node size
stParam.nJpgQuality = 80; # ch:jpg編碼,僅在保存Jpg圖像時有效。保存BMP時SDK內忽略該參數
if True == self.b_save_bmp:
self.Save_Bmp() #ch:保存Bmp圖片 | en:Save Bmp
else:
continue
#轉換像素結構體賦值
stConvertParam = MV_CC_PIXEL_CONVERT_PARAM()
memset(byref(stConvertParam), 0, sizeof(stConvertParam))
stConvertParam.nWidth = self.st_frame_info.nWidth
stConvertParam.nHeight = self.st_frame_info.nHeight
stConvertParam.pSrcData = self.buf_cache
stConvertParam.nSrcDataLen = self.st_frame_info.nFrameLen
stConvertParam.enSrcPixelType = self.st_frame_info.enPixelType
# Mono8直接顯示
if PixelType_Gvsp_Mono8 == self.st_frame_info.enPixelType:
numArray = CameraOperation.Mono_numpy(self,self.buf_cache,self.st_frame_info.nWidth,self.st_frame_info.nHeight)
# RGB直接顯示
elif PixelType_Gvsp_RGB8_Packed == self.st_frame_info.enPixelType:
numArray = CameraOperation.Color_numpy(self,self.buf_cache,self.st_frame_info.nWidth,self.st_frame_info.nHeight)
#如果是黑白且非Mono8則轉為Mono8
elif True == self.Is_mono_data(self.st_frame_info.enPixelType):
nConvertSize = self.st_frame_info.nWidth * self.st_frame_info.nHeight
stConvertParam.enDstPixelType = PixelType_Gvsp_Mono8
stConvertParam.pDstBuffer = (c_ubyte * nConvertSize)()
stConvertParam.nDstBufferSize = nConvertSize
ret = self.obj_cam.MV_CC_ConvertPixelType(stConvertParam)
if ret != 0:
# tkinter.messagebox.showerror('show error','convert pixel fail! ret = '+self.To_hex_str(ret))
continue
cdll.msvcrt.memcpy(byref(img_buff), stConvertParam.pDstBuffer, nConvertSize)
numArray = CameraOperation.Mono_numpy(self,img_buff,self.st_frame_info.nWidth,self.st_frame_info.nHeight)
#如果是彩色且非RGB則轉為RGB後顯示
elif True == self.Is_color_data(self.st_frame_info.enPixelType):
nConvertSize = self.st_frame_info.nWidth * self.st_frame_info.nHeight * 3
stConvertParam.enDstPixelType = PixelType_Gvsp_RGB8_Packed
stConvertParam.pDstBuffer = (c_ubyte * nConvertSize)()
stConvertParam.nDstBufferSize = nConvertSize
ret = self.obj_cam.MV_CC_ConvertPixelType(stConvertParam)
if ret != 0:
# tkinter.messagebox.showerror('show error','convert pixel fail! ret = '+self.To_hex_str(ret))
continue
cdll.msvcrt.memcpy(byref(img_buff), stConvertParam.pDstBuffer, nConvertSize)
numArray = CameraOperation.Color_numpy(self,img_buff,self.st_frame_info.nWidth,self.st_frame_info.nHeight)
self.q.put(numArray.copy())
if self.b_exit == True:
cv2.destroyAllWindows()
if img_buff is not None:
del img_buff
if self.buf_cache is not None:
del self.buf_cache
break
def Save_jpg(self):
if(None == self.buf_cache):
return
self.buf_save_image = None
file_path = str(self.st_frame_info.nFrameNum) + ".jpg"
self.n_save_image_size = self.st_frame_info.nWidth * self.st_frame_info.nHeight * 3 + 2048
if self.buf_save_image is None:
self.buf_save_image = (c_ubyte * self.n_save_image_size)()
stParam = MV_SAVE_IMAGE_PARAM_EX()
stParam.enImageType = MV_Image_Jpeg; # ch:需要保存的圖像類型 | en:Image format to save
stParam.enPixelType = self.st_frame_info.enPixelType # ch:相機對應的像素格式 | en:Camera pixel type
stParam.nWidth = self.st_frame_info.nWidth # ch:相機對應的寬 | en:Width
stParam.nHeight = self.st_frame_info.nHeight # ch:相機對應的高 | en:Height
stParam.nDataLen = self.st_frame_info.nFrameLen
stParam.pData = cast(self.buf_cache, POINTER(c_ubyte))
stParam.pImageBuffer= cast(byref(self.buf_save_image), POINTER(c_ubyte))
stParam.nBufferSize = self.n_save_image_size # ch:存儲節點的大小 | en:Buffer node size
stParam.nJpgQuality = 80; # ch:jpg編碼,僅在保存Jpg圖像時有效。保存BMP時SDK內忽略該參數
return_code = self.obj_cam.MV_CC_SaveImageEx2(stParam)
if return_code != 0:
# tkinter.messagebox.showerror('show error','save jpg fail! ret = '+self.To_hex_str(return_code))
self.b_save_jpg = False
return
file_open = open(file_path.encode('ascii'), 'wb+')
img_buff = (c_ubyte * stParam.nImageLen)()
try:
cdll.msvcrt.memcpy(byref(img_buff), stParam.pImageBuffer, stParam.nImageLen)
file_open.write(img_buff)
self.b_save_jpg = False
# tkinter.messagebox.showinfo('show info','save bmp success!')
except:
self.b_save_jpg = False
raise Exception("get one frame failed:%s" % e.message)
if(None != img_buff):
del img_buff
def Save_Bmp(self):
if(0 == self.buf_cache):
return
self.buf_save_image = None
path = self.save_path + "\\"
bmpName = self.save_name
# bmpName = time.strftime("%Y-%m-%d_%H%M%S", time.localtime())
file_path = path + str(bmpName) + ".bmp"
self.n_save_image_size = self.st_frame_info.nWidth * self.st_frame_info.nHeight * 3 + 2048
if self.buf_save_image is None:
self.buf_save_image = (c_ubyte * self.n_save_image_size)()
stParam = MV_SAVE_IMAGE_PARAM_EX()
stParam.enImageType = MV_Image_Bmp; # ch:需要保存的圖像類型 | en:Image format to save
stParam.enPixelType = self.st_frame_info.enPixelType # ch:相機對應的像素格式 | en:Camera pixel type
stParam.nWidth = self.st_frame_info.nWidth # ch:相機對應的寬 | en:Width
stParam.nHeight = self.st_frame_info.nHeight # ch:相機對應的高 | en:Height
stParam.nDataLen = self.st_frame_info.nFrameLen
stParam.pData = cast(self.buf_cache, POINTER(c_ubyte))
stParam.pImageBuffer = cast(byref(self.buf_save_image), POINTER(c_ubyte))
stParam.nBufferSize = self.n_save_image_size # ch:存儲節點的大小 | en:Buffer node size
return_code = self.obj_cam.MV_CC_SaveImageEx2(stParam)
if return_code != 0:
# tkinter.messagebox.showerror('show error','save bmp fail! ret = '+self.To_hex_str(return_code))
self.b_save_bmp = False
return
file_open = open(file_path.encode('ascii'), 'wb+')
img_buff = (c_ubyte * stParam.nImageLen)()
try:
cdll.msvcrt.memcpy(byref(img_buff), stParam.pImageBuffer, stParam.nImageLen)
file_open.write(img_buff)
self.b_save_bmp = False
# tkinter.messagebox.showinfo('show info','save bmp success!')
except:
self.b_save_bmp = False
raise Exception("get one frame failed:%s" % e.message)
if(None != img_buff):
del img_buff
def Is_mono_data(self,enGvspPixelType):
if PixelType_Gvsp_Mono8 == enGvspPixelType or PixelType_Gvsp_Mono10 == enGvspPixelType \
or PixelType_Gvsp_Mono10_Packed == enGvspPixelType or PixelType_Gvsp_Mono12 == enGvspPixelType \
or PixelType_Gvsp_Mono12_Packed == enGvspPixelType:
return True
else:
return False
def Is_color_data(self,enGvspPixelType):
if PixelType_Gvsp_BayerGR8 == enGvspPixelType or PixelType_Gvsp_BayerRG8 == enGvspPixelType \
or PixelType_Gvsp_BayerGB8 == enGvspPixelType or PixelType_Gvsp_BayerBG8 == enGvspPixelType \
or PixelType_Gvsp_BayerGR10 == enGvspPixelType or PixelType_Gvsp_BayerRG10 == enGvspPixelType \
or PixelType_Gvsp_BayerGB10 == enGvspPixelType or PixelType_Gvsp_BayerBG10 == enGvspPixelType \
or PixelType_Gvsp_BayerGR12 == enGvspPixelType or PixelType_Gvsp_BayerRG12 == enGvspPixelType \
or PixelType_Gvsp_BayerGB12 == enGvspPixelType or PixelType_Gvsp_BayerBG12 == enGvspPixelType \
or PixelType_Gvsp_BayerGR10_Packed == enGvspPixelType or PixelType_Gvsp_BayerRG10_Packed == enGvspPixelType \
or PixelType_Gvsp_BayerGB10_Packed == enGvspPixelType or PixelType_Gvsp_BayerBG10_Packed == enGvspPixelType \
or PixelType_Gvsp_BayerGR12_Packed == enGvspPixelType or PixelType_Gvsp_BayerRG12_Packed== enGvspPixelType \
or PixelType_Gvsp_BayerGB12_Packed == enGvspPixelType or PixelType_Gvsp_BayerBG12_Packed == enGvspPixelType \
or PixelType_Gvsp_YUV422_Packed == enGvspPixelType or PixelType_Gvsp_YUV422_YUYV_Packed == enGvspPixelType:
return True
else:
return False
def Mono_numpy(self,data,nWidth,nHeight):
data_ = np.frombuffer(data, count=int(nWidth * nHeight), dtype=np.uint8, offset=0)
data_mono_arr = data_.reshape(nHeight, nWidth)
numArray = np.zeros([nHeight, nWidth, 1],"uint8")
numArray[:, :, 0] = data_mono_arr
return numArray
def Color_numpy(self,data,nWidth,nHeight):
data_ = np.frombuffer(data, count=int(nWidth*nHeight*3), dtype=np.uint8, offset=0)
data_r = data_[0:nWidth*nHeight*3:3]
data_g = data_[1:nWidth*nHeight*3:3]
data_b = data_[2:nWidth*nHeight*3:3]
data_r_arr = data_r.reshape(nHeight, nWidth)
data_g_arr = data_g.reshape(nHeight, nWidth)
data_b_arr = data_b.reshape(nHeight, nWidth)
numArray = np.zeros([nHeight, nWidth, 3],"uint8")
numArray[:, :, 2] = data_r_arr
numArray[:, :, 1] = data_g_arr
numArray[:, :, 0] = data_b_arr
return numArray