saving elements of a dataset as images
3 views (last 30 days)
Show older comments
hello i would like to ask how to save inputData(:,:,idxSelected(i)) as an image file. thanks very much.
function [data,labelsVec,timestamps] = loadCSIDataset(fileName,label,visualizeData)
% loadCSIDataset Loads and visualizes the pre-recorded CSI dataset
% [DATA,LABELSVEC,TIMESTAMPS] =
% loadCSIDataset(FILENAME,LABEL,VISUALIZEDATA) loads the dataset that
% contains the data with the label (LABEL). Pre-recorded CSIs are
% visualized if (VISUALIZEDATA) is true. The function returns the
% pre-recorded beacon frame CSI (DATA), related timestamps (TIMESTAMPS),
% and the categorical labels vector (LABELSVEC).
% Copyright 2022-2024 The MathWorks, Inc.
arguments
fileName {char,string}
label (1,1) string
visualizeData = true;
end
% Load the pre-recorded dataset
datasetDir = which(fileName);
loadedData = load(datasetDir);
data = loadedData.data;
labelsVec = categorical(repmat(label,size(data,ndims(data)),1));
timestamps = loadedData.timestamps;
disp(['Dimensions of the ' char(label) ' dataset (numSubcarriers x numPackets x numCaptures): ' '[' num2str(size(data)) ']'])
% Visualize the dataset
if visualizeData
plotSamplesFromDataset(data,label);
end
% Plot samples from the pre-recorded dataset
function plotSamplesFromDataset(data,mode)
% Plot at most three random samples of the dataset
inputData = abs(data); % Visualize only the magnitude of the CSI
numTotalCaptures = size(inputData,ndims(inputData));
numPlots = min(3,numTotalCaptures);
idxSelected = sort(randperm(numTotalCaptures,numPlots));
figure;
T = tiledlayout(2,numPlots,'TileSpacing','compact');
% Plot 1 - CSI image
for i = 1:numPlots
nexttile
imagesc(inputData(:,:,idxSelected(i)));
colorbar;
xlabel('Packets');
ylabel('Subcarriers');
title(['Raw CSI (#' num2str(idxSelected(i)),')']);
end
% Plot 2 - Normalized CSI periodogram
for j = 1:numPlots
nexttile
imagesc(csi2periodogram(inputData(:,:,idxSelected(j))));
colorbar;
clim([0 1]);
xlabel('Temporal Index');
ylabel('Spatial Index');
title(['CSI Periodogram (#' num2str(idxSelected(j)),')']);
title(T,['Randomly Selected Samples of "', char(mode) '" Data']);
set(gcf,'Position',[0 0 650 450]);
end
end
end
0 Comments
Accepted Answer
Image Analyst
on 17 Jul 2024
You want to save inputData(:,:,idxSelected(i)), which is a variable internal to a Mathworks written function called loadCSIDataset. Not exactly sure what that function is but we recommend never changing a built-in function from a Toolbox written by the Mathworks.
What you should do is to make a copy of that function in the current folder, or a folder of your own earlier in the search path, with a different name, like myLoadCSIDataset.m. Then you will modify your copy, not the original function.
Then in the for loop you can construct a filename with sprintf and fullfile and then save the image slice with imwrite. Here is a snippet with the relevant part that you should modify.
% Define the name of the output folder where you want to save these images.
outputFolder = 'C:\whatever'; % TODO: CHANGE THIS LINE.
if ~isfolder(outputFolder)
mkdir(outputFolder);
end
% Plot 1 - CSI image
for k = 1 : numPlots % Use k instead of i (the imaginary constant) as an iteration variable.
sliceNumber = idxSelected(k);
nexttile
imagesc(inputData(:, :, sliceNumber));
colorbar;
xlabel('Packets');
ylabel('Subcarriers');
caption = sprintf('Raw CSI (Slice #%d))', sliceNumber);
title(caption, 'FontSize', 18);
drawnow; % Force it to update the screen display immediately.
% Now make output file name for a lossless compression PNG format file.
baseFileName = sprintf('Slice %3.3d.png', sliceNumber);
fullFileName = fullfile(outputFolder, baseFileName); % Prepend output folder.
% Now save this image slice to disk.
imwrite(inputData(:, :, sliceNumber), fullFileName) % Save to disk.
fprintf('On iteration %d, saved slice #%d as %s.\n', k, sliceNumber, fullFileName); % Print the progress to the command window.
end
3 Comments
Image Analyst
on 18 Jul 2024
I'm not sure that makes sense because it doesn't matter what the left hand size size is, as long as you're not using indexing on that side. It just takes whatever is on hte right side and makes a new variable out of it. What are the sizes:
size(img)
size(inputData)
size(r_img)
img=im2double(inputData(:,:,idxSelected(i)));
r_img = cRadon(img, 180);
More Answers (2)
Muskan
on 16 Jul 2024
Hi,
As per my understanding, to save the "inputData(:,:,idxSelected(i)") as an image file, you can use MATLAB's "imwrite" function. This function writes an image to a file in various formats such as PNG, JPEG, TIFF, etc.
You can follow the following steps:
- Convert the Data to a Suitable Format: Ensure the data is in a format that "imwrite" can handle. Typically, this means converting the data to "uint8" or "double" and normalizing it if necessary.
- Save the Image: Use "imwrite" to save the image.
You can also refer to the following documentation of "imwrite" for a better understanding: https://www.mathworks.com/help/matlab/ref/imwrite.html
0 Comments
Image Analyst
on 19 Jul 2024
Your cRadon() will do the radon transform for every angle from 1,2,3,4,....,179,180. Is that what you want or do you want just the single angle of 180 degrees for one projection only?
5 Comments
Image Analyst
on 22 Jul 2024
If my Answer solves your original question, then could you please click the "Accept this answer" link to award me with "reputation points" for their efforts in helping you? I'd appreciate it. Thanks in advance. 🙂 Note: you can only accept one answer (so pick the best one) but you can click the "Vote" icon for as many Answers as you want. Voting for an answer will also award reputation points.
See Also
Categories
Find more on Live Scripts and Functions in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!