number of channels in spectral image

6 次查看(过去 30 天)
Hello everyone I want to create a function that counts the number of channels in a spectral image, anyone have an idea?

回答(1 个)

Moh Moumouh
Moh Moumouh 2021-4-29
function img = pfs_read_image( fileName )
%PFS_READ_IMAGE Read image file and return RGB, luminance or multichannel matrix.
%
% IMG = PFS_READ_IMAGE( file_name )
%
% If input is a gray-scale or luminance image, IMG is a 2D matrix. If input is
% a color image, IMG(:,:,k) represents red, blue and green color channels for k=1,2 and 3.
% If input is a multi-channel image (channel names C1, C2, ..., Cn), IMG is a
% 3D matrix with 3rd dimension corresponding to the channels.
%
% PFS_READ_IMAGE accepts all formats recognized by the shell "pfsin"
% command.
%
% Example:
% img = PFS_READ_IMAGE( 'hdr_images/memorial.exr' );
%
% See also: PFS_READ_RGB, PFS_READ_LUMINANCE, PFS_READ_XYZ,
% PFS_WRITE_IMAGE, PFSVIEW.
%
% Copyright 2009 Rafal Mantiuk
%Check if file exists
fid = fopen( fileName, 'rb' );
if( fid == -1 )
error( 'pfs_read_image: File "%s" does not exist', fileName );
end
fclose( fid );
fid = pfspopen( sprintf( '%spfsin ''%s''%s', pfs_shell(), fileName, pfs_shell( 1 ) ), 'r' );
pin = pfsopen( fid );
pin = pfsget( pin );
if( isfield( pin.channels, 'X' ) && isfield( pin.channels, 'Z' ) )
img = pfs_transform_colorspace( 'XYZ', pin.channels.X, pin.channels.Y, pin.channels.Z, 'RGB' );
elseif( isfield( pin.channels, 'Y' ) )
img = pin.channels.Y;
elseif( isfield( pin.channels, 'C1' ) )
ch=1;
% count the number of channels
while( isfield( pin.channels, sprintf( 'C%d', ch ) ) )
ch = ch+1;
end
ch_max = ch-1;
img = zeros(pin.rows, pin.columns, ch_max);
for ch=1:ch_max
img(:,:,ch) = pin.channels.(sprintf( 'C%d', ch ));
end
else
error( 'Color channels missing in the pfs frame' );
end
pfsclose( pin );
% TODO: Check why crashes on windows
if ~ispc()
pfspclose( fid );
end
end
I found this program on the internet but I don't really understand it but I just want to use this part
% count the number of channels
while( isfield( pin.channels, sprintf( 'C%d', ch ) ) )
ch = ch+1;
end
ch_max = ch-1;
img = zeros(pin.rows, pin.columns, ch_max);
for ch=1:ch_max
img(:,:,ch) = pin.channels.(sprintf( 'C%d', ch ));
end
but I don't really know how to do it.

类别

Help CenterFile Exchange 中查找有关 Image Processing Toolbox 的更多信息

产品


版本

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by