From 64c77b8b938a152e2f57204b91ef0b4b5e9d440c Mon Sep 17 00:00:00 2001 From: Brian Wandell Date: Thu, 23 Jul 2020 20:17:21 -0700 Subject: [PATCH] Working to make the G data analysis --- sensor/sensorCrop.m | 120 ++++++++++++++++++++++++++++++++++++ sensor/sensorSetSizeToFOV.m | 11 ++-- validate/v_sensorResize.m | 24 +++++--- validate/v_sensorSize.m | 32 ++++++++++ 4 files changed, 173 insertions(+), 14 deletions(-) create mode 100644 sensor/sensorCrop.m create mode 100644 validate/v_sensorSize.m diff --git a/sensor/sensorCrop.m b/sensor/sensorCrop.m new file mode 100644 index 00000000..6eb4643a --- /dev/null +++ b/sensor/sensorCrop.m @@ -0,0 +1,120 @@ +function sensor = sensorCrop(sensor,rect) +% Crop a the sensor data, while preserving the CFA +% +% Synopsis +% sensor = sensorCrop(sensor,rect) +% +% Inputs +% sensor: ISETCam sensor struct +% rect: [x,y,width,height] +% +% Optional variables +% +% Outputs +% +% See also +% sensorSetSizeToFOV, v_sensorSize +% + +% Examples: +%{ +scene = sceneCreate; +oi = oiCreate; +oi = oiCompute(oi,scene); +sensor = sensorCreate; +sensor = sensorCompute(sensor,oi); +sensorWindow(sensor); + +rect = [10 12 13 21]; +test = sensorCrop(sensor,rect); +sensorWindow(test); +%} + +%% Parameters +if ieNotDefined('sensor'), error('sensor required'); end + +% For now. Though we could allow this to be interactive at some point. +if ieNotDefined('rect'), error('crop rect required'); end + +%% + +% Get the data +cfaSize = sensorGet(sensor,'cfaSize'); + +% The [x,y, width, height] needs to match the cfaSize. The cfaSize is +% [row,col]. So we need to deal with that. +cfaSize = fliplr(cfaSize); + +% If the cfa size is even, then the x and y values should be odd The number +% of entries in numel(x:(x + width)) and numel(y:(y + height)) should be a +% multiple of the cfaSize. +% + +%% Select rect values that will preserve the CFA pattern + +% Here are some test cases +%{ +cfaSize = [2,2]; rect = [ 7 9 6 8]; % 7 9 6 8 +cfaSize = [2,2]; rect = [ 8 9 6 8] % 9 9 6 8 + +cfaSize = [5,3]; rect = [6,10,6,8] % 6 10 10 9 +cfaSize = [5,3]; rect = [ 7 10 5 8] % 11 10 5 9 +cfaSize = [5,3]; rect = [ 8 11 6 9] % 11 13 10 9 + +cfaSize = [3,5]; rect = [ 8 11 6 9] % 10 11 6 10 +%} + +% +% First deal with x = rect(1), width = rect(3). +% Then deal with y = rect(2), height = rect(4) +% +for ii=0:1 + % The x value should start on a multiple of the cfaSize plus 1. + thisRem = rem(rect(ii+1),cfaSize(ii+1)); + if thisRem ~= 1 + if thisRem == 0 + rect(ii+1) = rect(ii+1) + 1; + else + % Adjust the starting value by the amount we missed by. which gets + % us to 0 and then add 1. + rect(ii+1) = rect(ii+1) + (cfaSize(ii+1) - thisRem + 1); + end + + end + % The total size should be a multiple of the cfaSize + cnt = numel(rect(ii+1):(rect(ii+1) + rect(ii+3))); + if rem(cnt,cfaSize(ii + 1)) % Zero if a proper multiple + % Not an even devisor of the cfaSize. So add an amount needed to + % make it a proper multiple + rect(ii+3) = rect(ii+3) + cfaSize(ii + 1) - rem(cnt,cfaSize(ii+ 1)); + end +end + +% These are checks that the adjustment worked. Though the width and height +% seem to be off a bit. So we should figure that out and fix it. +assert(rem(rect(1),cfaSize(1)) == 1); +assert(rem(rect(2),cfaSize(2)) == 1); +assert(rem(numel(rect(1):(rect(1) + rect(3))),cfaSize(1)) == 0) +assert(rem(numel(rect(2):(rect(2) + rect(4))),cfaSize(2)) == 0) + +%% Crop the voltage image + +% Crop the volts +volts = sensorGet(sensor,'volts'); +if isempty(volts) + % That's weird. why crop a sensor that has no data? Just resize. +else + newVolts = imcrop(volts,rect); + newSize = size(newVolts); + sensor = sensorSet(sensor,'size',newSize); + sensor = sensorSet(sensor,'volts',newVolts); +end + +% Crop the digital values +dv = sensorGet(sensor,'dv'); +if ~isempty(dv) + newDV = imcrop(dv,rect); + sensor = sensorSet(sensor,'digital values',newDV); +end + +end diff --git a/sensor/sensorSetSizeToFOV.m b/sensor/sensorSetSizeToFOV.m index 99744263..1b456246 100644 --- a/sensor/sensorSetSizeToFOV.m +++ b/sensor/sensorSetSizeToFOV.m @@ -1,4 +1,4 @@ -function [sensor,actualFOV] = sensorSetSizeToFOV(sensor,newFOV,scene,oi) +function [sensor,actualFOV] = sensorSetSizeToFOV(sensor,newFOV,~,oi) % Adjust sensor rows and columns so that horizontal FOV is deg (angle) % % Synopsis @@ -32,7 +32,8 @@ % size must be a multiple of the cfa size. % % The FOV of the sensor depends on the focal length to the optics and the -% size of the sensor. Hence, we normally send in the scene and oi. +% size of the sensor. Hence, we normally send in the oi. We should never +% have to send in the scene and that will be deprecated. % % We try to handle the human cone array case, which is special, by catching % the string 'human' in the name and saying the block size is one. This is @@ -64,10 +65,12 @@ [sensor, fov] = sensorSetSizeToFOV(sensor,[3,3],scene,oi); %} -%% +%% Parameters + +% It appears that we do not really need to send in the scene if ieNotDefined('sensor'), sensor = vcGetObject('sensor'); end if ieNotDefined('newFOV'), error('Must specify desired horizontal field of view (degrees)'); end -if ieNotDefined('scene'), scene = []; end +% if ieNotDefined('scene'), scene = []; end if ieNotDefined('oi'), oi = []; end % Get the size. If size is 0,0 set to a small size. Not sure when it is diff --git a/validate/v_sensorResize.m b/validate/v_sensorResize.m index 1b6e5faf..decdbdd8 100644 --- a/validate/v_sensorResize.m +++ b/validate/v_sensorResize.m @@ -1,17 +1,18 @@ % v_sensorResize % -% Resize the sensor - +% Test resizing the sensor. Mostly tested with human here. +% % v_sensorResize % -% Resize the sensor +% See also +% v_sensorSize (should be merged; that one tests with camera) %% ieInit %% Create a scene and compute an OI scene = sceneCreate; -vcAddAndSelectObject(scene); sceneWindow; +sceneWindow(scene); oi = oiCreate; oi = oiCompute(scene,oi); @@ -21,34 +22,37 @@ sensor = sensorCreate('human'); sensor = sensorSet(sensor,'exptime',0.2); sensor = sensorCompute(sensor,oi); -vcAddAndSelectObject(sensor); sensorWindow('scale',1); +sensorWindow(sensor,'scale',1); -% Something funny happens with the exposure duration. +%% Something funny happens with the exposure duration. rows = [10,10]; cols = [10,10]; sensor2 = sensorHumanResize(sensor,rows,cols); sensor2 = sensorSet(sensor2,'exptime',0.2); sensor2 = sensorCompute(sensor2,oi); -vcAddAndSelectObject(sensor2); sensorWindow; +sensorWindow(sensor2); +%% rows = [-10,-10]; cols = [-10,-10]; sensor3 = sensorHumanResize(sensor2,rows,cols); sensor3 = sensorSet(sensor3,'exptime',0.2); sensor3 = sensorCompute(sensor3,oi); -vcAddAndSelectObject(sensor3); +%% rows = [50,0]; cols = [0,0]; sensor4 = sensorHumanResize(sensor,rows,cols); sensor4 = sensorSet(sensor4,'exptime',0.2); sensor4 = sensorCompute(sensor4,oi); v = sensorGet(sensor4,'volts'); imagesc(v) -vcAddAndSelectObject(sensor4); sensorWindow; +sensorWindow(sensor4); +%% rows = [-50,0]; cols = [0,0]; sensor5 = sensorHumanResize(sensor4,rows,cols); sensor5 = sensorSet(sensor5,'volts',v(51:end,:)); % vcNewGraphWin; v2 = sensorGet(sensor5,'volts'); imagesc(v2) -vcAddAndSelectObject(sensor5); sensorWindow; +sensorWindow(sensor5); +%% END diff --git a/validate/v_sensorSize.m b/validate/v_sensorSize.m new file mode 100644 index 00000000..ac8e0404 --- /dev/null +++ b/validate/v_sensorSize.m @@ -0,0 +1,32 @@ +%% v_sensorSize +% +% Test adjusting the sensor field of view and size. +% +% See also +% v_sensor* + +%% +scene = sceneCreate; +oi = oiCreate; +oi = oiCompute(oi,scene); +sensor = sensorCreate; +sensor = sensorCompute(sensor,oi); + +sensorWindow(sensor); + +%% +sensor2 = sensorSetSizeToFOV(sensor,sceneGet(scene,'fov'),[],oi); +sensor2 = sensorCompute(sensor2,oi); +sensorWindow(sensor2); + +%% Increase the size this way + +sz = sensorGet(sensor2,'size'); +sensor3 = sensorSet(sensor,'size',sz); +sensor3 = sensorCompute(sensor3,oi); +sensorWindow(sensor3); + +%% + + +