├── .gitignore ├── LICENSE ├── README.md ├── analysis └── system_analysis │ └── illustrator_draft.pptx ├── arxiv ├── function_test.mlx ├── light_propagation_v1.m └── light_propagation_v2.m ├── bak └── .gitkeep ├── cacti ├── blur_spot.m ├── cacti.m ├── cacti_conv.m ├── cacti_drawing.m ├── light_drawing.m ├── mask_aver_pool.m ├── mask_conv.m ├── mask_enlarge.m ├── mask_propagate.m ├── params.param ├── point_img.m └── sensor_imaging.m ├── cacti_imaging.m ├── cacti_imaging_conv.m ├── config ├── .gitkeep ├── dmd │ ├── cyl.m │ └── dmd_design.mlx └── patterns_generator │ └── patterns_gen.m ├── demo ├── .gitkeep ├── compare.png ├── image.png ├── obj.png ├── optical_sys.png ├── optical_sys2.png ├── orig_img │ ├── basketball.jpg │ └── rugby.jpg ├── result.png ├── run_time_simu.gif ├── run_time_simu.png └── sys.png ├── light_propagation.m ├── result └── .gitkeep ├── toolbox └── correlation_analysis │ ├── corr_group.m │ ├── masks_corr_analysis.m │ └── matrix_corr.m └── utils ├── FileTraversal.m ├── binary_mask.m ├── blkcolfun.m ├── clamp.m ├── combine_mask.m ├── coord2sub.m ├── corr_group.m ├── dist_map.m ├── dvpdata_normalize.m ├── gen_params.m ├── gen_struct_params.m ├── graphIndepSets.m ├── gray_mask.m ├── idx_matrix.m ├── matrix_corr.m ├── origin2center.m ├── params_function_test.mlx ├── previous_version ├── gen_params_v1.m ├── load_struct_params.old └── mask_propagate.old ├── print2log.m ├── print_params.m ├── run_time.m └── sub2coord.m /.gitignore: -------------------------------------------------------------------------------- 1 | # files 2 | *.txt 3 | *.fig 4 | *.asv 5 | *.vscode 6 | *.mat 7 | *.zip 8 | *.rar 9 | *.pyc 10 | 11 | /result/* 12 | 13 | 14 | 15 | 16 | # dirs 17 | _trash/ 18 | _release/ 19 | __pycache__/ 20 | /tmp/ 21 | 22 | # keep 23 | !.gitkeep 24 | 25 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2020 Zhihong Zhang 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Optical Simulation for CACTI (Coded Aperture Compressive Temporal Imaging) System 2 | 3 | This repository provides a simulation tool for coded aperture compressive temporal imaging based on geometrical optics. 4 | 5 | Maintained by [Zhihong Zhang](https://github.com/dawnlh/) 6 | 7 | CACTI System 8 | 9 | ## CACTI: Coded Aperture Compressive Temporal Imaging 10 | 11 | This CACTI system contains an object, a DMD (Digital Mirror Device), a lens, a static mask and a sensor. The lights emitting from object are collected by the lens and converge towards the sensor (image plane). When propagating through the image space, the light spot will be modulated by DMD and mask before they are finally captured by the sensor. By using dynamic low-resolution DMD masks to modulate the static high-resolution mask, we can synthesize a set of virtual dynamic high-resolution masks, which can be calibrated with a whiteboard. When using the CACTI system to capture the natural objects, these high frequency virtual masks will modulate the scene of different time points. And after that, a set of modulated scenario images will be collected and summed up by the low-speed sensor during one exposure time to form a single frame. By incorporating compressive imaging reconstruction algorithm, we can figure out the coherent scene images to recover a high-speed video sequence. 12 | 13 | CACTI System 14 | 15 | ## Usage 16 | 17 | ### Environment 18 | 19 | The code can run successfully on Windows10, Matlab 2018b. It's supposed to run normally on other platforms (like linux) with relative new version Matlab installed, too. 20 | 21 | ### CACTI imaging 22 | 23 | The main script is "cacti_imaging.m", change the parameter settings and the input data in the code and run it to get the images simulated by CACTI system. 24 | 25 | ```matlab 26 | - sys_params: system parameters, 1*1 struct, with the following fields: 27 | obj_pos, obj_size, obj_pix_size; 28 | lens_pos, lens_f, lens_radius; 29 | dmd_pos, dmd_size, dmd_pix_size; 30 | mask_pos, mask_size, mask_pix_size; 31 | sensor_pos, sensor_size, sensor_pix_size 32 | 33 | - ctrl_params: controlling parameters, 1*1 struct, optional, with the 34 | following fields: 35 | show_compare_flag % show obj, dmd, mask and image, default = 1; 36 | drawing_flag % drawing the optical system 37 | resampling_factor % resampling factor for MASK_PROPAGATE 38 | ideal_sensor_flag % sensor conducts ideal sampling 39 | TEST_MODE_FLAG_ % test mode for MASK_PROPAGATE, default = 'non-test' 40 | parallel_flag % whether to use parallel calculation in CACTI 41 | 42 | - obj: object pattern stack(gray image), 3D numeric matrix, [h, w, batch_num] 43 | 44 | - dmd: dmd pattern stack, 3D logical(double) matrix, 0|1, [h, w, batch_num] 45 | 46 | - mask: mask pattern, 2D logical matrix (binary mask,0|1) or 2D float 47 | matrix(gray mask, [0,1]) 48 | 49 | ``` 50 | 51 | ### Light propagation demonstration 52 | 53 | To see the simulation pipeline, you can run "light_propagation.m" and change the parameter settings to enable the real time illustration of light propagation and light spot changing. 54 | 55 | 56 | 57 | ## Structure of directories 58 | 59 | | directory | description | 60 | | ------------ | --------------------------------------------- | 61 | | arxiv | archived codes | 62 | | bak | backup codes | 63 | | cacti | core codes for CACTI simulation | 64 | | CI algorithm | compressive imaging reconstruction algorithms | 65 | | config | parameters config file | 66 | | dataset | dataset | 67 | | demo | demo pictures | 68 | | result | code result | 69 | | utils | utility codes | 70 | 71 | 72 | 73 | ## Reference 74 | 75 | > [1] T. Bishop, S. Zanetti, and P. Favaro, “Light Field Superresolution,” in Proc. ICCP ’09. IEEE International Conference on Computational Photography, 2009. 76 | > 77 | > [2] **Plug-and-play Algorithms for Large-scale Snapshot Compressive Imaging** in ***IEEE/CVF Conference on Computer Vision and Pattern Recognition (CVPR)*** 2020 (**Oral**) by [Xin Yuan](https://www.bell-labs.com/usr/x.yuan), [Yang Liu](https://liuyang12.github.io/), [Jinli Suo](https://sites.google.com/site/suojinli/), and [Qionghai Dai](http://media.au.tsinghua.edu.cn/). [[pdf\]](https://arxiv.org/pdf/2003.13654) [[github\]](https://github.com/liuyang12/PnP-SCI) [[arXiv\]](https://arxiv.org/abs/2003.13654) 78 | > 79 | > [3] **Deep Learning for Video Compressive Sensing** (***APL Photonics 5, 030801 (2020)***) by Mu Qiao*, Ziyi Meng*, Jiawei Ma, [Xin Yuan](https://www.bell-labs.com/usr/x.yuan) (*Equal contributions). [[pdf\]](https://aip.scitation.org/doi/pdf/10.1063/1.5140721?download=true) [[doi\]](https://aip.scitation.org/doi/10.1063/1.5140721) 80 | 81 | 82 | 83 | ## Contact 84 | 85 | Zhihong Zhang, Tsinghua University,z_zhi_hong@163.com 86 | 87 | 88 | 89 |
Copyright (c) 2020 Zhihong Zhang
90 | -------------------------------------------------------------------------------- /analysis/system_analysis/illustrator_draft.pptx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Design-and-Simulation/CACTI-Simulation/2d5936d2e5ddb3d86fc071aea73f6da1661d2ddc/analysis/system_analysis/illustrator_draft.pptx -------------------------------------------------------------------------------- /arxiv/function_test.mlx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Design-and-Simulation/CACTI-Simulation/2d5936d2e5ddb3d86fc071aea73f6da1661d2ddc/arxiv/function_test.mlx -------------------------------------------------------------------------------- /arxiv/light_propagation_v1.m: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Design-and-Simulation/CACTI-Simulation/2d5936d2e5ddb3d86fc071aea73f6da1661d2ddc/arxiv/light_propagation_v1.m -------------------------------------------------------------------------------- /arxiv/light_propagation_v2.m: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Design-and-Simulation/CACTI-Simulation/2d5936d2e5ddb3d86fc071aea73f6da1661d2ddc/arxiv/light_propagation_v2.m -------------------------------------------------------------------------------- /bak/.gitkeep: -------------------------------------------------------------------------------- 1 | # Ignore everything in this directory 2 | * 3 | # Except this file !.gitkeep 4 | -------------------------------------------------------------------------------- /cacti/blur_spot.m: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Design-and-Simulation/CACTI-Simulation/2d5936d2e5ddb3d86fc071aea73f6da1661d2ddc/cacti/blur_spot.m -------------------------------------------------------------------------------- /cacti/cacti.m: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Design-and-Simulation/CACTI-Simulation/2d5936d2e5ddb3d86fc071aea73f6da1661d2ddc/cacti/cacti.m -------------------------------------------------------------------------------- /cacti/cacti_conv.m: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Design-and-Simulation/CACTI-Simulation/2d5936d2e5ddb3d86fc071aea73f6da1661d2ddc/cacti/cacti_conv.m -------------------------------------------------------------------------------- /cacti/cacti_drawing.m: -------------------------------------------------------------------------------- 1 | function cacti_drawing(sys_params, varargin) 2 | %CACTI_DRAWING illustrating the cacti optical system using given optical system 3 | %parameters 4 | % 5 | % Input: 6 | % -------- 7 | % - sys_params: system parameters 8 | % 9 | % - varargin, contain the following optional arguements 10 | % unit_len[k] - the unit length of figure coordinate system 11 | % [key, str, '-unit_len_k'] 12 | % unit_len[v] - the unit length of figure coordinate system 13 | % [value, float, default = smallest element's size] 14 | % draw_what - {'draw_sys', 'draw_light', 'all'} 15 | % dimension - {'2D', '3D'} 16 | % fig_handle - figure handle, optional, default = generate a new axes; 17 | % 18 | % Output: 19 | % -------- 20 | % the optical path figure 21 | % 22 | % Note: 23 | % -------- 24 | % - physical 3D coordinate system is:x-up, y-out, z-right 25 | % 26 | % - the optical element(obj, dmd, mask,..) is placed with their matrix mn 27 | % coord-sys oppsite to physical 2D xy coord-sys. (i.e., observing from 28 | % anti optical axis direction, the [1 1] element of matrix is at the left 29 | % up corner of the physical object(obj, dmd or mask) 30 | % 31 | % See also: 32 | % -------- 33 | % LIGHT_DRAWING 34 | % 35 | % Info: 36 | % -------- 37 | % Created: Zhihong Zhang , 2020-06-24 38 | % Last Modified: Zhihong Zhang, 2020-06-24 39 | % 40 | % Copyright 2020 Zhihong Zhang 41 | 42 | 43 | % set sys params 44 | % obj 45 | drawing_params(1).name = 'obj'; 46 | drawing_params(1).pos = sys_params.obj_pos; 47 | drawing_params(1).size = sys_params.obj_size; 48 | drawing_params(1).pattern = sys_params.obj; 49 | drawing_params(1).element_sz = sys_params.obj_pix_size; 50 | drawing_params(1).spot = [[sys_params.obj_pix_size.*sys_params.obj_size/2, sys_params.obj_pos]'; 0]; 51 | 52 | % lens 53 | drawing_params(2).name = 'lens'; 54 | drawing_params(2).pos = sys_params.lens_pos; 55 | drawing_params(2).size = 2*sys_params.lens_radius; 56 | drawing_params(2).spot = [[0,0,sys_params.lens_pos]'; sys_params.lens_radius]; 57 | 58 | 59 | % dmd 60 | drawing_params(3).name = 'dmd'; 61 | drawing_params(3).pos = sys_params.dmd_pos; 62 | drawing_params(3).size = sys_params.dmd_size; 63 | drawing_params(3).pattern = sys_params.dmd; 64 | % drawing_params(3).spot = [squeeze(sys_params.dmd_spot_p(1,1,:)); dmd_spot_r]; 65 | drawing_params(3).element_sz = sys_params.dmd_pix_size; 66 | 67 | % mask 68 | drawing_params(4).name = 'mask'; 69 | drawing_params(4).pos = sys_params.mask_pos; 70 | drawing_params(4).size = sys_params.mask_size; 71 | drawing_params(4).pattern = sys_params.mask; 72 | % drawing_params(4).spot = [squeeze(sys_params.mask_spot_p(1,1,:)); mask_spot_r]; 73 | drawing_params(4).element_sz = sys_params.mask_pix_size; 74 | 75 | % sensor 76 | sensor_point = point_img(drawing_params(1).spot(1:3), sys_params.lens_f); 77 | drawing_params(5).name = 'sensor'; 78 | drawing_params(5).pos = sys_params.sensor_pos; 79 | drawing_params(5).size = sys_params.sensor_size; 80 | % drawing_params(5).pattern = sys_params.sensor_img; 81 | drawing_params(5).spot = [sensor_point; 0]; %in-focus 82 | drawing_params(5).element_sz = sys_params.sensor_pix_size; 83 | fig_drawing_ = figure; 84 | light_drawing(drawing_params,fig_drawing_) %,'draw_sys' 85 | drawnow 86 | 87 | end -------------------------------------------------------------------------------- /cacti/light_drawing.m: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Design-and-Simulation/CACTI-Simulation/2d5936d2e5ddb3d86fc071aea73f6da1661d2ddc/cacti/light_drawing.m -------------------------------------------------------------------------------- /cacti/mask_aver_pool.m: -------------------------------------------------------------------------------- 1 | function dst_mask = mask_aver_pool(mask, dst_size, mode) 2 | %MASK_AVER_POOL average pool the given mask to get a new mask with given size 3 | % This can combine adjacent mask elements to get a new elements which has 4 | % equivalent transmittance 5 | % 6 | % Input: 7 | % -------- 8 | % - img: original image, 2D double matrix 9 | % 10 | % - dst_size: size of destination, 2-element int row vector 11 | % 12 | % - mode: image resampling method, str, "fast"| "acc", optional, 13 | % defaut="fast". 14 | % 15 | % Note: 16 | % -------- 17 | % For "fast" mode, use "imresize" to enlarge the original image 18 | % to enable it to be devided exactly, but resize process will 19 | % involve interpolation, which brings about inaccurate factor. 20 | % For "acc" mode, use "kron", instead of "imresize" to enlarge 21 | % the original image, it's accurate, but memory-consumed and 22 | % time-consumed. 23 | % 24 | % Output: 25 | % -------- 26 | % - dst_img: image after average pooling, 2D matrix 27 | % 28 | % See also: 29 | % -------- 30 | % MASK_ENLARGE 31 | % 32 | % Info: 33 | % -------- 34 | % Created: Zhihong Zhang , 2020-03-29 35 | % Last Modified: Zhihong Zhang, 2020-03-29 36 | % 37 | % Copyright (c) 2020 Zhihong Zhang 38 | 39 | 40 | %% imput setting 41 | if nargin<3 42 | mode = "fast"; 43 | end 44 | mode = [lower(char(mode)) ' ']; % Protect against short string 45 | 46 | if isvector(dst_size) && numel(dst_size)==2 47 | if ~isrow(dst_size) 48 | dst_size = dst_size(:)'; 49 | end 50 | elseif isscalar(dst_size) 51 | dst_size = [dst_size dst_size]; 52 | else 53 | error("error input - 'dst_size'"); 54 | end 55 | 56 | 57 | %% processing 58 | % input mask size 59 | mask_sz = size(mask); 60 | 61 | if mode(1) == 'f' 62 | 63 | % convert mask to double 64 | if islogical(mask) 65 | mask = double(mask); 66 | end 67 | 68 | block_sz = round(mask_sz./dst_size); 69 | enlarge_mask_sz = block_sz.*dst_size; 70 | enlarge_mask = imresize(mask,enlarge_mask_sz); 71 | 72 | dst_mask = blkcolfun(enlarge_mask, block_sz, @mean, 'distinct'); 73 | 74 | elseif mode(1) == 'a' 75 | kron_f = lcm(dst_size, mask_sz(1))./mask_sz(1); 76 | block_sz = lcm(dst_size, mask_sz(1))./dst_size; 77 | if prod(mask_sz.*kron_f) > 1e8 78 | warning_mess = ['The "kron" function will create a large matrix(%d elements), ',... 79 | 'which may cause OOM or Not Responding. Press any key to continue, press Ctrl-C to exit']; 80 | warning(warning_mess, prod(mask_sz.*kron_f) ); 81 | pause 82 | end 83 | 84 | enlarge_mask = kron(mask,ones(kron_f)); 85 | 86 | dst_mask = blkcolfun(enlarge_mask, block_sz, @mean, 'distinct'); 87 | else 88 | error("input error - 'mode'"); 89 | end 90 | 91 | end -------------------------------------------------------------------------------- /cacti/mask_conv.m: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Design-and-Simulation/CACTI-Simulation/2d5936d2e5ddb3d86fc071aea73f6da1661d2ddc/cacti/mask_conv.m -------------------------------------------------------------------------------- /cacti/mask_enlarge.m: -------------------------------------------------------------------------------- 1 | function dst_mask = mask_enlarge(mask, dst_size) 2 | %MASK_ENLARGE enlarge the given mask to get a new mask with almost the same 3 | %balck-white distribution. 4 | % This function can virtually divide a mask elements into several new elements which has 5 | % the same value. (i.e. mask resampling to get refined elements) 6 | % 7 | % Input: 8 | % -------- 9 | % - img: original image, 2D matrix or 3D array([row, col, num]) 10 | % 11 | % - dst_size: size of destination, 2-element int vector or int scalar 12 | % 13 | % 14 | % Note: 15 | % -------- 16 | % First, use "kron" to conduct the first enlargement to get a 17 | % tmp mask with size approximate to given size. "kron" can only 18 | % enlarge the mask to a integer times, but it's accurate. 19 | % Then use "imresize" to conduct the second enlargement, to 20 | % enlarge the tmp mask to the given size. Two steps enlargement 21 | % can get accurate result than direct "imresize" 22 | % 23 | % 24 | % Output: 25 | % -------- 26 | % - dst_img: image after average pooling, 2D matrix 27 | % 28 | % See also: 29 | % -------- 30 | % MASK_AVER_POOL 31 | % 32 | % Info: 33 | % -------- 34 | % Created: Zhihong Zhang , 2020-03-29 35 | % Last Modified: Zhihong Zhang, 2020-11-25 36 | % 37 | % Copyright (c) 2020 Zhihong Zhang 38 | 39 | % input check 40 | if isvector(dst_size) 41 | if ~isrow(dst_size) 42 | dst_size = dst_size(:)'; 43 | end 44 | elseif isscalar(dst_size) 45 | dst_size = [dst_size dst_size]; 46 | else 47 | error("error input - 'dst_size'"); 48 | end 49 | 50 | 51 | % enlarge 52 | sz_ratio = dst_size./[size(mask,1),size(mask,2)]; 53 | nmask = size(mask,3); 54 | dst_mask = zeros([dst_size nmask]); 55 | 56 | for k = 1:nmask 57 | tmp_mask = kron(mask(:,:,k), ones(floor(sz_ratio))); 58 | if any(floor(sz_ratio)~=sz_ratio) 59 | dst_mask(:,:,k) = imresize(tmp_mask, dst_size, 'nearest'); 60 | else 61 | dst_mask(:,:,k) = tmp_mask; 62 | end 63 | end 64 | 65 | end 66 | -------------------------------------------------------------------------------- /cacti/mask_propagate.m: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Design-and-Simulation/CACTI-Simulation/2d5936d2e5ddb3d86fc071aea73f6da1661d2ddc/cacti/mask_propagate.m -------------------------------------------------------------------------------- /cacti/params.param: -------------------------------------------------------------------------------- 1 | %% control parameters 2 | ctrl_params.show_compare_flag = 1; % show obj, dmd, mask and image 3 | ctrl_params.drawing_sys_flag = 0; % drawing optical system 4 | ctrl_params.dmd_resampling_factor = 50; % dmd's MASK_PROPAGATE, resampling_factor 5 | ctrl_params.mask_resampling_factor = dmd_resampling_factor; % mask's MASK_PROPAGATE, resampling_factor 6 | ctrl_params.TEST_MODE_FLAG_ = 'non-test'; % test mode off 7 | ctrl_params.parallel_flag = false; % don't use parallel calculation in CACTI 8 | ctrl_params.ideal_sensor_flag = 0; % whether to assume sensor is ideal (sensor is the same as image plane) 9 | 10 | 11 | %% system parameters (mm) 12 | sys_params.obj_pos = -5; 13 | sys_params.obj_size = [200,200]; 14 | sys_params.obj_pix_size = 5e-3; 15 | 16 | % lens 17 | sys_params.lens_pos = 0; 18 | sys_params.lens_f = 2.5; 19 | sys_params.lens_radius = 0.5; 20 | 21 | % dmd 22 | sys_params.dmd_pos = 0; 23 | sys_params.dmd_size = [100,100]; 24 | sys_params.dmd_pix_size = 10e-3; 25 | 26 | % mask 27 | sys_params.mask_pos = 4.85; 28 | % sys_params.mask_pos = 5; 29 | sys_params.mask_size = [200,200]; 30 | sys_params.mask_pix_size = 5e-3; 31 | 32 | % sensor 33 | sys_params.sensor_pos = []; 34 | sys_params.sensor_size = [100, 100]; 35 | sys_params.sensor_pix_size = 10e-3; 36 | 37 | % calculate image position(sensor_pos) 38 | sensor_point = point_img([0,0,sys_params.obj_pos]', sys_params.lens_f); 39 | sys_params.sensor_pos = sensor_point(3); 40 | 41 | 42 | 43 | %% drawing parameters 44 | % set sys params 45 | % obj 46 | drawing_params(1).name = 'obj'; 47 | drawing_params(1).pos = obj_pos; 48 | drawing_params(1).size = obj_size; 49 | drawing_params(1).pattern = obj; 50 | drawing_params(1).spot = [[0;0;0]; 0]; 51 | drawing_params(1).element_sz = obj_pix_size; 52 | 53 | % lens 54 | drawing_params(2).name = 'lens'; 55 | drawing_params(2).pos = 0; 56 | drawing_params(2).size = 2*lens_radius; 57 | drawing_params(2).spot = [[0;0;0]; lens_radius]; 58 | 59 | 60 | % dmd 61 | drawing_params(3).name = 'dmd'; 62 | drawing_params(3).pos = dmd_pos; 63 | drawing_params(3).size = dmd_size; 64 | drawing_params(3).pattern = dmd; 65 | drawing_params(3).spot = [[0;0;0]; 0]; 66 | drawing_params(3).element_sz = dmd_pix_size; 67 | 68 | % mask 69 | drawing_params(4).name = 'mask'; 70 | drawing_params(4).pos = mask_pos; 71 | drawing_params(4).size = mask_size; 72 | drawing_params(4).pattern = mask; 73 | drawing_params(4).spot = [[0;0;0]; 0]; 74 | drawing_params(4).element_sz = mask_pix_size; 75 | 76 | % sensor 77 | drawing_params(5).name = 'sensor'; 78 | drawing_params(5).pos = sensor_pos; 79 | drawing_params(5).size = sensor_size; 80 | drawing_params(5).pattern = sensor; 81 | drawing_params(5).spot = [[0;0;0]; 0]; %in-focus 82 | drawing_params(5).element_sz = sensor_pix_size; 83 | 84 | fig_drawing_ = figure; 85 | light_drawing(drawing_params, 'draw_sys',fig_drawing_) -------------------------------------------------------------------------------- /cacti/point_img.m: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Design-and-Simulation/CACTI-Simulation/2d5936d2e5ddb3d86fc071aea73f6da1661d2ddc/cacti/point_img.m -------------------------------------------------------------------------------- /cacti/sensor_imaging.m: -------------------------------------------------------------------------------- 1 | function sensor_img = sensor_imaging(varargin) 2 | %SENSOR_IMAGING simulation of the imaging process of the sensor 3 | % Assume that, the sensor is located at the image plane. 4 | % Input 5 | % -------- 6 | % tmp_image the image formed by preceding optical components, 7 | % 2D numeric matrix 8 | % 9 | % tmp_img_pixel_size the physical size of tmp image's pixel (element), 10 | % float scalar 11 | % 12 | % sensor_size sensor size (pixel numbers), 2*1 int vector 13 | % 14 | % sensor_pixel_size the physical size of sensor's pixel (element), 15 | % float scalar 16 | % 17 | % AD analog-digital-convert params, [quan_level, sat_value], 18 | % "quan_level" is quantitative level, int scalar, 19 | % default=ceil(sat_value), i.e. default quantitative 20 | % interval is "1". "sat_value" is saturation value, i.e. 21 | % the maximum limitation of ADC, float scalar 22 | % default=max(tmp_img); 23 | % save_class output class of sensor image. 'double'|'single'|'uint8'| 24 | % 'uint16'|'default', optional, default = 'default' 25 | % 26 | % Input 27 | % -------- 28 | % sensor_img image captured by sensor 29 | % 30 | % Input 31 | % -------- 32 | % If only 33 | % 34 | % Info: 35 | % -------- 36 | % Created: Zhihong Zhang , 2020-03-31 37 | % Last Modified: Zhihong Zhang, 2020-03-31 38 | % 39 | % Copyright (c) 2020 Zhihong Zhang 40 | 41 | 42 | % input setting 43 | narginchk(1,6) 44 | 45 | % the first param is 'tmp_img' 46 | tmp_img = varargin{1}; 47 | if nargin==1 48 | % ideal sensor with default saving datatype,{tmp_img} 49 | save_class = 'default'; 50 | elseif nargin==2 51 | % ideal sensor with assigned saving datatype,{tmp_img, save_class} 52 | save_class = varargin{2}; 53 | elseif nargin==4 54 | % {tmp_img, tmp_img_pixel_size, sensor_size, sensor_pixel_size} 55 | tmp_img_pixel_size = varargin{2}; 56 | sensor_size = varargin{3}; 57 | sensor_pixel_size = varargin{4}; 58 | elseif nargin==5 || nargin==6 59 | % non-ideal sensor {tmp_img, tmp_img_pixel_size, sensor_size, sensor_pixel_size, AD [,save_class]} 60 | tmp_img_pixel_size = varargin{2}; 61 | sensor_size = varargin{3}; 62 | sensor_pixel_size = varargin{4}; 63 | AD = varargin{5}; 64 | if nargin==6 65 | save_class = varargin{6}; 66 | else 67 | save_class = 'default'; 68 | end 69 | else 70 | error('input parameters error') 71 | end 72 | 73 | 74 | % tmp image size 75 | tmp_img_size = size(tmp_img); 76 | tmp_img = double(tmp_img); 77 | % image brightness record 78 | img_energy_scale = max(tmp_img, [],'all'); 79 | % geometrical optics, the image is inverted 80 | if img_energy_scale>0 81 | tmp_img = rot90(tmp_img./img_energy_scale,2); 82 | else 83 | % black image (all 0) 84 | tmp_img = rot90(tmp_img,2); 85 | end 86 | 87 | 88 | % ideal sensor sampling 89 | if nargin<3 || (all(tmp_img_size==sensor_size, 'all') && all(tmp_img_pixel_size==sensor_pixel_size, 'all')) 90 | % ideal sensor sampling: the sensor pixels are exactly matched with the object 91 | % points' pixels. And ADC is not considerated. 92 | sensor_img = tmp_img*img_energy_scale; 93 | sensor_max_value = max(sensor_img, [],'all'); 94 | if strcmp(save_class, 'default') 95 | % default save class 96 | if sensor_max_value < 256 97 | save_class = 'uint8'; 98 | elseif sensor_max_value < 65536 99 | save_class = 'uint16'; 100 | else 101 | save_class = 'double'; 102 | end 103 | end 104 | sensor_img = feval(save_class, sensor_img); 105 | return 106 | end 107 | 108 | 109 | % sensor size 110 | if ~isrow(sensor_size) 111 | sensor_size = sensor_size'; 112 | end 113 | 114 | %% physical size comparsion and crop 115 | tmp_img_sz = tmp_img_size*tmp_img_pixel_size; 116 | sensor_sz = sensor_size*sensor_pixel_size; 117 | 118 | % tmp_img crop 119 | tmp_delta_sz =max(tmp_img_sz - sensor_sz, 0); 120 | tmp_delta_pix_num = round(tmp_delta_sz./tmp_img_pixel_size); 121 | image_captured_pix_num = min(round(sensor_sz./tmp_img_pixel_size), tmp_img_size); 122 | 123 | tmp_img_captured_idx = [floor(tmp_delta_pix_num./2)+1, floor(tmp_delta_pix_num./2)+image_captured_pix_num]; 124 | tmp_image_captured = tmp_img(tmp_img_captured_idx(1):tmp_img_captured_idx(3),tmp_img_captured_idx(2):tmp_img_captured_idx(4)); 125 | 126 | 127 | % sensor area 128 | sensor_delta_sz =max(sensor_sz - tmp_img_sz, 0); 129 | sensor_delta_pix_num = round(sensor_delta_sz./sensor_pixel_size); 130 | sensor_used_pix_num = min(round(tmp_img_sz./sensor_pixel_size), sensor_size); 131 | 132 | sensor_img_used_idx = [floor(sensor_delta_pix_num./2)+1, floor(sensor_delta_pix_num./2)+sensor_used_pix_num]; 133 | 134 | %% sensor sampling 135 | sensor_img = zeros(sensor_size); 136 | 137 | if all(image_captured_pix_num == sensor_used_pix_num,'all') 138 | sampled_tmp_img = tmp_image_captured; 139 | elseif all(image_captured_pix_num > sensor_used_pix_num,'all') 140 | % tmp image has more pixels than sensor, combine tmp image pixels to 141 | % match one sensor pixel 142 | block_sz = ceil(image_captured_pix_num./sensor_used_pix_num); 143 | resize_tmp_img_sz = block_sz.*sensor_used_pix_num; 144 | resize_tmp_img = imresize(tmp_image_captured,resize_tmp_img_sz); 145 | 146 | sampled_tmp_img = blkcolfun(resize_tmp_img, block_sz, @sum, 'distinct'); 147 | else 148 | % tmp image has less pixels than sensor, divide tmp image pixel into 149 | % several sub pixels to match one sensor pixel 150 | sz_ratio = sensor_used_pix_num./image_captured_pix_num; 151 | resize__tmp_img = kron(tmp_image_captured, ones(floor(sz_ratio))); 152 | sampled_tmp_img = imresize(resize__tmp_img, sensor_used_pix_num, 'nearest'); 153 | sampled_tmp_img = sampled_tmp_img/prod(sz_ratio); % each subpixel get partial energy(gray value) 154 | end 155 | 156 | sensor_img(sensor_img_used_idx(1):sensor_img_used_idx(3),... 157 | sensor_img_used_idx(2):sensor_img_used_idx(4)) = sampled_tmp_img; 158 | 159 | %% quantification 160 | % recover the image's real brightness(energy) 161 | sensor_img = sensor_img*img_energy_scale; 162 | % ADC 163 | % default AD params 164 | if nargin < 5 165 | default_sat_value = max(sensor_img, [],'all'); 166 | default_quan_level = ceil(default_sat_value); 167 | AD = [default_quan_level, default_sat_value]; 168 | end 169 | 170 | quan_level = AD(1); 171 | sat_value = AD(2); 172 | 173 | sensor_img = min(sensor_img, sat_value); 174 | sensor_img = round(sensor_img./(sat_value/quan_level)); 175 | if strcmp(save_class, 'default') 176 | if quan_level < 256 177 | save_class = 'uint8'; 178 | elseif quan_level < 65536 179 | save_class = 'uint16'; 180 | else 181 | save_class = 'double'; 182 | end 183 | end 184 | sensor_img = feval(save_class, sensor_img); 185 | 186 | end -------------------------------------------------------------------------------- /cacti_imaging.m: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Design-and-Simulation/CACTI-Simulation/2d5936d2e5ddb3d86fc071aea73f6da1661d2ddc/cacti_imaging.m -------------------------------------------------------------------------------- /cacti_imaging_conv.m: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Design-and-Simulation/CACTI-Simulation/2d5936d2e5ddb3d86fc071aea73f6da1661d2ddc/cacti_imaging_conv.m -------------------------------------------------------------------------------- /config/.gitkeep: -------------------------------------------------------------------------------- 1 | # Ignore everything in this directory 2 | * 3 | # Except this file !.gitkeep 4 | -------------------------------------------------------------------------------- /config/dmd/cyl.m: -------------------------------------------------------------------------------- 1 | function z = cyl(N,R,P) 2 | % CYL generates samples of a continuous, aperiodic, unity-height cylinder 3 | % at the points specified in P with radius R (percent of the length) on a background 4 | % rectangle with size N. 5 | % 6 | % Input: 7 | % -------- 8 | % - N: 2-elements int vector, background rectangle size 9 | % - P: 2-elements int vector,centre point of the cylinder, default=[0,0] 10 | % - R: float scalar, radius of the cylinder 11 | % Output: 12 | % -------- 13 | % - z: unity-height cylinder 14 | 15 | % input check 16 | if numel(N)==1 17 | N = [N,N]; % background size 18 | z = zeros(N); % background 19 | elseif ~isvector(N) 20 | z = N; % background 21 | N = size(z); % background size 22 | end 23 | 24 | if nargin < 3 25 | P = round(N./2); 26 | end 27 | 28 | x0 = P(1); 29 | y0 = P(2); 30 | L1 = N(1); 31 | L2 = N(2); 32 | 33 | [x,y]=meshgrid(linspace(1, L1, L1), linspace(1, L2, L2)); 34 | x = x'; 35 | y = y'; 36 | 37 | r = sqrt((x-x0).*(x-x0)+(y-y0).*(y-y0)); % distance map 38 | 39 | z(r<=R) = 1; 40 | end -------------------------------------------------------------------------------- /config/dmd/dmd_design.mlx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Design-and-Simulation/CACTI-Simulation/2d5936d2e5ddb3d86fc071aea73f6da1661d2ddc/config/dmd/dmd_design.mlx -------------------------------------------------------------------------------- /config/patterns_generator/patterns_gen.m: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Design-and-Simulation/CACTI-Simulation/2d5936d2e5ddb3d86fc071aea73f6da1661d2ddc/config/patterns_generator/patterns_gen.m -------------------------------------------------------------------------------- /demo/.gitkeep: -------------------------------------------------------------------------------- 1 | # Ignore everything in this directory 2 | * 3 | # Except this file !.gitkeep 4 | -------------------------------------------------------------------------------- /demo/compare.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Design-and-Simulation/CACTI-Simulation/2d5936d2e5ddb3d86fc071aea73f6da1661d2ddc/demo/compare.png -------------------------------------------------------------------------------- /demo/image.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Design-and-Simulation/CACTI-Simulation/2d5936d2e5ddb3d86fc071aea73f6da1661d2ddc/demo/image.png -------------------------------------------------------------------------------- /demo/obj.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Design-and-Simulation/CACTI-Simulation/2d5936d2e5ddb3d86fc071aea73f6da1661d2ddc/demo/obj.png -------------------------------------------------------------------------------- /demo/optical_sys.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Design-and-Simulation/CACTI-Simulation/2d5936d2e5ddb3d86fc071aea73f6da1661d2ddc/demo/optical_sys.png -------------------------------------------------------------------------------- /demo/optical_sys2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Design-and-Simulation/CACTI-Simulation/2d5936d2e5ddb3d86fc071aea73f6da1661d2ddc/demo/optical_sys2.png -------------------------------------------------------------------------------- /demo/orig_img/basketball.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Design-and-Simulation/CACTI-Simulation/2d5936d2e5ddb3d86fc071aea73f6da1661d2ddc/demo/orig_img/basketball.jpg -------------------------------------------------------------------------------- /demo/orig_img/rugby.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Design-and-Simulation/CACTI-Simulation/2d5936d2e5ddb3d86fc071aea73f6da1661d2ddc/demo/orig_img/rugby.jpg -------------------------------------------------------------------------------- /demo/result.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Design-and-Simulation/CACTI-Simulation/2d5936d2e5ddb3d86fc071aea73f6da1661d2ddc/demo/result.png -------------------------------------------------------------------------------- /demo/run_time_simu.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Design-and-Simulation/CACTI-Simulation/2d5936d2e5ddb3d86fc071aea73f6da1661d2ddc/demo/run_time_simu.gif -------------------------------------------------------------------------------- /demo/run_time_simu.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Design-and-Simulation/CACTI-Simulation/2d5936d2e5ddb3d86fc071aea73f6da1661d2ddc/demo/run_time_simu.png -------------------------------------------------------------------------------- /demo/sys.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Design-and-Simulation/CACTI-Simulation/2d5936d2e5ddb3d86fc071aea73f6da1661d2ddc/demo/sys.png -------------------------------------------------------------------------------- /light_propagation.m: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Design-and-Simulation/CACTI-Simulation/2d5936d2e5ddb3d86fc071aea73f6da1661d2ddc/light_propagation.m -------------------------------------------------------------------------------- /result/.gitkeep: -------------------------------------------------------------------------------- 1 | # Ignore everything in this directory 2 | * 3 | # Except this file !.gitkeep 4 | -------------------------------------------------------------------------------- /toolbox/correlation_analysis/corr_group.m: -------------------------------------------------------------------------------- 1 | function [corr_idx_matrix, corr_group] = corr_group(corr_matrix, corr_level) 2 | %CORR_GROUP get correlated groups from given correlation coefficient matrix (or p-value matrix) 3 | % 4 | % Input: 5 | % -------- 6 | % - corr_matrix: correlation coefficient matrix (or p-value matrix), N*N 7 | % matrix 8 | % 9 | % - corr_level: double scalar, if corr_level>0, then it means value above "corr_level" 10 | % will be considered to be corelated (correlation coefficient matrix); if corr<0, 11 | % the it means value below "corr_level" will be considered to be 12 | % corelated. (for p-value matrix) 13 | % 14 | % Output: 15 | % -------- 16 | % - corr_group: correlation groups for every signal item (each row or each col, which represents 17 | % a signal item), 1*N cell 18 | % - corr_idx_matrix: correlation index matrix, 1-independent; 0-correlated 19 | % 20 | % Info: 21 | % -------- 22 | % Created: Zhihong Zhang , 2020-04-27 23 | % Last Modified: Zhihong Zhang, 2020-04-27 24 | % 25 | % Copyright (c) 2020 Zhihong Zhang. 26 | 27 | corr_idx_matrix = corr_matrix; 28 | 29 | 30 | if corr_level<0 31 | corr_idx_matrix(corr_matrix < -corr_level) = 0; % correlated 32 | corr_idx_matrix(corr_matrix >= -corr_level) = 1; % independent 33 | else 34 | corr_idx_matrix(corr_matrix > corr_level) = 0; 35 | corr_idx_matrix(corr_matrix <= corr_level) = 1; 36 | end 37 | 38 | % diagonal elements are self-correlated for sure 39 | N = size(corr_idx_matrix, 1); % corr_idx_matrix number of rows 40 | corr_idx_matrix(eye(N, 'logical')) = 0; 41 | 42 | figure,imshow(corr_idx_matrix) 43 | title('corr-idx-matrix:1-independent; 0-correlated') 44 | 45 | 46 | corr_group = cell(1,N); 47 | 48 | for k=1:N 49 | corr_group{k} = find(corr_idx_matrix(k,:)==0); 50 | corr_group{k} = [k, corr_group{k}]; 51 | end 52 | end 53 | 54 | 55 | 56 | -------------------------------------------------------------------------------- /toolbox/correlation_analysis/masks_corr_analysis.m: -------------------------------------------------------------------------------- 1 | close all 2 | [r,p,ar,ap] = matrix_corr(mask_bayer) % corr matrix and p-value 3 | [corr_idx_matrix, corr_groups] = corr_group(p, -0.05); 4 | % dmd_granularity = [6 6]; 5 | dmd_granularity = [3 3]; 6 | 7 | N = prod(dmd_granularity); 8 | for k = 1:N 9 | corr_item = cell2mat(corr_groups(k)); 10 | % [px, py] = ind2sub(dmd_blank_shape, [k corr_item]) 11 | 12 | corr_matrix = zeros(dmd_granularity); 13 | corr_matrix(corr_item) = 1; 14 | corr_matrix(k) = 0.5; 15 | % corr_matrix(px,py) = 1; 16 | 17 | figure 18 | imshow(corr_matrix) 19 | title(['correlation area(white) for pos ',num2str(k)]); 20 | end -------------------------------------------------------------------------------- /toolbox/correlation_analysis/matrix_corr.m: -------------------------------------------------------------------------------- 1 | function [corr_mat, p_mat, aver_corr, aver_p] = matrix_corr(X, varargin) 2 | %MATRIX_CORR calculate the correlation coefficient of given matrix 3 | % convert the matrixs to column vector and calculate their correlation 4 | % coefficient matrix and average correlation coefficient 5 | % 6 | % Input: 7 | % ------- 8 | % X: input matrix, 2D or 3D numerical matrix (3D means matrix is stacked along 9 | % 3rd axis, Y is omitted in these case) 10 | % varargin: more input matrix, 2D numerical matrix, take effect when X is 11 | % also a 2D numerical matrix 12 | % 13 | % Output: 14 | % corr_mat: correlation coefficient matrix of columns (straightened 15 | % matrix) 16 | % p_mat: p value matrix of columns 17 | % aver_corr: average correlation coefficient of different columns 18 | % aver_p: average p-value of different columns 19 | % 20 | % Note: 21 | % -------- 22 | % This funcion can be substituted with built-in function CORRCOEF 23 | % 24 | % 25 | % Info: 26 | % -------- 27 | % Created: Zhihong Zhang , 2020-04-06 28 | % Last Modified: Zhihong Zhang, 2020-04-06 29 | % 30 | % Copyright (c) 2020 Zhihong Zhang. 31 | 32 | 33 | Y = cell2mat(varargin); 34 | 35 | if nargin == 1 && isnumeric(X) && (ndims(X)==3) 36 | matrix_stack = X; 37 | 38 | elseif nargin > 1 && ismatrix(Y) && (isnumeric(Y)||islogical(Y)) 39 | 40 | Y = reshape(Y, [size(varargin{1}), numel(varargin)]); 41 | matrix_stack = cat(3,X,Y); 42 | 43 | else 44 | error("multi inputs--2D matrixs or one input--single 3D matrix is valid inputs") 45 | end 46 | 47 | matrix_stack = double(matrix_stack); % int is not supported by corr 48 | 49 | matrix_stack_sz =size(matrix_stack); 50 | X2col = reshape(matrix_stack, [prod(matrix_stack_sz(1:2)) matrix_stack_sz(3)]); 51 | [corr_mat, p_mat] = corrcoef(X2col); 52 | 53 | corr_mat_triu = triu(corr_mat, 1); 54 | aver_corr = sum(abs(corr_mat_triu), 'all')/nchoosek(matrix_stack_sz(3),2); 55 | 56 | p_mat_triu = triu(p_mat, 1); 57 | aver_p = sum(abs(p_mat_triu), 'all')/nchoosek(matrix_stack_sz(3),2); 58 | end 59 | -------------------------------------------------------------------------------- /utils/FileTraversal.m: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Design-and-Simulation/CACTI-Simulation/2d5936d2e5ddb3d86fc071aea73f6da1661d2ddc/utils/FileTraversal.m -------------------------------------------------------------------------------- /utils/binary_mask.m: -------------------------------------------------------------------------------- 1 | function mask = binary_mask(sz, mode, param) 2 | %BINARY_MASK generate a binary mask with given size and other info. 3 | % 4 | %The binary mask can be generated in a random or determined way. 5 | %It can also generate a binary mask from given image. 6 | % 7 | % Input: 8 | % -------- 9 | % - size: mask size, scalar or int 2D-vector 10 | % 11 | % - mode: mask's generating method, str, {'rand', 'fixed', 'image'}, 12 | % optional, default='rand' 13 | % 14 | % - param: 15 | % - for 'rand' mode: 'param' is the "1"'s probability, i.e. the 16 | % transmittance, float, optional, default=0.5. The mask is 17 | % generated from RAND (uniform random distribution). 18 | % 19 | % - for 'fixed' mode: 'param' is the pattern type, str, 20 | % {'up_half', 'down_half'} 21 | % 22 | % - for 'image' mode: 'param' is an image, it will be resized to 23 | % given size and then converted to a binary matrix with 'im2bw' 24 | % 25 | % Output: 26 | % -------- 27 | % - mask: binary mask, logical 2D matrix,{0, 1} 28 | % 29 | % See also: 30 | % -------- 31 | % RAND, IMBINARIZE 32 | % 33 | % Log: 34 | % -------- 35 | % 36 | % Info: 37 | % -------- 38 | % Created: Zhihong Zhang , 2020-03-22 39 | % Last Modified: Zhihong Zhang, 2020-03-22 40 | % 41 | % Copyright 2020 Zhihong Zhang 42 | 43 | % input setting 44 | narginchk(1,3); 45 | if nargin == 1 46 | mode = 'rand'; 47 | param = 0.5; 48 | elseif nargin == 2 49 | if isa(mode, 'float')|| isa(mode, 'double') 50 | param = mode; 51 | mode = 'rand'; 52 | else 53 | error('input error!'); 54 | end 55 | end 56 | 57 | if isscalar(sz) 58 | sz = [sz sz]; 59 | end 60 | 61 | % mask generating 62 | switch mode 63 | case 'rand' 64 | if (0<=param) && (param<=1) 65 | mask = rand(sz); 66 | tmp = mask; 67 | mask(tmp>param) = false; 68 | mask(tmp<=param) = true; 69 | else 70 | error('the input probability should be in [0 1]') 71 | end 72 | case 'fixed' 73 | if strcmp(param, 'up_half') 74 | mask = zeros(sz); 75 | mask(1:ceil(sz(1)/2),:) = true; 76 | elseif strcmp(param, 'down_half') 77 | mask = zeros(sz); 78 | mask(floor(sz(1)/2):end,:) = true; 79 | else 80 | error('unsupported mask type - "%s"', param); 81 | end 82 | case 'image' 83 | if isnumeric(param) && ismatrix(param) 84 | img = im2double(imresize(param, sz)); 85 | mask = imbinarize(img); 86 | elseif isnumeric(param) && (ndims(param)==3 && size(param,3)==3) 87 | img = im2double(imresize(rgb2gray(param), sz)); 88 | mask = imbinarize(img); 89 | else 90 | error('error arguement for - "%s"', mode); 91 | end 92 | end 93 | mask = logical(mask); 94 | end 95 | -------------------------------------------------------------------------------- /utils/blkcolfun.m: -------------------------------------------------------------------------------- 1 | function B = blkcolfun(A, blksize, f, varargin) 2 | %BLKCOLFUN processes the matrix A by rearranging each m-by-n block of A into 3 | %a column of a temporary matrix, and then applying the function fun to this 4 | %matrix. colfilt zero-pads A, if necessary. Finaly, rearranges the result 5 | %back to get the output. 6 | % 7 | % Input: 8 | % -------- 9 | % - A: original image, 2D matrix 10 | % 11 | % - blksize: size of destination, 2-element int row vector 12 | % 13 | % - f: function handle 14 | % 15 | % - varargin: optional inputs, contains the following variables 16 | % - padding: whether to pad the boundary blocks or discard 17 | % them, logical, true (default) | false 18 | % 19 | % - blktype: block type, str, 'sliding' (default)| 'distinct' 20 | % Specified as 'sliding' for sliding neighborhoods 21 | % or 'distinct' for distinct blocks. 22 | % 23 | % Note: 24 | % -------- 25 | % For "distinct" mode, the "f" should return a row vector with the number 26 | % of elements equal to the number of input matrix's columns, or a matrix with the 27 | % same size as the input matrix. But for "sliding" mode, only the former 28 | % case is accepted. 29 | % 30 | % So, for "distinct" mode, BLKCOLFUN can return a matrix with size equal to 31 | % the devided blocks' shape or equal to the input matrix's size according to 32 | % the "f". For "sliding" mode, BLKCOLFUN will return a matrix with size equal to 33 | % the devided blocks' shape (when "padding" is ture, the size equal to the 34 | % input matrix's size, otherwise, it may be smaller as the boundary 35 | % blocks are discarded.) 36 | % 37 | % Output: 38 | % -------- 39 | % - B: output matrix 40 | % 41 | % 42 | % Info: 43 | % -------- 44 | % Created: Zhihong Zhang , 2020-03-29 45 | % Last Modified: Zhihong Zhang, 2020-03-29 46 | % 47 | % Copyright (c) 2020 Zhihong Zhang 48 | 49 | 50 | %% input setting 51 | padding = 'true'; 52 | blktype = 'distinct'; 53 | 54 | for var_item = varargin 55 | var_value = var_item{1}; 56 | if islogical(var_value) 57 | padding = var_value; 58 | elseif ischar(var_value) || isstring(var_value) 59 | if isstring(var_value) 60 | var_value = char(var_value); 61 | end 62 | blktype = var_value; 63 | else 64 | error("error input") 65 | end 66 | end 67 | 68 | 69 | blktype = [lower(blktype) ' ']; % Protect against short string 70 | 71 | if isvector(blksize) && numel(blksize)==2 72 | if ~isrow(blksize) 73 | blksize = blksize(:)'; 74 | end 75 | elseif isscalar(blksize) 76 | blksize = [blksize blksize]; 77 | else 78 | error("error input - 'blksize'"); 79 | end 80 | 81 | %% processing 82 | if blktype(1)=='d' 83 | 84 | % input matrix size 85 | A_size = size(A); 86 | 87 | % Convert neighborhoods of matrix A to columns 88 | tmp_mat = im2col2(A, blksize, 'distinct'); 89 | 90 | % call fun 91 | tmp_res = f(tmp_mat); 92 | 93 | % rearrange outputs 94 | if all(size(tmp_res) == size(tmp_mat)) 95 | col_re_size = [blksize(1), blksize(2)]; % col2im's column rearrange's size 96 | out_re_size = ceil(A_size./blksize).*blksize; % col2im's output rearrange's size 97 | out_noborder_sz = A_size; % no-border output rearrange's size 98 | 99 | B = col2im2(tmp_res, col_re_size, out_re_size, 'distinct'); 100 | if ~padding 101 | B = B(1:out_noborder_sz(1), 1:out_noborder_sz(2)); 102 | end 103 | 104 | elseif isrow(tmp_res) 105 | 106 | out_re_size = ceil(A_size./blksize); % col2im's output rearrange's size 107 | out_noborder_sz = floor(A_size./blksize); % no-border output rearrange's size 108 | 109 | % col_re_size = [1 1]; % col2im's column rearrange's size 110 | % B = col2im(tmp_res, col_re_size, out_re_size, 'distinct'); 111 | B = reshape(tmp_res, out_re_size); 112 | if ~padding 113 | B = B(1:out_noborder_sz(1), 1:out_noborder_sz(2)); 114 | end 115 | end 116 | 117 | elseif blktype(1)=='s' 118 | % input matrix size 119 | A_size = size(A); 120 | 121 | % padding 122 | if padding 123 | left_up_pos = floor((blksize - 1)/2); 124 | A_padding = zeros(A_size+blksize - 1); 125 | A_padding(left_up_pos(1)+1:left_up_pos(1)+A_size(1),left_up_pos(2)+1:... 126 | left_up_pos(2)+A_size(2)) = A; 127 | A = A_padding; 128 | out_re_size = A_size; % col2im's output rearrange's size 129 | else 130 | out_re_size = (A_size-blksize)+1; % col2im's output rearrange's size 131 | end 132 | 133 | % Convert neighborhoods of matrix A to columns 134 | tmp_mat = im2col2(A, blksize, 'sliding'); 135 | 136 | % call fun 137 | tmp_res = f(tmp_mat); 138 | 139 | % rearrange outputs 140 | if isrow(tmp_res) 141 | B = reshape(tmp_res, out_re_size); 142 | else 143 | error("the function should have a row-vector return value"); 144 | end 145 | end 146 | 147 | end 148 | 149 | function b=im2col2(a, block, kind) 150 | %IM2COL2 Rearrange image blocks into columns. 151 | % rewrite from the build-in function IMG2COL 152 | % 153 | % See also: 154 | % IMG2COL 155 | 156 | if kind(1)=='d' 157 | 158 | nrows = block(1); 159 | ncols = block(2); 160 | nElementBlk = nrows*ncols; 161 | 162 | mpad = mod(size(a,1),nrows); if mpad>0, mpad = block(1)-mpad; end 163 | npad = mod(size(a,2),ncols); if npad>0, npad = block(2)-npad; end 164 | 165 | aPad = zeros([size(a,1)+mpad size(a,2)+npad]); 166 | aPad(1:size(a,1),1:size(a,2)) = a; 167 | 168 | t1 = reshape(aPad,nrows,size(aPad,1)/nrows,[]); 169 | t2 = reshape(permute(t1,[1 3 2]),size(t1,1)*size(t1,3),[]); 170 | t3 = permute(reshape(t2,nElementBlk,size(t2,1)/nElementBlk,[]),[1 3 2]); 171 | b = reshape(t3,nElementBlk,[]); 172 | 173 | 174 | elseif kind(1)=='s' 175 | [ma,na] = size(a); 176 | m = block(1); n = block(2); 177 | 178 | if any([ma na] < [m n]) % if neighborhood is larger than image 179 | b = zeros(m*n,0); 180 | return 181 | end 182 | 183 | % Create Hankel-like indexing sub matrix. 184 | mc = block(1); nc = ma-m+1; nn = na-n+1; 185 | cidx = (0:mc-1)'; ridx = 1:nc; 186 | t = cidx(:,ones(nc,1)) + ridx(ones(mc,1),:); % Hankel Subscripts 187 | tt = zeros(mc*n,nc); 188 | rows = 1:mc; 189 | for i=0:n-1 190 | tt(i*mc+rows,:) = t+ma*i; 191 | end 192 | ttt = zeros(mc*n,nc*nn); 193 | cols = 1:nc; 194 | for j=0:nn-1 195 | ttt(:,j*nc+cols) = tt+ma*j; 196 | end 197 | 198 | % If a is a row vector, change it to a column vector. This change is 199 | % necessary when A is a row vector and [M N] = size(A). 200 | if ismatrix(a) && na > 1 && ma == 1 201 | a = a(:); 202 | end 203 | b = a(ttt); 204 | 205 | else 206 | % We should never fall into this section of code. This problem should 207 | % have been caught in input parsing. 208 | error(message('images:im2col2:internalErrorUnknownBlockType', kind)); 209 | end 210 | end 211 | 212 | function a = col2im2(b,block,mat,kind) 213 | %COL2IM Rearrange matrix columns into blocks. 214 | % A = COL2IM(B,[M N],[MM NN],'distinct') rearranges each column of B into a 215 | % rewrite from the build-in function IMG2COL 216 | % 217 | % See also: 218 | % COL2IMG 219 | 220 | 221 | if kind(1)=='d' % Distinct 222 | % Check argument sizes 223 | [m,n] = size(b); 224 | if prod(block)~=m, error(message('images:col2im:wrongSize')); end 225 | 226 | % Find size of padded A. 227 | mpad = rem(mat(1),block(1)); if mpad>0, mpad = block(1)-mpad; end 228 | npad = rem(mat(2),block(2)); if npad>0, npad = block(2)-npad; end 229 | mpad = mat(1)+mpad; npad = mat(2)+npad; 230 | if mpad*npad/prod(block)~=n 231 | error(message('images:col2im:inconsistentSize')); 232 | end 233 | 234 | mblocks = mpad/block(1); 235 | nblocks = npad/block(2); 236 | aa = repmat(feval(class(b), 0), [mpad npad]); 237 | x = repmat(feval(class(b), 0), block); 238 | rows = 1:block(1); cols = 1:block(2); 239 | for i=0:mblocks-1 240 | for j=0:nblocks-1 241 | x(:) = b(:,i+j*mblocks+1); 242 | aa(i*block(1)+rows,j*block(2)+cols) = x; 243 | end 244 | end 245 | a = aa(1:mat(1),1:mat(2)); 246 | 247 | elseif kind(1)=='s' % sliding 248 | a = reshape(b,mat(1)-block(1)+1,mat(2)-block(2)+1); 249 | else 250 | error(message('images:col2im:unknownBlockType', deblank(kind))) 251 | 252 | end 253 | 254 | end 255 | -------------------------------------------------------------------------------- /utils/clamp.m: -------------------------------------------------------------------------------- 1 | function b = clamp(a, lower_limit, higher_limit) 2 | %CLAMP limits the values of given array to a certain boundary 3 | % 4 | % Input 5 | % -------- 6 | % a : input array, numeric 7 | % lower_limit : lower boundary, numeric 8 | % higher_limit: lower boundary, numeric 9 | % 10 | % Output 11 | % -------- 12 | % b : output array with value clamped, numeric 13 | % 14 | 15 | if nargin<3 16 | higher_limit = inf; 17 | end 18 | 19 | b = max(a, lower_limit); 20 | b = min(b, higher_limit); 21 | 22 | end 23 | 24 | -------------------------------------------------------------------------------- /utils/combine_mask.m: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Design-and-Simulation/CACTI-Simulation/2d5936d2e5ddb3d86fc071aea73f6da1661d2ddc/utils/combine_mask.m -------------------------------------------------------------------------------- /utils/coord2sub.m: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Design-and-Simulation/CACTI-Simulation/2d5936d2e5ddb3d86fc071aea73f6da1661d2ddc/utils/coord2sub.m -------------------------------------------------------------------------------- /utils/corr_group.m: -------------------------------------------------------------------------------- 1 | function [corr_idx_matrix, corr_group] = corr_group(corr_matrix, corr_level) 2 | %CORR_GROUP get correlated groups from given correlation coefficient matrix (or p-value matrix) 3 | % 4 | % Input: 5 | % -------- 6 | % - corr_matrix: correlation coefficient matrix (or p-value matrix), N*N 7 | % matrix 8 | % 9 | % - corr_level: double scalar, if corr_level>0, then it means value above "corr_level" 10 | % will be considered to be corelated (correlation coefficient matrix); if corr<0, 11 | % the it means value below "corr_level" will be considered to be 12 | % corelated. (for p-value matrix) 13 | % 14 | % Output: 15 | % -------- 16 | % - corr_group: correlation groups for every signal item (each row or each col, which represents 17 | % a signal item), 1*N cell 18 | % - corr_idx_matrix: correlation index matrix, 1-independent; 0-correlated 19 | % 20 | % Info: 21 | % -------- 22 | % Created: Zhihong Zhang , 2020-04-27 23 | % Last Modified: Zhihong Zhang, 2020-04-27 24 | % 25 | % Copyright (c) 2020 Zhihong Zhang. 26 | 27 | corr_idx_matrix = corr_matrix; 28 | 29 | 30 | if corr_level<0 31 | corr_idx_matrix(corr_matrix < -corr_level) = 0; % correlated 32 | corr_idx_matrix(corr_matrix >= -corr_level) = 1; % independent 33 | else 34 | corr_idx_matrix(corr_matrix > corr_level) = 0; 35 | corr_idx_matrix(corr_matrix <= corr_level) = 1; 36 | end 37 | 38 | % diagonal elements are self-correlated for sure 39 | N = size(corr_idx_matrix, 1); % corr_idx_matrix number of rows 40 | corr_idx_matrix(eye(N, 'logical')) = 0; 41 | 42 | figure,imshow(corr_idx_matrix) 43 | title('corr-idx-matrix:1-independent; 0-correlated') 44 | 45 | 46 | corr_group = cell(1,N); 47 | 48 | for k=1:N 49 | corr_group{k} = find(corr_idx_matrix(k,:)==0); 50 | corr_group{k} = [k, corr_group{k}]; 51 | end 52 | end 53 | 54 | 55 | 56 | -------------------------------------------------------------------------------- /utils/dist_map.m: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Design-and-Simulation/CACTI-Simulation/2d5936d2e5ddb3d86fc071aea73f6da1661d2ddc/utils/dist_map.m -------------------------------------------------------------------------------- /utils/dvpdata_normalize.m: -------------------------------------------------------------------------------- 1 | % function [mask, meas] = dvpdata_normalize(init_mask, init_meas) 2 | function varargout = dvpdata_normalize(varargin) 3 | %DVPDATA_NORMALIZE normalize the mask_bayer and meas_bayer with the 4 | %maximal value of mask_bayer for the dvp algorithm reconstruction. 5 | % 6 | % Input 7 | % ------ 8 | % "mask_bayer and meas_bayer" or "dataset path and save path" 9 | % 10 | % Output 11 | % ------ 12 | % normalized "mask_bayer and meas_bayer" or directly save normalized 13 | % dataset 14 | % 15 | 16 | % input setting 17 | if isnumeric(varargin{1}) 18 | init_mask = varargin{1}; 19 | init_meas = varargin{2}; 20 | mask_max = max(init_mask,[],'a'); 21 | mask = init_mask./ mask_max; 22 | meas = init_meas./ mask_max; 23 | varargout{1} = mask; 24 | varargout{2} = meas; 25 | elseif ischar(varargin{1}) || isstring(varargin{1}) 26 | dataset_path = varargin{1}; 27 | if nargin<2 28 | save_path = [dataset_path(1:end-4) '_normalized.mat']; % default_save_path 29 | else 30 | save_path = varargin{2}; 31 | end 32 | 33 | % load 34 | init_mask = load(dataset_path, 'mask_bayer'); 35 | init_mask = init_mask.mask_bayer; 36 | init_meas = load(dataset_path, 'meas_bayer'); 37 | init_meas = init_meas.meas_bayer; 38 | orig_bayer = load(dataset_path, 'orig_bayer'); 39 | orig_bayer = orig_bayer.orig_bayer; 40 | 41 | % normalize 42 | mask_max = max(init_mask,[],'a'); 43 | mask_bayer = init_mask./ mask_max; 44 | meas_bayer = init_meas./ mask_max; 45 | 46 | % save 47 | save(save_path,'orig_bayer','mask_bayer','meas_bayer','-v7.3') 48 | disp(['normalized dataset saved to: ' save_path]) 49 | else 50 | error("error input") 51 | end 52 | 53 | 54 | end 55 | 56 | -------------------------------------------------------------------------------- /utils/gen_params.m: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Design-and-Simulation/CACTI-Simulation/2d5936d2e5ddb3d86fc071aea73f6da1661d2ddc/utils/gen_params.m -------------------------------------------------------------------------------- /utils/gen_struct_params.m: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Design-and-Simulation/CACTI-Simulation/2d5936d2e5ddb3d86fc071aea73f6da1661d2ddc/utils/gen_struct_params.m -------------------------------------------------------------------------------- /utils/graphIndepSets.m: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Design-and-Simulation/CACTI-Simulation/2d5936d2e5ddb3d86fc071aea73f6da1661d2ddc/utils/graphIndepSets.m -------------------------------------------------------------------------------- /utils/gray_mask.m: -------------------------------------------------------------------------------- 1 | function mask = gray_mask(sz, mode, param) 2 | %GRAY_MASK generate a gray mask with given size and other info. 3 | % 4 | %The gray mask can be generated in a random or determined way. 5 | %It can also generate a gray mask from given image. 6 | % 7 | % Input: 8 | % -------- 9 | % - size: mask size, scalar or int 2D-vector 10 | % 11 | % - mode: mask's generating method, str, {'randn', 'rand', 'fixed', 'image'}, 12 | % optional, default='rand'. 'randn' use RANDN to generate a random mask, 13 | % and 'rand' use RAND to generate a random mask. The other modes generate fixed 14 | % mask from given request or image. 15 | % 16 | % - param: 17 | % - for 'rand' mode: 'param' is a vector containing [a, b, kernel_size], 18 | % a,b-float, 0-1; kernel_size-int scalar; a and b is the boundary of 19 | % the uniform distribution, which determines the average transmittance and variance. 20 | % And kernel_size determines the average filter kernel size, which affects 21 | % the granularity of gray mask, default=[0, 1, 1]; 22 | % 23 | % - for 'randn' mode: 'param' is a vector containing [mu, sigma, 24 | % kernel_size], mu-float, [0 1]; sigma-float; kernel_size-int scalar. 25 | % mu and sigma determine the gaussian random distribution, which 26 | % affects the average transmittance and variance. And kernel_size 27 | % determines the average filter kernel size, which affects the granularity 28 | % of gray mask, default=[0.5,0.5,1]; 29 | % 30 | % - for 'fixed' mode: 'param' is the pattern type, str, 31 | % {'up_half', 'down_half'} 32 | % 33 | % - for 'image' mode: 'param' is an image, it will be resized to 34 | % given size and then converted to a binary matrix with 'im2bw' 35 | % 36 | % Output: 37 | % -------- 38 | % - mask: gray mask, float 2D matrix,[0 1] 39 | % 40 | % Example: 41 | % -------- 42 | % mask = GRAY_MASK(mask_size); % 0-1 uniform disstribution random mask 43 | % (equal to: mask = GRAY_MASK(mask_size, 'rand'), 'rand' can be omitted, similarly hereinafter); 44 | % 45 | % mask = GRAY_MASK(mask_size, [a, b]); % a-b uniform disstribution 46 | % 47 | % mask = GRAY_MASK(mask_size, [a, b, kernel_size]); % a-b uniform disstribution 48 | % random mask, with a average convolution of given kernel_size 49 | % 50 | % 51 | % mask = GRAY_MASK(mask_size, 'randn'); % guassian disstribution mask, 52 | % m=0 and sigma=1 53 | % 54 | % mask = GRAY_MASK(mask_size, 'randn', [mu, sigma]); % guassian disstribution mask, 55 | % mu and sigma are given 56 | % 57 | % mask = GRAY_MASK(mask_size, 'randn', [mu, sigma, kernel_size]); % guassian disstribution mask, 58 | % mu and sigma are given , with a average convolution of given kernel_size 59 | % 60 | % See also: 61 | % -------- 62 | % BINARY_MASK, RANDN 63 | % 64 | % Log: 65 | % -------- 66 | % 67 | % Info: 68 | % -------- 69 | % Created: Zhihong Zhang , 2020-03-30 70 | % Last Modified: Zhihong Zhang, 2020-05-04 71 | % 72 | % Copyright 2020 Zhihong Zhang 73 | 74 | % input setting 75 | narginchk(1,3); 76 | if nargin == 1 77 | % default mode-'rand' with default params 78 | mode = 'rand'; 79 | param = [0 1 1]; 80 | elseif nargin == 2 81 | if isnumeric(mode) && isvector(mode) 82 | % default mode-'rand' with assigned params 83 | param = mode; 84 | mode = 'rand'; 85 | elseif strcmp(mode,'rand') 86 | % default mode-'rand' with default params 87 | param = [0,1,1]; 88 | elseif strcmp(mode,'randn') 89 | % default mode-'randn' with default params 90 | param = [0.5,0.5,1]; 91 | else 92 | error('input error!'); 93 | end 94 | end 95 | 96 | if isscalar(sz) 97 | sz = [sz sz]; 98 | end 99 | 100 | % mask generating 101 | switch mode 102 | case 'rand' 103 | if isnumeric(param) && isvector(param) 104 | param_num = numel(param); 105 | if param_num == 3 106 | a = param(1); b = param(2); kernel_size = param(3); 107 | elseif param_num == 2 108 | a = param(1); b = param(2); kernel_size = 1; 109 | elseif param_num == 1 110 | a = 0; b = param(1); kernel_size = 1; 111 | end 112 | mask = a + (b-a).*rand(sz); % use uniform random distribution 113 | if kernel_size~=1 114 | nchannel = size(mask, 3); 115 | kernel = fspecial('average',kernel_size); 116 | if nchannel==1 117 | mask = filter2(kernel, mask); 118 | else 119 | for k=1:nchannel 120 | mask(:,:,k) = filter2(kernel, mask(:,:,k)); 121 | end 122 | end 123 | end 124 | else 125 | error('error input - param') 126 | end 127 | case 'randn' 128 | if isnumeric(param) && isvector(param) 129 | param_num = numel(param); 130 | if param_num == 3 131 | mu = param(1); sigma = param(2); kernel_size = param(3); 132 | elseif param_num == 2 133 | mu = param(1); sigma = param(2); kernel_size = 1; 134 | elseif param_num == 1 135 | mu = param(1); sigma = 0.5; kernel_size = 1; 136 | end 137 | 138 | mask = mu + sigma*randn(sz); % use gaussian random distribution 139 | if kernel_size~=1 140 | nchannel = size(mask, 3); 141 | kernel = fspecial('average',kernel_size); 142 | if nchannel==1 143 | mask = filter2(kernel, mask); 144 | else 145 | for k=1:nchannel 146 | mask(:,:,k) = filter2(kernel, mask(:,:,k)); 147 | end 148 | end 149 | end 150 | else 151 | error('error input - param') 152 | end 153 | case 'fixed' 154 | error('Not implement') 155 | case 'image' 156 | if isnumeric(param) && ismatrix(param) 157 | img = im2double(imresize(param, sz)); 158 | mask = img/max(img,[],'all'); 159 | elseif isnumeric(param) && (ndims(param)==3 && size(param,3)==3) 160 | img = im2double(imresize(rgb2gray(param), sz)); 161 | mask = img/max(img,[],'all'); 162 | else 163 | error('error arguement for - "%s"', mode); 164 | end 165 | end 166 | 167 | % clamp to 0-1 168 | mask = max(mask, 0); 169 | mask = min(mask, 1); 170 | end 171 | -------------------------------------------------------------------------------- /utils/idx_matrix.m: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Design-and-Simulation/CACTI-Simulation/2d5936d2e5ddb3d86fc071aea73f6da1661d2ddc/utils/idx_matrix.m -------------------------------------------------------------------------------- /utils/matrix_corr.m: -------------------------------------------------------------------------------- 1 | function [corr_mat, p_mat, aver_corr, aver_p] = matrix_corr(X, varargin) 2 | %MATRIX_CORR calculate the correlation coefficient of given matrix 3 | % convert the matrixs to column vector and calculate their correlation 4 | % coefficient matrix and average correlation coefficient 5 | % 6 | % Input: 7 | % ------- 8 | % X: input matrix, 2D or 3D numerical matrix (3D means matrix is stacked along 9 | % 3rd axis, Y is omitted in these case) 10 | % varargin: more input matrix, 2D numerical matrix, take effect when X is 11 | % also a 2D numerical matrix 12 | % 13 | % Output: 14 | % corr_mat: correlation coefficient matrix of columns (straightened 15 | % matrix) 16 | % p_mat: p value matrix of columns 17 | % aver_corr: average correlation coefficient of different columns 18 | % aver_p: average p-value of different columns 19 | % 20 | % Note: 21 | % -------- 22 | % This funcion can be substituted with built-in function CORRCOEF 23 | % 24 | % 25 | % Info: 26 | % -------- 27 | % Created: Zhihong Zhang , 2020-04-06 28 | % Last Modified: Zhihong Zhang, 2020-04-06 29 | % 30 | % Copyright (c) 2020 Zhihong Zhang. 31 | 32 | 33 | Y = cell2mat(varargin); 34 | 35 | if nargin == 1 && isnumeric(X) && (ndims(X)==3) 36 | matrix_stack = X; 37 | 38 | elseif nargin > 1 && ismatrix(Y) && (isnumeric(Y)||islogical(Y)) 39 | 40 | Y = reshape(Y, [size(varargin{1}), numel(varargin)]); 41 | matrix_stack = cat(3,X,Y); 42 | 43 | else 44 | error("multi inputs--2D matrixs or one input--single 3D matrix is valid inputs") 45 | end 46 | 47 | matrix_stack = double(matrix_stack); % int is not supported by corr 48 | 49 | matrix_stack_sz =size(matrix_stack); 50 | X2col = reshape(matrix_stack, [prod(matrix_stack_sz(1:2)) matrix_stack_sz(3)]); 51 | [corr_mat, p_mat] = corrcoef(X2col); 52 | 53 | corr_mat_triu = triu(corr_mat, 1); 54 | aver_corr = sum(abs(corr_mat_triu), 'all')/nchoosek(matrix_stack_sz(3),2); 55 | 56 | p_mat_triu = triu(p_mat, 1); 57 | aver_p = sum(abs(p_mat_triu), 'all')/nchoosek(matrix_stack_sz(3),2); 58 | end 59 | -------------------------------------------------------------------------------- /utils/origin2center.m: -------------------------------------------------------------------------------- 1 | function origin2center(ax) 2 | %ORIGIN2CENTER put the axis's origin to the center of the figure 3 | % 4 | % Input: axes's handle 5 | 6 | 7 | % extend the x,y axis lengths 8 | xL=xlim; 9 | yL=ylim; 10 | extend_x = ( xL(2)-xL(1) ) * 0.1 ; 11 | extend_y = ( yL(2)-yL(1) ) * 0.1 ; 12 | xxL = xL + [ -extend_x extend_x] ; 13 | yyL = yL + [ -extend_y extend_y] ; 14 | set(gca,'xlim', xxL) ; 15 | set(gca,'ylim', yyL) ; 16 | 17 | % put origin to center 18 | ax.XAxisLocation = 'origin'; 19 | ax.YAxisLocation = 'origin'; 20 | ax.Box = 'off'; 21 | 22 | pos = get(gca,'Position'); 23 | x_Lim = get(gca,'Xlim'); 24 | y_Lim = get(gca,'Ylim'); 25 | if prod(y_Lim)>0 26 | position_x = [pos(1), pos(2)+pos(4)/2, pos(3), eps]; 27 | else 28 | position_x = [pos(1), pos(2)-y_Lim(1)/diff(y_Lim)*pos(4), pos(3), eps]; 29 | end 30 | if prod(x_Lim)>0 31 | position_y = [pos(1)+pos(3)/2, pos(2), eps, pos(4)]; 32 | else 33 | position_y = [pos(1)-x_Lim(1)/diff(x_Lim)*pos(3), pos(2), eps, pos(4)]; 34 | end 35 | annotation('arrow', [pos(1)-0.065*pos(3), pos(1)+pos(3)+0.065*pos(3)], ... 36 | [position_x(2)-0.001,position_x(2)-0.001],'HeadLength',6,'HeadWidth',6); 37 | annotation('arrow', [position_y(1)+0.001, position_y(1)+0.001],... 38 | [pos(2)-0.065*pos(4),pos(2)+pos(4)+0.065*pos(4)],... 39 | 'HeadLength',6,'HeadWidth',6); 40 | end -------------------------------------------------------------------------------- /utils/params_function_test.mlx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Design-and-Simulation/CACTI-Simulation/2d5936d2e5ddb3d86fc071aea73f6da1661d2ddc/utils/params_function_test.mlx -------------------------------------------------------------------------------- /utils/previous_version/gen_params_v1.m: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Design-and-Simulation/CACTI-Simulation/2d5936d2e5ddb3d86fc071aea73f6da1661d2ddc/utils/previous_version/gen_params_v1.m -------------------------------------------------------------------------------- /utils/previous_version/load_struct_params.old: -------------------------------------------------------------------------------- 1 | function params = load_struct_params(path) 2 | %LOAD_STRUCT_PARAMS: load struct type optical system parameters 3 | % input: 4 | % - non-input: using default parameters 5 | % - path: parameter loading path, str 6 | % output: 7 | % - params: struct type parameters 8 | % note: 9 | % - this function is not recommended, as it can only deal with struct data. 10 | % And it can be substituted with "gen_params()" and "load()" 11 | if nargin ~= 0 12 | % load params from file 13 | params = load(path); 14 | else 15 | % use default parameter settings 16 | % objective 17 | params.obj_pos = -5; % objective distance -50 mm 18 | params.obj_size = [200,200]; % pixel number 19 | params.obj_pix_size = 5e-3; % 0.5um sampling(representing) size 20 | params.obj = 255*ones(params.obj_size); % obj pattern, white board 21 | 22 | % lens 23 | params.lens_f = 2.5; % focal length 24 | params.lens_radius = 0.5; % lens radius 25 | 26 | % DMD 27 | params.dmd_pos = 2.5; % dmd's position 28 | params.dmd_size = [100,100]; % pixel number 29 | params.dmd_pix_size = 10e-3; % 10um pixel size 30 | params.dmd_t = 0.5; % dmd's transmission rate [standard] 31 | params.dmd = binary_mask(params.dmd_size, params.dmd_t); % dmd pattern 32 | 33 | 34 | % mask 35 | params.mask_pos = 4.5; % mask's position 36 | params.mask_size = [200,200]; % pixel number 37 | params.mask_pix_size = 5e-3; % 10um pixel size 38 | params.mask_t = 0.5; % mask's transmission rate [standard] 39 | params.mask = binary_mask(params.mask_size,params.mask_t); % mask pattern 40 | 41 | % sensor 42 | params.sensor_pos = []; 43 | params.sensor_size = [100, 100]; % pixel number 44 | params.sensor_pix_size = [10e-3, 10e-3]'; % 5um pixel size 45 | params.sensor = zeros(params.sensor_size); % image pattern 46 | params.large_sensor_flag = 1; % assuming sensor is large enough 47 | 48 | end 49 | 50 | end 51 | 52 | % bak 53 | %{ 54 | % objective 55 | obj_pos = -5; % objective distance -50 mm 56 | obj_size = [200,200]; % pixel number 57 | obj_pix_size = 5e-3; % 0.5um sampling(representing) size 58 | obj = 255*ones(obj_size); % obj pattern, white board 59 | 60 | % lens 61 | lens_f = 2.5; % focal length 62 | lens_radius = 0.5; % lens radius 63 | 64 | % DMD 65 | % dmd_pos = 0; 66 | dmd_pos = 2.5; % [standard] 67 | dmd_size = [100,100]; % pixel number 68 | dmd_pix_size = 10e-3; % 10um pixel size 69 | dmd_t = 0.5; % dmd's transmission rate [standard] 70 | % dmd_t = 1; % dmd's transmission rate 71 | dmd = binary_mask(dmd_size, dmd_t); % dmd pattern 72 | % load dmd.mat 73 | 74 | % mask 75 | mask_pos = 5; 76 | % mask_pos = 4.5;% [standard] 77 | mask_size = [200,200]; % pixel number 78 | mask_pix_size = 5e-3; % 10um pixel size 79 | mask_t = 0.5; % mask's transmission rate [standard] 80 | mask = binary_mask(mask_size,mask_t); % mask pattern 81 | load mask.mat 82 | 83 | % sensor 84 | sensor_pos = []; 85 | sensor_size = [100, 100]; % pixel number 86 | sensor_pix_size = [10e-3, 10e-3]'; % 5um pixel size 87 | sensor = zeros(sensor_size); % image pattern 88 | large_sensor_flag = 1; % assuming sensor is large enough 89 | %} 90 | -------------------------------------------------------------------------------- /utils/previous_version/mask_propagate.old: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Design-and-Simulation/CACTI-Simulation/2d5936d2e5ddb3d86fc071aea73f6da1661d2ddc/utils/previous_version/mask_propagate.old -------------------------------------------------------------------------------- /utils/print2log.m: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Design-and-Simulation/CACTI-Simulation/2d5936d2e5ddb3d86fc071aea73f6da1661d2ddc/utils/print2log.m -------------------------------------------------------------------------------- /utils/print_params.m: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Design-and-Simulation/CACTI-Simulation/2d5936d2e5ddb3d86fc071aea73f6da1661d2ddc/utils/print_params.m -------------------------------------------------------------------------------- /utils/run_time.m: -------------------------------------------------------------------------------- 1 | function time = run_time(command_strs, run_times) 2 | %TEST_TIME test command's run time 3 | % 4 | % Input: 5 | % -------- 6 | % command_strs: command that need to test the run time, string vector 7 | % run_times: command's repeate times 8 | % 9 | % Output: 10 | % -------- 11 | % time: command's run time(s), float 12 | 13 | if nargin<2 14 | run_times = 1; 15 | end 16 | 17 | % start timer 18 | tic 19 | 20 | % run commands 21 | for k=1:run_times 22 | for command = command_strs 23 | evalin('base', command_strs) 24 | end 25 | end 26 | 27 | % stop timer 28 | time = toc; 29 | 30 | fprintf('\n time consumed: %f s\n', time); 31 | 32 | end 33 | 34 | -------------------------------------------------------------------------------- /utils/sub2coord.m: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Design-and-Simulation/CACTI-Simulation/2d5936d2e5ddb3d86fc071aea73f6da1661d2ddc/utils/sub2coord.m --------------------------------------------------------------------------------