フィルターのクリア

How to write the pixel values of a graylevel image into a text file?

6 ビュー (過去 30 日間)
VENKATA SUBBAIAH MADAKA
VENKATA SUBBAIAH MADAKA 2022 年 6 月 15 日
コメント済み: Image Analyst 2022 年 7 月 11 日
I would like to read a grayscale image and write that pixel values into a text file
  3 件のコメント
VENKATA SUBBAIAH MADAKA
VENKATA SUBBAIAH MADAKA 2022 年 7 月 11 日
Could you please share the equivalent code for that?
VENKATA SUBBAIAH MADAKA
VENKATA SUBBAIAH MADAKA 2022 年 7 月 11 日
At present, I am using MATLAB R2016.1a version. In it there is no writematrix function. Could you please mention the equivalent function for writematrix in matlab R2016.1a

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

回答 (2 件)

ILoveMATLAB
ILoveMATLAB 2022 年 6 月 15 日
  • Read the image using imread
  • Save the matrix as a text file using writematrix

Image Analyst
Image Analyst 2022 年 6 月 15 日
See attached demo. It writes out the coordinate and RGB values or gray levels to a CSV file.
  3 件のコメント
VENKATA SUBBAIAH MADAKA
VENKATA SUBBAIAH MADAKA 2022 年 7 月 11 日
I need the code for reading a gray scale image and write its pixel values in matrix form to a text file.
Image Analyst
Image Analyst 2022 年 7 月 11 日
The demo does that. Here, I 've made it less general so that it handles only gray scale, not both gray scale and RGB:
% Demo by Image Analyst
% Initialization Steps.
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 20;
% Read in gray scale image.
fullFileName = 'cameraman.tif';
grayScaleImage = imread(fullFileName);
[rows, columns, numberOfColorChannels] = size(grayScaleImage)
if numberOfColorChannels > 1
warningMessage = 'This is not a gray scale image.';
uiwait(errordlg(warningMessage))
end
[x, y] = meshgrid(1:columns, 1:rows);
% Extract the individual gray levels.
% Need to cast to double or else x and y will be clipped to 255 when we concatenate them.
% Get array listing [grayLevel, x, y]. Using (:) will turn all the 2-D arrays into column vectors.
output = [grayScaleImage(:), x(:), y(:)];
% Get the output filename - same as input file name but with .csv extension.
[folder, baseFileNameNoExtension, extension] = fileparts(fullFileName);
baseFileName = [baseFileNameNoExtension, '.csv'];
% folder = pwd; % Change to current folder.
outputFileName = fullfile(folder, baseFileName);
% Write output to CSV file.
message = sprintf('Please wait...\n Writing data to CSV file:\n %s', outputFileName);
fprintf('%s\n', message);
csvwrite(outputFileName, output);
% Let user know we're done.
fprintf('Done!\n Wrote data to CSV file:\n %s\n', outputFileName);
% Open up
promptMessage = sprintf('Done!\n\nWrote data to CSV file:\n%s\n\nDo you want me to it now?', outputFileName);
titleBarCaption = 'Open?';
buttonText = questdlg(promptMessage, titleBarCaption, 'Yes - open it', 'No, do not open it', 'Yes - open it');
if contains(buttonText, 'No,')
return;
end
winopen(outputFileName);

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

カテゴリ

Help Center および File ExchangeLarge Files and Big Data についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by