Asking about the rounding numbers. (Problem with rotating image)

2 次查看(过去 30 天)
Hi,
the following is my question. With the line [ x-min(min(x))+1 ], x is a matrix, but min(min(x)) is a scalar. How are they subtracted? What is [ x-min(min(x))+1 ]? I don't get the code by other.
I = imread('pout.tif');
theta = 0.25*pi;
R = [cos(theta) -sin(theta);sin(theta) cos(theta)];
for i = 1:size(I,1)
for j = 1:size(I,2)
a = R*[i-1;j-1];
x(i,j)=a(1); %new x
y(i,j)=a(2); %new y
end
end
x=int16(x-min(min(x))+1);%rounded
y=int16(y-min(min(y))+1);%rounded
for i = 1:size(I,1)
for j = 1:size(I,2)
I_new(x(i,j),y(i,j)) = I(i,j);
end
end
subplot(1,2,1); imshow(I);
subplot(1,2,2); imshow(I_new);
  2 个评论
DGM
DGM 2022-2-4
编辑:DGM 2022-2-4
Assuming that x is 2D, then this
x-min(min(x))+1
subtracts the global minimum of x from x. This removes any offset. Since zero is not a valid index, 1 is added. The use of int16() is equivalent to just using round() in this context.
I'm also assuming that you're more concerned with why the very act of rounding the transformed indices is leaving holes in the image. Since you're mapping forward and placing source image pixels in a blank output image, the rounding is bound to mean that some output pixels have no source pixel mapped to them. You can either try mapping in reverse to find the source pixel for each output pixel, or you can use some sort of interpolation instead of direct indexing. The fact that you're making your own image rotation code means that you have some reason for doing so, and likely some corresponding limitations. I don't know what those might be.

请先登录,再进行评论。

采纳的回答

DGM
DGM 2022-2-4
编辑:DGM 2022-2-4
I mentioned mapping from output back to input. Here is one crude example. This still uses no interpolation.
inpict = imread('pout.tif');
theta = 45; % i'm going to use degrees
% adding 1px of black padding makes all clamped regions black
inpict = padarray(inpict,[1 1],0,'both');
% calculate output image size
% this can probably be simplified
s0 = size(inpict);
initialangle = atand(s0(1)/s0(2));
appliedangle = mod(theta,90);
quadrant = floor(theta/90+1);
bbox = [sind(appliedangle+initialangle) cosd(appliedangle-initialangle)];
if mod(quadrant,2) == 0
bbox = fliplr(bbox);
end
os0 = ceil(sqrt(sum(s0.^2)).*bbox)-2; % -2 removes 1px padding
% output coordinate space
x = linspace(-os0(2)/2,os0(2)/2,os0(2));
y = linspace(-os0(1)/2,os0(1)/2,os0(1));
[XX YY] = meshgrid(x,y);
% rotate subs
R = [cosd(theta) -sind(theta); sind(theta) cosd(theta)];
idx = (R*[XX(:) YY(:)].').';
XX = reshape(idx(:,1),os0); % devectorize
YY = reshape(idx(:,2),os0);
XX = min(max(XX + (s0(2)+1)/2,1),s0(2)); % center, clamp
YY = min(max(YY + (s0(1)+1)/2,1),s0(1));
% construct output
idx = sub2ind(s0,round(YY),round(XX)); % convert to linear index
outpict = reshape(inpict(idx),os0);
imshow(outpict)

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Image Processing Toolbox 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by