如何在Matlab中将图像从笛卡尔坐标更改为极坐标

如何在Matlab中将图像从笛卡尔坐标更改为极坐标

本文介绍了如何在Matlab中将图像从笛卡尔坐标更改为极坐标?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

function [ newImage ] = PolarCartRot% 读取并显示图像image= imread('1.jpg');%%imshow(图像);%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%更改为极坐标[x y z]= 大小(图像);r = sqrt(x*x+y*y);theta = atan2(y,x);对于 i = 0:r对于 j= 0:theta新像素 = [i;j];newImage(newpixel(1), newpixel(2),:) = image(i,j,:);结尾结尾数字;imshow (newImage);
解决方案

你想做什么不是很清楚,这就是为什么我自己做例子......

所以给定一个图像,我正在将像素 x/y 坐标从笛卡尔坐标转换为极坐标 的源图像,并注意它们如何映射到直线在以圆心为原点的极坐标中:

编辑

这是如何按照评论中的要求以极坐标显示图像的另一个示例.请注意,我们在反方向执行映射pol2cart:

[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 = 零(大小(x));子图(121),imshow(img)subplot(122), warp(x, y, z, img), view(2), 轴方形紧

再一次,如果你用直线输入输入图像,效果会更好,看看它们如何在极坐标中映射(垂直线变成圆,水平线变成从原点发出的光线):

I am trying to convert the pixels of an image from x-y coordinate to polar coordinate and I have problem with it, as I want to code the function by myself.Here is the code I did so far:

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);
解决方案

It is not quite clear what you are trying to do, which is why I am making my own example...

So given an image, I am converting the pixel x/y coordinates from Cartesian to polar with CART2POL.

In the first figure, I am showing the locations of the points, and in the second, I plot both the original image and the one with polar coordinates.

Note that I am using the WARP function from the Image Processing Toolbox. Under the hood, it uses the SURF/SURFACE function to display a texture-mapped image.

% 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

EDIT

As I originally stated, the question is not clear. You have to describe the mapping you want in a well defined manner...

For one you need to think about where the origin is located before converting to polar coordinates. The previous example assume the origin to be the axes base at (0,0). Suppose you want to take the center of the image (w/2,h/2) as origin, then you would do this instead:

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

with the rest of the code unchanged. To better illustrate the effect, consider a source image with concentric circles drawn in Cartesian coordinates, and notice how they map to straight lines in polar coordinates when using the center of the circles as origin:


EDIT

Here is another example of how to display an image in polar coordinates as requested in the comments. Note that we perform the mapping in the inverse direction pol2cart:

[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

Again the effect is better show if you feed it an input image with straight lines, and see how they map in polar coordinates (vertical lines become circles, and horizontal lines become rays emanating from the origin):

这篇关于如何在Matlab中将图像从笛卡尔坐标更改为极坐标?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-04 22:48