如何在Matlab中将图像从直角坐标转换为极坐标?

6mzjoqzu  于 2022-11-24  发布在  Matlab
关注(0)|答案(1)|浏览(409)

我正在尝试将图像的像素从x-y坐标转换为极坐标,但遇到了问题,因为我想自己编写函数。以下是我目前为止所做的代码:

function [ newImage ] = PolarCartRot
% read and show the image
image= imread('1.jpg');
%%imshow(image);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%change to polar coordinate
[x y z]= size(image);
r = sqrt(x*x+y*y);
theta = atan2(y,x);
for i =0:r
    for j= 0:theta
newpixel = [i; j];
newImage(newpixel(1), newpixel(2),:) = image(i,j,:);
    end
end
figure;
imshow (newImage);
5cnsuln7

5cnsuln71#

我不太清楚你想做什么,这就是为什么我要做出我自己的榜样......
给定一幅图像,我将像素的x/y坐标从笛卡尔坐标转换为极坐标CART2POL
在第一张图中,我显示了点的位置,在第二张图中,我绘制了原始图像和极坐标图像。
请注意,我使用的是图像处理工具箱中的WARP函数,它使用SURF/SURFACE函数来显示纹理Map图像。

% load image 
load clown;
img = ind2rgb(X,map);
%img = imread(...);   % or use any other image

% convert pixel coordinates from cartesian to polar
[h,w,~] = size(img);
[X,Y] = meshgrid(1:w,1:h);
[theta,rho] = cart2pol(X, Y);
Z = zeros(size(theta));

% show pixel locations (subsample to get less dense points)
XX = X(1:8:end,1:4:end);
YY = Y(1:8:end,1:4:end);
tt = theta(1:8:end,1:4:end);
rr = rho(1:8:end,1:4:end);
subplot(121), scatter(XX(:),YY(:),3,'filled'), axis ij image
subplot(122), scatter(tt(:),rr(:),3,'filled'), axis ij square tight

% show images
figure
subplot(121), imshow(img), axis on
subplot(122), warp(theta, rho, Z, img), view(2), axis square

x1c 0d1x

编辑

正如我最初所说,问题并不清楚。你必须以一种定义良好的方式描述你想要的Map...
例如,在转换为极坐标之前,您需要考虑原点的位置。上一个示例假设原点是(0,0)处的轴。假设您要将图像(w/2,h/2)的中心作为原点,则可以这样做:

[X,Y] = meshgrid((1:w)-floor(w/2), (1:h)-floor(h/2));

为了更好地说明效果,考虑一个用笛卡尔坐标系绘制的concentric circles的源图像,并注意当使用圆心作为原点时,它们是如何Map到极坐标系中的直线的:

编辑

下面是另一个例子,说明如何按照注解中的要求在极坐标中显示图像。注意,我们在反方向pol2cart上执行Map:

[h,w,~] = size(img);
s = min(h,w)/2;
[rho,theta] = meshgrid(linspace(0,s-1,s), linspace(0,2*pi));
[x,y] = pol2cart(theta, rho);
z = zeros(size(x));
subplot(121), imshow(img)
subplot(122), warp(x, y, z, img), view(2), axis square tight off

同样,如果你给它一个带有直线的输入图像,并查看它们如何在极坐标中Map(垂直线变成圆,水平线变成从原点发出的射线),效果会更好:
x1c4d 1x指令集

相关问题