How can I optimize the the code below which assigns grey-scale color values to a color array from pixels in an image?
Hi!
I am trying to optimise the code below, which is the only part of a program i have made which uses for loops, and as such is fairly time consuming.
The program essentially uses two cameras, with their positions calibrated and defined relative to the position of an object they are imaging. The object is created in the virtual space by importing it as a point cloud and converting this to a triangulated surface. The code uses information about the normal vector to each triangle on the surface to figure out whether each camera can actually "see" that point on the object in the real world, and then uses the relevant pixel data for that point (each point is mapped onto the image to find out which pixel refers to it) to assign the correct colour value to the color array for the point cloud.
Any advice would be appreciated, I’m fairly new to matlab and definitely new to the concept of eliminating for loops and code optimisation.
If i haven’t explained the code clearly enough please let me know.
Thanks,
Toby
%Assign color to points if the surface faces towards the relevant camera
for m=1:numberoffaces
camera1vector=locationcamera1-P(m,:);
camera2vector=locationcamera2-P(m,:);
if (dot(camera1vector,F(m,:)))<0
colors(m)= imageGL(pixelposition1(m,2),pixelposition1(m,1));
colors(m)= imageGL(pixelposition1(m,2),pixelposition1(m,1));
colors(m)= imageGL(pixelposition1(m,2),pixelposition1(m,1));
elseif (dot(camera2vector,F(m,:)))<0
colors(m)= imageGR(pixelposition2(m,2),pixelposition2(m,1));
colors(m)= imageGR(pixelposition2(m,2),pixelposition2(m,1));
colors(m)= imageGR(pixelposition2(m,2),pixelposition2(m,1));
else
colors(m)= (0);
colors(m)= (0);
colors(m)= (0);
end
endHi!
I am trying to optimise the code below, which is the only part of a program i have made which uses for loops, and as such is fairly time consuming.
The program essentially uses two cameras, with their positions calibrated and defined relative to the position of an object they are imaging. The object is created in the virtual space by importing it as a point cloud and converting this to a triangulated surface. The code uses information about the normal vector to each triangle on the surface to figure out whether each camera can actually "see" that point on the object in the real world, and then uses the relevant pixel data for that point (each point is mapped onto the image to find out which pixel refers to it) to assign the correct colour value to the color array for the point cloud.
Any advice would be appreciated, I’m fairly new to matlab and definitely new to the concept of eliminating for loops and code optimisation.
If i haven’t explained the code clearly enough please let me know.
Thanks,
Toby
%Assign color to points if the surface faces towards the relevant camera
for m=1:numberoffaces
camera1vector=locationcamera1-P(m,:);
camera2vector=locationcamera2-P(m,:);
if (dot(camera1vector,F(m,:)))<0
colors(m)= imageGL(pixelposition1(m,2),pixelposition1(m,1));
colors(m)= imageGL(pixelposition1(m,2),pixelposition1(m,1));
colors(m)= imageGL(pixelposition1(m,2),pixelposition1(m,1));
elseif (dot(camera2vector,F(m,:)))<0
colors(m)= imageGR(pixelposition2(m,2),pixelposition2(m,1));
colors(m)= imageGR(pixelposition2(m,2),pixelposition2(m,1));
colors(m)= imageGR(pixelposition2(m,2),pixelposition2(m,1));
else
colors(m)= (0);
colors(m)= (0);
colors(m)= (0);
end
end Hi!
I am trying to optimise the code below, which is the only part of a program i have made which uses for loops, and as such is fairly time consuming.
The program essentially uses two cameras, with their positions calibrated and defined relative to the position of an object they are imaging. The object is created in the virtual space by importing it as a point cloud and converting this to a triangulated surface. The code uses information about the normal vector to each triangle on the surface to figure out whether each camera can actually "see" that point on the object in the real world, and then uses the relevant pixel data for that point (each point is mapped onto the image to find out which pixel refers to it) to assign the correct colour value to the color array for the point cloud.
Any advice would be appreciated, I’m fairly new to matlab and definitely new to the concept of eliminating for loops and code optimisation.
If i haven’t explained the code clearly enough please let me know.
Thanks,
Toby
%Assign color to points if the surface faces towards the relevant camera
for m=1:numberoffaces
camera1vector=locationcamera1-P(m,:);
camera2vector=locationcamera2-P(m,:);
if (dot(camera1vector,F(m,:)))<0
colors(m)= imageGL(pixelposition1(m,2),pixelposition1(m,1));
colors(m)= imageGL(pixelposition1(m,2),pixelposition1(m,1));
colors(m)= imageGL(pixelposition1(m,2),pixelposition1(m,1));
elseif (dot(camera2vector,F(m,:)))<0
colors(m)= imageGR(pixelposition2(m,2),pixelposition2(m,1));
colors(m)= imageGR(pixelposition2(m,2),pixelposition2(m,1));
colors(m)= imageGR(pixelposition2(m,2),pixelposition2(m,1));
else
colors(m)= (0);
colors(m)= (0);
colors(m)= (0);
end
end point cloud, for loop, for, performance, optimization, calibration, colormap, image processing, stereo imaging MATLAB Answers — New Questions