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 件のコメント

Alberto Acri
Alberto Acri 2022 年 10 月 23 日
編集済み: Alberto Acri 2022 年 10 月 23 日
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
Alberto Acri
Alberto Acri 2022 年 10 月 23 日
I would like to open the images obtained at the end of the "pic2points" function into a single three-dimensional image.

サインインしてコメントする。

 採用された回答

Image Analyst
Image Analyst 2022 年 10 月 23 日

0 投票

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 件のコメント

Alberto Acri
Alberto Acri 2022 年 10 月 23 日
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?
Image Analyst
Image Analyst 2022 年 10 月 23 日
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.
Alberto Acri
Alberto Acri 2022 年 10 月 24 日
編集済み: Alberto Acri 2022 年 10 月 24 日
Okay, the code works!
Is it also possible to put the created images in one plot3? As in attached figure.
Image Analyst
Image Analyst 2022 年 10 月 24 日
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.
Alberto Acri
Alberto Acri 2022 年 10 月 25 日
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.
Image Analyst
Image Analyst 2022 年 10 月 25 日
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:
Alberto Acri
Alberto Acri 2022 年 10 月 25 日
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
Image Analyst
Image Analyst 2022 年 10 月 25 日
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);
Alberto Acri
Alberto Acri 2022 年 10 月 26 日
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.
Image Analyst
Image Analyst 2022 年 10 月 26 日
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. 🙂
Alberto Acri
Alberto Acri 2022 年 10 月 26 日
Thank you @Image Analyst! I will make another post to see if it is possible to set a step value to my liking.

サインインしてコメントする。

その他の回答 (0 件)

製品

リリース

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by