Making an X ray image out of x, y, z coordinates ERROR
2 views (last 30 days)
Show older comments
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 Comments
Image Analyst
on 25 Jun 2024
If you have any more questions, then attach your data with the paperclip icon after you read this:
Answers (2)
DGM
on 24 Jun 2024
Edited: DGM
on 25 Jun 2024
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.
data = readmatrix(fname); % or just use readmatrix
1 Comment
DGM
on 25 Jun 2024
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
on 25 Jun 2024
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')
fileID = fopen('Chest_Xray_Raw_Data.txt', 'r');
data = textscan(fileID, '%f,%f,%f,%f', 'Delimiter',',') % As Presented In The Question Code
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
fclose(fileID);
x = data{1}
y = data{2}
z = data{3} % If needed, otherwise ignore
grayscale = data{4}
data = readmatrix('Chest_Xray_Raw_Data.txt') % Use 'readmattrix' If You Have It — Just Easier
.
0 Comments
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!