-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrays2img.m
50 lines (45 loc) · 2 KB
/
rays2img.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
function [img, x, y] = rays2img(rays_x, rays_y, width, Npixels)
% rays2img - Simulates the operation of a camera sensor, where each pixel
% simply collects (i.e., counts) all of the rays that intersect it. The
% image sensor is assumed to be square with 100% fill factor (no dead
% areas) and 100% quantum efficiency (each ray intersecting the sensor is
% collected).
%
% inputs:
% rays_x: A 1 x N vector representing the x position of each ray in meters.
% rays_y: A 1 x N vector representing the y position of each ray in meters.
% width: A scalar that specifies the total width of the image sensor in
% meters.
% Npixels: A scalar that specifies the number of pixels along one side of
% the square image sensor.
%
% outputs:
% img: An Npixels x Npixels matrix representing a grayscale image captured
% by an image sensor with a total Npixels^2 pixels.
% x: A 1 x 2 vector that specifies the x positions of the left and right
% edges of the imaging sensor in meters.
% y: A 1 x 2 vector that specifies the y positions of the bottom and top
% edges of the imaging sensor in meters.
%
% Matthew Lew 11/27/2018
% 11/26/2021 - edited to create grayscale images from a rays_x, rays_y
% vectors
% 11/9/2022 - updated to fix axis flipping created by histcounts2()
% eliminate rays that are off screen
onScreen = abs(rays_x) < width / 2 & abs(rays_y) < width / 2;
x_in = rays_x(onScreen);
y_in = rays_y(onScreen);
% separate screen into pixels, calculate coordinates of each pixel's edges
mPerPx = width/Npixels;
Xedges = ((1:Npixels + 1) - (1 + Npixels + 1) / 2) * mPerPx;
Yedges = ((1:Npixels + 1) - (1 + Npixels + 1) / 2) * mPerPx;
% count rays at each pixel within the image
% histcounts2 for some reason assigns x to rows, y to columns
img = histcounts2(y_in, x_in, Yedges, Xedges);
% rescale img to uint8 dynamic range
img = uint8(round(img/max(img(:)) * 255));
x = Xedges([1 end]);
y = Yedges([1 end]);
% figure;
% image(x_edges([1 end]),y_edges([1 end]),img); axis image xy;
end