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
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
I would like to open the images obtained at the end of the "pic2points" function into a single three-dimensional image.
채택된 답변
Image Analyst
2022년 10월 23일
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.

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.

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)
