def __init__(self): print('init LI_IMX377_MIPI_M12') self.width = CAMERA_WIDTH self.height = CAMERA_HEIGHT self.cap = None self.mtx, self.dist = load_calibration() self.open()
def load_and_prepare_img(src_img_filename): src_img = cv2.imread(src_img_filename) camera_matrix, dist_coefs = load_calibration() undistorted = undistort_image(camera_matrix, dist_coefs, src_img) height, width, color_depth = undistorted.shape clipped = undistorted[10:height-10, 100:width-100] grey = cv2.cvtColor(clipped, cv2.COLOR_BGR2GRAY) return { 'original_img': src_img, 'grey_img': grey, 'filename': src_img_filename, 'basename': basename(src_img_filename) }
M_inv = cv2.getPerspectiveTransform(dst, src) # Only for creating the final video visualization X_b = [574, 706, 706, 574] Y_b = [719, 719, 0, 0] src_ = np.floor(np.float32([[x[0], y[0]], [x[1], y[1]],[x[2], y[2]], [x[3], y[3]]]) / (input_scale*2)) dst_ = np.floor(np.float32([[X_b[0], Y_b[0]], [X_b[1], Y_b[1]],[X_b[2], Y_b[2]], [X_b[3], Y_b[3]]]) / (input_scale*2)) M_b = cv2.getPerspectiveTransform(src_, dst_) # Only for creating the final video visualization # Threshold for color and gradient thresholding s_thresh, sx_thresh, dir_thresh, m_thresh, r_thresh = (120, 255), (20, 100), (0.7, 1.3), (30, 100), (200, 255) # load the calibration calib_file = 'camera_cal/calibration_pickle.p' mtx, dist = load_calibration(calib_file) def abs_sobel_thresh(img, orient='x', sobel_kernel=3, thresh=(0, 255)): # Apply the following steps to img # 1) Convert to grayscale gray = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY) # 2) Take the derivative in x or y given orient = 'x' or 'y' # 3) Take the absolute value of the derivative or gradient if orient == 'x': abs_sobel = np.absolute(cv2.Sobel(gray, cv2.CV_64F, 1, 0, ksize=sobel_kernel)) if orient == 'y': abs_sobel = np.absolute(cv2.Sobel(gray, cv2.CV_64F, 0, 1, ksize=sobel_kernel))
def process_vanadium_data(vanadium, empty_instr, lambda_binning, calibration=None, **absorp): """ Create corrected vanadium dataset Correction applied to Vanadium data only 1. Subtract empty instrument 2. Correct absorption 3. Use calibration for grouping 4. Focus into groups Parameters ---------- vanadium : Vanadium nexus datafile empty_instr: Empty instrument nexus file lambda_binning: format=(lambda_min, lambda_min, number_of_bins) lambda_min and lambda_max are in Angstroms calibration: calibration file Mantid format **absorp: dictionary containing information to correct absorption for sample and vanadium Only the inputs related to Vanadium will be selected to calculate the correction see docstrings of powder_reduction for more details see help of Mantid's algorithm CylinderAbsorption for details https://docs.mantidproject.org/nightly/algorithms/CylinderAbsorption-v1.html """ vana_red = process_event_data(vanadium, lambda_binning) ec_red = process_event_data(empty_instr, lambda_binning) # remove 'spectrum' from wavelength coordinate and match this coordinate # between Vanadium and Empty instrument data min_lambda = vana_red.coords['wavelength'].values[:, 0].min() max_lambda = vana_red.coords['wavelength'].values[:, 1].max() vana_red.coords['wavelength'] = sc.Variable(['wavelength'], unit=sc.units.angstrom, values=np.linspace(min_lambda, max_lambda, num=2)) ec_red.coords['wavelength'] = sc.Variable(['wavelength'], unit=sc.units.angstrom, values=np.linspace(min_lambda, max_lambda, num=2)) # vana - EC ec_red.coords['wavelength'] = vana_red.coords['wavelength'] vana_red.bins.concatenate(-ec_red, out=vana_red) del ec_red # Absorption correction applied if bool(absorp): # The values of number_density, scattering and attenuation are # hard-coded since they must correspond to Vanadium. Only radius and # height of the Vanadium cylindrical sample shape can be set. The # names of these inputs if present have to be renamed to match # the requirements of Mantid's algorithm CylinderAbsorption # Create dictionary to calculate absorption correction for Vanadium. absorp_vana = { key.replace('Vanadium', 'Sample'): value for key, value in absorp.items() if 'Vanadium' in key } absorp_vana['SampleNumberDensity'] = 0.07118 absorp_vana['ScatteringXSection'] = 5.16 absorp_vana['AttenuationXSection'] = 4.8756 correction = absorption_correction(vanadium, lambda_binning, **absorp_vana) # the 3 following lines of code are to place info about source and # sample position at the right place in the correction dataArray in # order to proceed to the normalization del correction.coords['source_position'] del correction.coords['sample_position'] del correction.coords['position'] lambda_min, lambda_max, number_bins = lambda_binning edges_lambda = sc.Variable(['wavelength'], unit=sc.units.angstrom, values=np.linspace(lambda_min, lambda_max, num=number_bins)) correction = sc.rebin(correction, 'wavelength', edges_lambda) vana_red = vana_red.bins / sc.lookup(func=correction, dim='wavelength') del correction # Calibration # Load input_load_cal = {'InstrumentFilename': 'WISH_Definition.xml'} calvana = load_calibration(calibration, mantid_args=input_load_cal) # Merge table with detector->spectrum mapping from vanadium # (implicitly checking that detectors between vanadium and # calibration are the same) cal_vana = sc.merge(calvana, vana_red.coords['detector_info'].value) del calvana # Compute spectrum mask from detector mask maskvana = sc.groupby(cal_vana['mask'], group='spectrum').any('detector') # Compute spectrum groups from detector groups gvana = sc.groupby(cal_vana['group'], group='spectrum') groupvana = gvana.min('detector') assert sc.identical(groupvana, gvana.max('detector')), \ ("Calibration table has mismatching group " "for detectors in same spectrum") vana_red.coords['group'] = groupvana.data vana_red.masks['mask'] = maskvana.data # convert to d-spacing with calibration vana_dspacing = convert_with_calibration(vana_red, cal_vana) del vana_red, cal_vana # Focus focused_vana = \ sc.groupby(vana_dspacing, group='group').bins.concatenate('spectrum') del vana_dspacing return focused_vana
def powder_reduction(sample='sample.nxs', calibration=None, vanadium=None, empty_instr=None, lambda_binning=(0.7, 10.35, 5615), **absorp): """ Simple WISH reduction workflow Note ---- The sample data were not recorded using the same layout of WISH as the Vanadium and empty instrument. That's why: - loading calibration for Vanadium used a different IDF - the Vanadium correction involved cropping the sample data to the first 5 groups (panels) ---- Corrections applied: - Vanadium correction - Absorption correction - Normalization by monitors - Conversion considering calibration - Masking and grouping detectors into panels Parameters ---------- sample: Nexus event file calibration: .cal file following Mantid's standards The columns correspond to detectors' IDs, offset, selection of detectors and groups vanadium: Nexus event file empty_instr: Nexus event file lambda_binning: min, max and number of steps for binning in wavelength min and max are in Angstroms **absorp: dictionary containing information to correct absorption for Sample and Vanadium. There could be only up to two elements related to the correction for Vanadium: the radius and height of the cylindrical sample shape. To distinguish them from the inputs related to the sample, their names in the dictionary are 'CylinderVanadiumRadius' and 'CylinderVanadiumHeight'. The other keysof the 'absorp' dictionary follow Mantid's syntax and are related to the sample data only. See help of Mantid's algorithm CylinderAbsorption for details https://docs.mantidproject.org/nightly/algorithms/CylinderAbsorption-v1.html Returns ------- Scipp dataset containing reduced data in d-spacing Hints ----- To plot the output data, one can histogram in d-spacing and sum according to groups using scipp.histogram and sc.sum, respectively. """ # Load counts sample_data = scn.load(sample, advanced_geometry=True, load_pulse_times=False, mantid_args={'LoadMonitors': True}) # Load calibration if calibration is not None: input_load_cal = {"InstrumentName": "WISH"} cal = load_calibration(calibration, mantid_args=input_load_cal) # Merge table with detector->spectrum mapping from sample # (implicitly checking that detectors between sample and calibration # are the same) cal_sample = sc.merge(cal, sample_data.coords['detector_info'].value) # Compute spectrum mask from detector mask mask = sc.groupby(cal_sample['mask'], group='spectrum').any('detector') # Compute spectrum groups from detector groups g = sc.groupby(cal_sample['group'], group='spectrum') group = g.min('detector') assert sc.identical(group, g.max('detector')), \ "Calibration table has mismatching group for detectors in same spectrum" sample_data.coords['group'] = group.data sample_data.masks['mask'] = mask.data del cal # Correct 4th monitor spectrum # There are 5 monitors for WISH. Only one, the fourth one, is selected for # correction (like in the real WISH workflow). # Select fourth monitor and convert from tof to wavelength mon4_lambda = scn.convert(sample_data.attrs['monitor4'].values, 'tof', 'wavelength', scatter=False) # Spline background # mon4_spline_background = bspline_background(mon4_lambda, # sc.Dim('wavelength'), # smoothing_factor=70) # Smooth monitor mon4_smooth = smooth_data( mon4_lambda, # mon4_spline_background, dim='wavelength', NPoints=40) # Delete intermediate data del mon4_lambda # , mon4_spline_background # Correct data # 1. Normalize to monitor # Convert to wavelength (counts) sample_lambda = scn.convert(sample_data, 'tof', 'wavelength', scatter=True) del sample_data # Rebin monitors' data lambda_min, lambda_max, number_bins = lambda_binning edges_lambda = sc.Variable(['wavelength'], unit=sc.units.angstrom, values=np.linspace(lambda_min, lambda_max, num=number_bins)) mon_rebin = sc.rebin(mon4_smooth, 'wavelength', edges_lambda) # Realign sample data sample_lambda = sample_lambda.bins / sc.lookup(func=mon_rebin, dim='wavelength') del mon_rebin, mon4_smooth # 2. absorption correction if bool(absorp): # Copy dictionary of absorption parameters absorp_sample = absorp.copy() # Remove input related to Vanadium if present in absorp dictionary found_vana_info = [ key for key in absorp_sample.keys() if 'Vanadium' in key ] for item in found_vana_info: absorp_sample.pop(item, None) # Calculate absorption correction for sample data correction = absorption_correction(sample, lambda_binning, **absorp_sample) # the 3 following lines of code are to place info about source and # sample position at the right place in the correction dataArray in # order to proceed to the normalization del correction.coords['source_position'] del correction.coords['sample_position'] del correction.coords['position'] correction_rebin = sc.rebin(correction, 'wavelength', edges_lambda) del correction sample_lambda = sample_lambda.bins / sc.lookup(func=correction_rebin, dim='wavelength') # 3. Convert to d-spacing with option of taking calibration into account if calibration is None: # No calibration data, use standard convert algorithm sample_dspacing = scn.convert(sample_lambda, 'tof', 'dspacing') else: # Calculate dspacing from calibration file sample_dspacing = convert_with_calibration(sample_lambda, cal_sample) del cal_sample del sample_lambda # 4. Focus panels # Assuming sample is in d-spacing: Focus into groups focused = sc.groupby(sample_dspacing, group='group').bins.concatenate('spectrum') del sample_dspacing # 5. Vanadium correction (requires Vanadium and Empty instrument) if vanadium is not None and empty_instr is not None: print("Proceed with reduction of Vanadium data ") vana_red_focused = process_vanadium_data(vanadium, empty_instr, lambda_binning, calibration, **absorp) # The following selection of groups depends on the loaded data for # Sample, Vanadium and Empty instrument focused = focused['group', 0:5].copy() # histogram vanadium for normalizing + cleaning 'metadata' d_min, d_max, number_dbins = (1., 10., 2000) edges_dspacing = sc.Variable(['dspacing'], unit=sc.units.angstrom, values=np.linspace(d_min, d_max, num=number_dbins)) vana_histo = sc.histogram(vana_red_focused, edges_dspacing) del vana_red_focused vana_histo.coords['detector_info'] = focused.coords[ 'detector_info'].copy() # del vana_histo.coords['source_position'] # del vana_histo.coords['sample_position'] # normalize by vanadium result = focused.bins / sc.lookup(func=vana_histo, dim='dspacing') del vana_histo, focused return result
from moviepy.editor import VideoFileClip from Frame import Frame from Line import Line import cv2 import numpy as np import matplotlib.image as mpimg import matplotlib.pyplot as plt from Line import LaneSide from calibration import load_calibration, undistort # load calibration mtx, dist = load_calibration() l_line = Line(LaneSide.Left) r_line = Line(LaneSide.Right) # image = mpimg.imread('test_images/straight_lines1.jpg') #image = mpimg.imread('test_images/straight_lines2.jpg') # image = mpimg.imread('test_images/test1.jpg') #image = mpimg.imread('test_images/test2.jpg') image = mpimg.imread('test_images/challenge.jpg') # image = mpimg.imread('test_images/challenge2.png') # image = mpimg.imread('test_images/challenge4.jpg') # image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) frames = []
M_inv = cv2.getPerspectiveTransform(dst, src) # Only for creating the final video visualization X_b = [574, 706, 706, 574] Y_b = [719, 719, 0, 0] src_ = np.floor(np.float32([[x[0], y[0]], [x[1], y[1]],[x[2], y[2]], [x[3], y[3]]]) / (input_scale*2)) dst_ = np.floor(np.float32([[X_b[0], Y_b[0]], [X_b[1], Y_b[1]],[X_b[2], Y_b[2]], [X_b[3], Y_b[3]]]) / (input_scale*2)) M_b = cv2.getPerspectiveTransform(src_, dst_) # Only for creating the final video visualization # Threshold for color and gradient thresholding s_thresh, sx_thresh, dir_thresh, m_thresh, r_thresh = (120, 255), (20, 100), (0.7, 1.3), (30, 100), (200, 255) # load the calibration calib_file = 'calibration_pickle.p' mtx, dist = load_calibration(calib_file) def abs_sobel_thresh(img, orient='x', sobel_kernel=3, thresh=(0, 255)): # Apply the following steps to img # 1) Convert to grayscale gray = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY) # 2) Take the derivative in x or y given orient = 'x' or 'y' # 3) Take the absolute value of the derivative or gradient if orient == 'x': abs_sobel = np.absolute(cv2.Sobel(gray, cv2.CV_64F, 1, 0, ksize=sobel_kernel)) if orient == 'y': abs_sobel = np.absolute(cv2.Sobel(gray, cv2.CV_64F, 0, 1, ksize=sobel_kernel))