这是测试pol2cart()和cart2pol的代码:
clear all
close all
clc
% data = imread('D:\Projects\CarPool\TestData\test_img1.bmp');
data = phantom(128);
img = sum(data, 3);
[M, N] = size(img);
xvec = linspace(-N/2, N/2);
% convert pixel coordinates from cartesian to polar
[x, y] = meshgrid(xvec,xvec);
[theta, rho] = cart2pol(x, y);
[xx, yy] = pol2cart(theta, rho);
%# show pixel locations (subsample to get less dense points)
xdisp = x(1:8:end,1:4:end);
ydisp = y(1:8:end,1:4:end);
tdisp = theta(1:8:end,1:4:end);
rdisp = rho(1:8:end,1:4:end);
xxdisp = xx(1:8:end,1:4:end);
yydisp = yy(1:8:end,1:4:end);
% h = warp(xx, yy, zeros(size(xx)), img);
% imgWarp = get(h, 'FaceColor');imgWapr = sum(imgWarp,3);
% imgWarp = (imgWarp - min(imgWarp(:)))/max(imgWarp(:));
% img = (img - min(img(:)))/max(img(:));
% diffImg = img - imgWarp;
figure; clf
subplot(241)
scatter(xdisp(:),ydisp(:),3,'filled', 'red'), axis ij image
title('cartesian coordinates'); xlabel('x'); ylabel('y');
subplot(242)
scatter(tdisp(:),rdisp(:),3,'filled'), axis ij square tight,
title('cartesian to polar coordinates'); xlabel('\theta'); ylabel('\rho');
subplot(243)
scatter(xxdisp(:),yydisp(:),3,'filled'), axis ij square tight,
title('cartesian to polar coordinates'); xlabel('x'); ylabel('y');
subplot(244)
scatter(xdisp(:),ydisp(:),3,'filled', 'red'), axis ij image; hold on
scatter(xxdisp(:),yydisp(:),3,'filled', 'blue'), axis ij square tight,
legend('orginal cartesian coordinates', 'coordinates from polar to cartesian');
title('cartesian to polar coordinates'); xlabel('x'); ylabel('y');
%# show images
subplot(245), imshow(img), axis on
title('cartesian'); xlabel('x'); ylabel('y');
subplot(246), warp(theta, rho, zeros(size(theta)), img)
title('cartesian to polar'); xlabel('\theta'); ylabel('\rho');
view(2), axis square
subplot(247), warp(xx, yy, zeros(size(xx)), img)
title('polar to cartesian'); xlabel('x'); ylabel('y');
view(2), axis image
subplot(248)
imagesc(diffImg); colormap(gray); colorbar;
title('difference'); xlabel('x'); ylabel('y');
view(2), axis image
这是我的结果。基本上第一张图片是原版,第二张是cart2pol,第三张是pol2cart。
我的问题是:如何看待第一张和第三张图片之间的区别? (如果你查看我的脚本,第一张图片的数据是img
,但我不知道如何为第三张图片找到这样的数据?第三张图片只是使用warp()
函数来做显示。)
答案 0 :(得分:2)
函数warp(x,y,z,img)
首先通过调用[x,y,z,cdata,cdatamapping,clim,map,likeimage] = parse_inputs(varargin{:});
函数来解析您的输入,其中im2double
应用于img
以获得cdata
。之后,
surface(x,y,z,cdata,'EdgeColor','none','FaceColor','texturemap', ...
'CDataMapping',cdatamapping);
调用来显示图像。
所以你的三个数字都用你给定的坐标显示img
,而你的数字1和3应该是相同的。
您可以type warp
了解该功能的更多详情。