Is it possible to loop through and set image pixels with a parfor loop?
조회 수: 12 (최근 30일)
이전 댓글 표시
This code transforms a 360 degree image, but it is it slow with images at high resolutions. I was wondering if it was possible to convert one of the loops to a parfor loop to cut down on the time it takes to run the transformations.
Below is the code:
% loop through every pixel of am image
for row = 1 : width
% find pixel theta (elevation)
theta = (90/width) * (row + (row-1));
% transform theta into z cartesian coordinates
z = cosd(theta);
for col = 1 : 1: length
% find pixel psi (azimuth)
psi = (180/length) * (col + (col-1)) - 180;
% transform theta and psi into x and y cartesian coordinates
x = sind(theta) * cosd(psi);
y = sind(theta) * sind(psi);
% roll transformation
yNew = y * cosd(roll) - z * sind(roll);
zNew = y * sind(roll) + z * cosd(roll);
% pitch transformation
xNew = x * cosd(-pitch) + zNew * sind(-pitch);
zNew = zNew * cosd(-pitch) - x * sind(-pitch);
% yaw transformation
xYaw = xNew;
xNew = xYaw * cosd(yaw) - yNew * sind(yaw);
yNew = xYaw * sind(yaw) + yNew * cosd(yaw);
% transform x, y, and z back to spherical coordinates
psiNew = atan2d(yNew, xNew);
thetaNew = acosd(zNew);
% find new pixel row from theta
rowNew = round((thetaNew*width)/180 + 0.5);
% find new pixel column from psi
colNew = round((psiNew*length)/360 + length/2 + 0.5);
% map pixels from the original image to a new one
try
transformedImage(row, col, :) = app.newImage(rowNew, colNew, :); % THIS WONT WORK W/ A PARFOR LOOP
catch
disp("Psi/theta out of bounds:");
disp(psiNew + ", " + thetaNew);
disp(rowNew + ", " + colNew);
end
end
end
댓글 수: 1
Constantino Carlos Reyes-Aldasoro
2022년 8월 2일
In general it is best not to use loops through matrices, that is the whole power of Matlab that considers everything a matrix. I.e., you can run through every pixel of an image and compare to a certain value:
myImage = rand(32,32);
for k1=1:32
for k2=1:32
myImage2(k1,k2) = myImage(k1,k2)>0.5;
end
end
imagesc(myImage2)
Or you can simply work as matrices:
myImage3 = (myImage>0.5);
imagesc(myImage3)
Thus, try to avoid loops and work with your data as a matrix.
채택된 답변
Walter Roberson
2022년 8월 2일
transformedImage(row, col, :) = app.newImage(rowNew, colNew, :); % THIS WONT WORK W/ A PARFOR LOOP
Change that to something like
transformedrow(col, :) = newImage(rowNew, colNew, :);
and after the end of the col loop
transformedImage(row, :, :) = transformedrow;
And before the parfor
newImage = app.newImage;
When you have a variable indexed by the parfor loop variable, all other indices of the variable should be constants or arithmetic expressions that evaluate to things that are constant for the entire parfor.
댓글 수: 2
Walter Roberson
2022년 8월 3일
transformedImage(row, :, :) = transformedrow;
If you can rewrite the code to work down columns instead of across rows, then the performance would be improved.
추가 답변 (1개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Get Started with Phased Array System Toolbox에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!