Making an X ray image out of x, y, z coordinates ERROR

2 ビュー (過去 30 日間)
Hayley Devine
Hayley Devine 2024 年 6 月 24 日
コメント済み: Rena Berman 2024 年 7 月 10 日
I am trying to make an xray image out of given x, y, z coordinates. Here is my code below:
fileID = fopen('Chest_Xray_Raw_Data.txt', 'r');
data = textscan(fileID, '%f,%f,%f,%f', 'Delimiter', ',');
fclose(fileID);
% Extract x, y, z coordinates and grayscale values
x = data{1};
y = data{2};
z = data{3}; % If needed, otherwise ignore
grayscale = data{4};
% Determine the size of the image
maxX = int64(max(x));
maxY = int64(max(y));
% Normalize coordinates to fit into the pixel grid
x = round(x - min(x) + 1);
y = round(y - min(y) + 1);
% Initialize the image matrix
imageMatrix = zeros(maxY, maxX);
% Populate the image matrix with grayscale values
for i = 1:length(x)
imageMatrix(y(i), x(i)) = grayscale(i);
end
% Display the image
figure;
imshow(imageMatrix, []);
title('Reconstructed Image from Raw Data');
colormap(gray); % Use grayscale colormap
colorbar;
An example of the data I am given is this, but obviously larger:
+4.23789300E+002, +0.00000000E+000, +0.00000000E+000, +420
+4.23619400E+002, +0.00000000E+000, +0.00000000E+000, +420
+4.23449400E+002, +0.00000000E+000, +0.00000000E+000, +420
+4.23279500E+002, +0.00000000E+000, +0.00000000E+000, +420
When I run it, I get this message
"Index exceeds the number of array elements. Index must not exceed 0.
Error in untitled (line 24)
imageMatrix(y(i), x(i)) = grayscale(i);"
How can I get rid of this error message? Thanks!
  2 件のコメント
Image Analyst
Image Analyst 2024 年 6 月 25 日
If you have any more questions, then attach your data with the paperclip icon after you read this:
Rena Berman
Rena Berman 2024 年 7 月 10 日

(Answers Dev) Restored edit

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

回答 (2 件)

DGM
DGM 2024 年 6 月 24 日
編集済み: DGM 2024 年 6 月 25 日
The data probably isn't being picked up by textscan() because of the way the delimiter is being specified. If there aren't other odd things going on with the full file, you can try this.
%data = textscan(fileID, '%f%f%f%f', 'Delimiter', ','); % one way
data = textscan(fileID, '%f,%f,%f,%f'); % or another
I don't really ever use textscan() much, so I'm not the one to ask for the best usage.
Alternatively, you can probably just use readmatrix() instead.
data = readmatrix(fname); % or just use readmatrix
  1 件のコメント
DGM
DGM 2024 年 6 月 25 日
Well I don't have your data, and I can't see what's going on, but are you sure your normalization approach makes sense? Your output array is of size [maxY maxX], but you normalize x and y such that their range does not correspond to either maxX or maxY anymore. If x or y is always positive, then they'll always be smaller than the image space, which might result in a squished image. If x or y extend into negative values, they will exceed the image space, causing indexing errors.
I'm not sure what's appropriate for your data and usage.

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


Star Strider
Star Strider 2024 年 6 月 25 日
Ir may be necessary to add the name-value pair:
'EndOfLine','\r\n'
to the textscan call, since otherwise textscan fails to read any except the first element in the matrix. (This is a relatively common problem with textscan, that is otherwise reasonably reliable.) The 'Delimiter' name-value pair is actually not needed here.
There is no reason to automatically assume that the file I created is the same as your original file (not supplied), however It appears to re-create the actual problem you are seeing, with empty cell arrays, that later throw the error.
As DGM menttioned, the readmatrix function is likely your best option, if you have access to it.
A = {'+4.23789300E+002', '+0.00000000E+000', '+0.00000000E+000', '+420'
'+4.23619400E+002', '+0.00000000E+000', '+0.00000000E+000', '+420'
'+4.23449400E+002', '+0.00000000E+000', '+0.00000000E+000', '+420'
'+4.23279500E+002', '+0.00000000E+000', '+0.00000000E+000', '+420'};
writecell(A,'Chest_Xray_Raw_Data.txt')
type('Chest_Xray_Raw_Data.txt')
+4.23789300E+002,+0.00000000E+000,+0.00000000E+000,+420 +4.23619400E+002,+0.00000000E+000,+0.00000000E+000,+420 +4.23449400E+002,+0.00000000E+000,+0.00000000E+000,+420 +4.23279500E+002,+0.00000000E+000,+0.00000000E+000,+420
fileID = fopen('Chest_Xray_Raw_Data.txt', 'r');
data = textscan(fileID, '%f,%f,%f,%f', 'Delimiter',',') % As Presented In The Question Code
data = 1x4 cell array
{[423.7893]} {0x1 double} {0x1 double} {0x1 double}
fclose(fileID);
fileID = fopen('Chest_Xray_Raw_Data.txt', 'r');
data = textscan(fileID, '%f,%f,%f,%f', 'EndOfLine','\r\n') % This Works With The 'EndOfLine' Name-Value Pair
data = 1x4 cell array
{4x1 double} {4x1 double} {4x1 double} {4x1 double}
fclose(fileID);
x = data{1}
x = 4x1
423.7893 423.6194 423.4494 423.2795
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
y = data{2}
y = 4x1
0 0 0 0
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
z = data{3} % If needed, otherwise ignore
z = 4x1
0 0 0 0
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
grayscale = data{4}
grayscale = 4x1
420 420 420 420
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
data = readmatrix('Chest_Xray_Raw_Data.txt') % Use 'readmattrix' If You Have It — Just Easier
data = 4x4
423.7893 0 0 420.0000 423.6194 0 0 420.0000 423.4494 0 0 420.0000 423.2795 0 0 420.0000
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
.

カテゴリ

Help Center および File ExchangeImages についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by