第一次提交
1、hpi的可用代码; 2、修复了多次点击曝光后,福亮度数据错误的问题; 3、定标方式为大的蓝菲积分球的标准能量曲线,而不是基于asd的能量曲线;
This commit is contained in:
230
record_system_v11/1record_system_v1.1.py
Normal file
230
record_system_v11/1record_system_v1.1.py
Normal file
@ -0,0 +1,230 @@
|
||||
from ximea import xiapi
|
||||
import cv2
|
||||
import numpy as np
|
||||
import matplotlib.pyplot as plt
|
||||
from osgeo import gdal #读写影像数据
|
||||
import os, sys, traceback
|
||||
import datetime # 几种Python执行时间的计算方法:https://blog.csdn.net/wangshuang1631/article/details/54286551
|
||||
|
||||
#create instance for first connected camera
|
||||
cam = xiapi.Camera()
|
||||
|
||||
#start communication to open specific device, use: cam.open_device_by_SN('41305651')
|
||||
print('Opening first camera...')
|
||||
cam.open_device()
|
||||
|
||||
# 打开相机后,显示相机信息
|
||||
print('SN: %s'% str(cam.get_device_sn(), encoding = "utf-8"))
|
||||
print('Device name: %s'% str(cam.get_device_name(), encoding = "utf-8"))
|
||||
print('Device name: %s'% str(cam.get_device_type(), encoding = "utf-8"))
|
||||
print('Instance path: %s'% str(cam.get_device_inst_path(), encoding = "utf-8")) # Returns device instance path in operating system.
|
||||
print('Location path: %s'% str(cam.get_device_loc_path(), encoding = "utf-8"))
|
||||
|
||||
# debug_level、线程数和horizontal flip
|
||||
print('Debug level: %s' % cam.get_debug_level())
|
||||
print('Default number of threads per image processor: %d' % cam.get_proc_num_threads())
|
||||
cam.set_proc_num_threads(8)
|
||||
print('Current number of threads per image processor: %d' % cam.get_proc_num_threads())
|
||||
print('Is horizontal flip enabled?, %s' % str(cam.is_horizontal_flip()))
|
||||
|
||||
|
||||
#采集模式:This mode is supported by selected camera families: CB, MC, MT, MX
|
||||
cam.set_acq_timing_mode('XI_ACQ_TIMING_MODE_FRAME_RATE_LIMIT')
|
||||
mode_used = cam.get_acq_timing_mode()
|
||||
if mode_used == 'XI_ACQ_TIMING_MODE_FRAME_RATE_LIMIT':
|
||||
print('Mode is XI_ACQ_TIMING_MODE_FRAME_RATE_LIMIT')
|
||||
else:
|
||||
print('Mode is not XI_ACQ_TIMING_MODE_FRAME_RATE_LIMIT')
|
||||
sys.exit()
|
||||
|
||||
#settings,cam.set_param("exposure",10000)
|
||||
exposure = 50
|
||||
framerate = 2
|
||||
cam.set_exposure(exposure)#曝光时间单位为微秒,1s共有1000000微秒
|
||||
cam.set_framerate(framerate)
|
||||
print('Exposure was set to %i us' % cam.get_exposure())
|
||||
print('Framerate was set to %i FPS' % cam.get_framerate())
|
||||
|
||||
# # 设置binning参数
|
||||
# # XI_BIN_MODE_SUM(默认): The response from the combined pixels will be added, resulting in increased sensitivity.
|
||||
# # XI_BIN_MODE_AVERAGE: The response from the combined pixels will be averaged, resulting in increased signal/noise ratio.
|
||||
bin_spec = 2
|
||||
bin_spatial = 2
|
||||
|
||||
cam.set_binning_selector('XI_BIN_SELECT_HOST_CPU') # 默认为XI_BIN_SELECT_SENSOR(会报错),不可用:XI_BIN_SELECT_DEVICE_FPGA
|
||||
cam.set_binning_horizontal_mode('XI_BIN_MODE_SUM')
|
||||
cam.set_binning_horizontal(bin_spatial)
|
||||
cam.set_binning_vertical_mode('XI_BIN_MODE_SUM')
|
||||
cam.set_binning_vertical(bin_spec)
|
||||
|
||||
|
||||
# cam.get_width_increment() -->16
|
||||
# cam.get_height_increment() -->2
|
||||
# cam.get_offsetX_increment() -->16
|
||||
# cam.get_offsetY_increment() -->2
|
||||
# 设置有效输出窗口
|
||||
|
||||
if bin_spatial == 1:
|
||||
cam.set_width(1392)
|
||||
cam.set_offsetX(272)
|
||||
|
||||
cam.set_height(302)
|
||||
cam.set_offsetY(338)
|
||||
elif bin_spatial == 2:
|
||||
cam.set_width(696)
|
||||
cam.set_offsetX(128)
|
||||
|
||||
cam.set_height(151)
|
||||
cam.set_offsetY(168)
|
||||
|
||||
|
||||
# 创建img = xiapi.Image()前需要设置一系列img参数,例如:格式、位深度、
|
||||
# 如果格式设置为XI_RAW8,image_raw_numpy.dtype -> dtype('uint8'), uint8
|
||||
# 如果格式设置为XI_RAW16,image_raw_numpy.dtype -> dtype('<u2'), uint16
|
||||
cam.set_imgdataformat('XI_RAW16')
|
||||
cam.set_output_bit_depth("XI_BPP_16")
|
||||
# cam.set_image_data_bit_depth("XI_BPP_16")
|
||||
# cam.set_sensor_bit_depth("XI_BPP_16") # 报错
|
||||
# cam.get_buffer_policy()
|
||||
|
||||
# create instance of Image to store image data and metadata
|
||||
img = xiapi.Image()
|
||||
|
||||
# start data acquisition
|
||||
print('Starting data acquisition...')
|
||||
starttime = datetime.datetime.now()
|
||||
|
||||
framenumber = 10
|
||||
image_container = np.empty((int(300 / bin_spec), framenumber, int(1364 / bin_spatial)))
|
||||
|
||||
if bin_spec == 1:
|
||||
startRow = 1
|
||||
endRow = 301
|
||||
startColumn = 12
|
||||
endColumn = 1376
|
||||
if bin_spec == 2:
|
||||
startRow = 169
|
||||
endRow = 319
|
||||
startColumn = 143
|
||||
endColumn = 824
|
||||
if bin_spec == 4:
|
||||
startRow = 84
|
||||
endRow = 159
|
||||
startColumn = 71
|
||||
endColumn = 412
|
||||
|
||||
|
||||
|
||||
# cam.set_framerate(int(framerate))
|
||||
# cam.set_aeag_roi_offset_x(startColumn)
|
||||
# cam.set_aeag_roi_offset_y(startRow)
|
||||
# cam.set_aeag_roi_height(endRow - startRow)
|
||||
# cam.set_aeag_roi_width(endColumn - startColumn)
|
||||
# cam.enable_aeag() # 开启自动曝光
|
||||
# cam.start_acquisition()
|
||||
# for i in range(10):
|
||||
# cam.get_image(img) # get data and pass them from camera to img
|
||||
# cam.stop_acquisition()
|
||||
# cam.disable_aeag() # 关闭自动曝光
|
||||
|
||||
print('Framerate was set to %i FPS' % cam.get_framerate())
|
||||
print('Exposure was set to %i us' % cam.get_exposure())
|
||||
print('Gain was set to %i dB' % cam.get_gain())
|
||||
|
||||
|
||||
cam.start_acquisition()
|
||||
for i in range(framenumber):
|
||||
#get data and pass them from camera to img
|
||||
cam.get_image(img)
|
||||
|
||||
#get raw data from camera
|
||||
#for Python2.x function returns string
|
||||
#for Python3.x function returns bytes
|
||||
# data_raw = img.get_image_data_raw()
|
||||
# print("第%d帧"% i)
|
||||
# image_raw_numpy.dtype -> dtype('<u2'), uint16
|
||||
# cornning 410的位深度为12,所以尽管uint16能表达16位的数据,但是相机返回的数据最大值依然为4095
|
||||
image_raw_numpy = img.get_image_data_numpy()
|
||||
print()
|
||||
|
||||
try:
|
||||
if bin_spec == 1:
|
||||
image_container[:, i, :] = image_raw_numpy[startRow:endRow, startColumn:endColumn]
|
||||
if bin_spec == 2:
|
||||
image_container[:, i, :] = image_raw_numpy[startRow:endRow, startColumn:endColumn]
|
||||
if bin_spec == 4:
|
||||
image_container[:, i, :] = image_raw_numpy[startRow:endRow, startColumn:endColumn]
|
||||
except Exception:
|
||||
traceback.print_exc()
|
||||
|
||||
|
||||
# if i == 0:
|
||||
# image_container[:, 0, :] = image_raw_numpy[340:640, 285:1650]
|
||||
# elif i != 0:
|
||||
# image_container = np.concatenate((image_container, image_raw_numpy[340:640, 285:1650].reshape(300, 1, 1365)), axis=1)
|
||||
|
||||
print(i)
|
||||
# print(cam.get_framerate())
|
||||
|
||||
#stop data acquisition
|
||||
print('Stopping acquisition...')
|
||||
cam.stop_acquisition()
|
||||
endtime = datetime.datetime.now()
|
||||
print('影像采集用时:%d'%(endtime - starttime).seconds)
|
||||
|
||||
#stop communication
|
||||
cam.close_device()
|
||||
|
||||
|
||||
class GRID:
|
||||
|
||||
#读图像文件
|
||||
def read_img(self, filename):
|
||||
try:
|
||||
dataset = gdal.Open(filename) # 打开文件
|
||||
im_width = dataset.RasterXSize # 栅格矩阵的列数
|
||||
im_height = dataset.RasterYSize # 栅格矩阵的行数
|
||||
num_bands = dataset.RasterCount # 栅格矩阵的波段数
|
||||
im_geotrans = dataset.GetGeoTransform() # 仿射矩阵
|
||||
im_proj = dataset.GetProjection() # 地图投影信息
|
||||
im_data = dataset.ReadAsArray(0, 0, im_width, im_height) # 将数据写成数组,对应栅格矩阵
|
||||
del dataset
|
||||
return im_proj, im_geotrans, im_data
|
||||
except:
|
||||
sys.exit()
|
||||
|
||||
#写文件,以写成tif为例
|
||||
def write_img(self, dst_filename, data):
|
||||
format = "ENVI"
|
||||
driver = gdal.GetDriverByName(format)
|
||||
RasterXSize = data.shape[2] # 遥感影像的sample(列数)
|
||||
RasterYSize = data.shape[1] # 遥感影像的line(行数)
|
||||
band = data.shape[0]
|
||||
# driver.Create()函数中RasterXSize代表影像的sample(列数),RasterYSize代表影像的line(行数)
|
||||
dst_ds = driver.Create(dst_filename, RasterXSize, RasterYSize, band, gdal.GDT_Float32)
|
||||
for i in range(band):
|
||||
dst_ds.GetRasterBand(i + 1).WriteArray(data[i, :, :]) # gdal的band从1开始,所以dst_ds.GetRasterBand(i+1)
|
||||
dst_ds = None
|
||||
|
||||
starttime_xie = datetime.datetime.now()
|
||||
# dark_
|
||||
im_driver = GRID()
|
||||
# im_driver.write_img('jangnandaxue-焦距22' + str(exposure)+'_'+str(framerate)+'_'+str(34), image_container)
|
||||
im_driver.write_img('ar-ren-bin2a---------------------', image_container)
|
||||
endtime_xie = datetime.datetime.now()
|
||||
print('影像写入磁盘用时:%d'%(endtime_xie - starttime_xie).seconds)
|
||||
|
||||
|
||||
print('计算波长')
|
||||
def calculate_wavelength(x):
|
||||
wavelength = x*1.999564 - 279.893
|
||||
return wavelength
|
||||
wavelength = np.empty(300)
|
||||
for i in range(340, 640):
|
||||
wavelength[i-340] = calculate_wavelength(i)
|
||||
|
||||
|
||||
plt.imshow(image_container[100, :, :])
|
||||
plt.show()
|
||||
|
||||
print('Done.')
|
Reference in New Issue
Block a user