Convert image pixels to XYZ-coordinates (3D plot)
显示 更早的评论
Hello everyone,
I want to extend the following code:
Im = imread('./Images/Plot.png');
figure(1);
imshow(Im);
CoordinateMatrix = pic2points(Im);
scatter(CoordinateMatrix (:,1), CoordinateMatrix (:,2),'.');
figure(2);
so that:
- be able to display multiple images in the same graph (the images are in the "Images" folder that has been created; an example of an image is the one attached)
- display the graph in 3D (and not in 2D as in this case)
2 个评论
I changed the code in the following way but it only allows me to see a transformed figure with "pic2point".
myFolder = 'C:\Users\Alberto\Downloads\pic2points\Images';
if ~isdir(myFolder)
errorMessage = sprintf('Error: The following folder does not exist:\n%s', myFolder);
uiwait(warndlg(errorMessage));
return;
end
filePattern = fullfile(myFolder, '*.png');
jpegFiles = dir(filePattern);
for k = 1:length(jpegFiles)
baseFileName = jpegFiles(k).name;
fullFileName = fullfile(myFolder, baseFileName);
fprintf(1, 'Now reading %s\n', fullFileName);
imageArray = imread(fullFileName);
% figure();
imshow(imageArray); % Display image.
drawnow; % Force display to update immediately.
figure(1);
CoordinateMatrix = pic2points(imageArray);
scatter(CoordinateMatrix (:,1), CoordinateMatrix (:,2),'.');
figure(2);
end
I would like to open the images obtained at the end of the "pic2points" function into a single three-dimensional image.
采纳的回答
Try this:
myFolder = 'C:\Users\Alberto\Downloads\pic2points\Images';
if ~isfolder(myFolder)
errorMessage = sprintf('Error: The following folder does not exist:\n%s', myFolder);
uiwait(warndlg(errorMessage));
return;
end
filePattern = fullfile(myFolder, '*.png');
imageFiles = dir(filePattern);
for k = 1:length(imageFiles)
baseFileName = imageFiles(k).name;
fullFileName = fullfile(myFolder, baseFileName);
fprintf('Now reading %s\n', fullFileName);
imageArray = imread(fullFileName);
CoordinateMatrix = pic2points(imageArray);
scatter(CoordinateMatrix(:, 1), CoordinateMatrix(:, 2), '.');
hold on;
end
fprintf('Done!\n');
11 个评论
OK, thanks for the help @Image Analyst. With your code I can correctly display the images separately.
Is it possible to display all the images I got in the same figure? Possibly with three dimensional visualization?
I'm not exactly sure what z is. Maybe k? If so use plot3
Instead of
scatter(CoordinateMatrix(:, 1), CoordinateMatrix(:, 2), '.');
try
z = k * ones(size(CoordinateMatrix, 1), 1);
plot3(CoordinateMatrix(:, 1), CoordinateMatrix(:, 2), z, '.');
Or see the attached demo.

Okay, the code works!
Is it also possible to put the created images in one plot3? As in attached figure.

Everything should already be in the same axes. Nothing in my code creates a new axes or figure window.
Plus I also use "hold on" so it will just overwrite (add on) the additional plot curves to the existing axes.
I tried running the code again. I display the figures (.png) in separate images (plot3). I would like to "merge" them all into a single image as in the figure shown in the demo.
Attach your script and 3 of your input images.
If you have any more questions, then attach your data and code to read it in with the paperclip icon after you read this:
Here is the code and 4 figures attached:
myFolder = 'C:\Users\Alberto\Downloads\pic2points\Images';
if ~isfolder(myFolder)
errorMessage = sprintf('Error: The following folder does not exist:\n%s', myFolder);
uiwait(warndlg(errorMessage));
return;
end
filePattern = fullfile(myFolder, '*.png');
imageFiles = dir(filePattern);
for k = 1:length(imageFiles)
baseFileName = imageFiles(k).name;
fullFileName = fullfile(myFolder, baseFileName);
fprintf('Now reading %s\n', fullFileName);
imageArray = imread(fullFileName);
CoordinateMatrix = pic2points(imageArray);
% scatter(CoordinateMatrix(:, 1), CoordinateMatrix(:, 2), '.');
z = k * ones(size(CoordinateMatrix, 1), 1);
plot3(CoordinateMatrix(:, 1), CoordinateMatrix(:, 2), z, '.');
hold on
end
Try this
clc; % Clear the command window.
fprintf('Beginning to run %s.m ...\n', mfilename);
close all; % Close all figures (except those of imtool.)
clearvars;
workspace; % Make sure the workspace panel is showing.
format short g;
format compact;
myFolder = pwd; %'C:\Users\Alberto\Downloads\pic2points\Images';
if ~isfolder(myFolder)
errorMessage = sprintf('Error: The following folder does not exist:\n%s', myFolder);
uiwait(warndlg(errorMessage));
return;
end
filePattern = fullfile(myFolder, '*.png');
imageFiles = dir(filePattern);
hFig3 = figure('Name', '3D Plot', 'NumberTitle', 'off');
for k = 1:length(imageFiles)
baseFileName = imageFiles(k).name;
fullFileName = fullfile(myFolder, baseFileName);
fprintf('Now reading %s\n', fullFileName);
imageArray = imread(fullFileName);
drawnow;
% Call pic2points. This will open a new figure.
CoordinateMatrix = pic2points(imageArray);
% Switch to the figure for the 3-D plotting:
figure(hFig3);
% scatter(CoordinateMatrix(:, 1), CoordinateMatrix(:, 2), '.');
z = k * ones(size(CoordinateMatrix, 1), 1);
plot3(CoordinateMatrix(:, 1), CoordinateMatrix(:, 2), z, '.');
hold on
drawnow;
end
grid on;
hFig3.WindowState = 'maximized';
fprintf('Done running %s.m.\n', mfilename);

OK, the code works.
Last clarification. Is it possible to apply the same color for all figures? And is it possible to change the position of each figure on the Z axis? In the graph: the first figure is placed on the plane with Z=1, the second figure on the plane with Z=2, etc. That is, a step=1 is imposed. I would like to define at which step to place the images.
Yes, just specify the color in plot3(), for example if you want blue:
plot3(CoordinateMatrix(:, 1), CoordinateMatrix(:, 2), z, 'b.');
Since it answered your question, could you please click the "Accept this answer" link?
Thanks in advance. 🙂
Thank you @Image Analyst! I will make another post to see if it is possible to set a step value to my liking.
更多回答(0 个)
类别
在 帮助中心 和 File Exchange 中查找有关 Image Arithmetic 的更多信息
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!选择网站
选择网站以获取翻译的可用内容,以及查看当地活动和优惠。根据您的位置,我们建议您选择:。
您也可以从以下列表中选择网站:
如何获得最佳网站性能
选择中国网站(中文或英文)以获得最佳网站性能。其他 MathWorks 国家/地区网站并未针对您所在位置的访问进行优化。
美洲
- América Latina (Español)
- Canada (English)
- United States (English)
欧洲
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
