Extract one pixel vertical stripes from multiple images and concatenate them in a new image in Matlab

2 次查看(过去 30 天)
Hello, I need some help with a Matlab code for the following situation: I have 200 images of the same size each. I want to extract from each image a vertical stripe of one pixel size from a particular given position (the same position for each image) and recombine (horizontally concatenate) these 200 one-pixel extracted stripes into a new image.Thank you so much,Augustin Mot

采纳的回答

MarKf
MarKf 2023-1-4
编辑:MarKf 2023-1-4
folder = ''; % pwd or C:\Users\etc or /usr/local/ or fullfile or https// or whatever
position = 10; % column of pixels in pixels
imgs_dim = [400 400]; % images' dimensions in pixels (if img length is half nfiles you'll get stripes until half of the img)
folder_imgs = dir(fullfile(folder, '*.png')); % or just dir(folder) but loop starts from index 3 then, or whatever
imgs_files = {folder_imgs(:).name}';
num_files = numel(imgs_files);
stripes = uint8([imgs_dim(1), num_files ,3]; % preallocate
for cfi = 1:num_files
im=imread(fullfile(folder,imgs_files{cfi}));
stripes(:,cfi,:) = im(:,position,:); %this is the image
end
  3 个评论
Walter Roberson
Walter Roberson 2023-1-5
stripes = uint8(zeros([imgs_dim(1), num_files ,3])) ; % preallocate
There are better ways of writing that; I use this form for the minimum change from the logic posted.

请先登录,再进行评论。

更多回答(1 个)

Walter Roberson
Walter Roberson 2023-1-4
The following code assumes that at least one of the files might be RGB, but does not assume that they all are. It also assumes that images might be indexed (pseudocolor). If you are certain that they are all grayscale then you can simplify the logic a bit.
The code does not, however, handle the possibility of alpha components.
The code as-written assumes that images should be sliced together in whatever order is returned by asking for directory information about the files. If you have files named something like IMG1.jpg IMG2.jpg ... IMG9.jpg IMG10.jpg ... then the directory order is probably not the order you want and you should look in the file exchange for natsortfiles()
column_to_extract = 217; %set as appropriate
imagedir = 'appropriate directory name'; %could be '.'
imgext = '.png'; %or as appropriate, remember the period
dinfo = dir( fullfile(imagedir, "*" + imgext) );
filenames = fullfile( {dinfo.folder}, {dinfo.name});
numfiles = numel(filenames);
for K = 1 : numfiles
thisfile = filenames{K};
[thisimg, map] = imread(thisfile);
if size(thisimg, 2) < column_to_extract
error('file "%s" has too few columns', thisfile);
end
if ~isempty(map); thisimg = ind2rgb(thisimg, map); end %convert indexed images
if size(thisimg,3) == 1; thisimg = thisimg(:,:,[1 1 1]); end %convert gray to RGB
thisslice = thisimg(:,column_to_extract,:);
if K == 1
allslices = zeros(size(thisslice,1), numfiles, 3, 'like', thisslice);
elseif size(thisslice,1) ~= size(allslices,1)
error('file "%s" has different number of rows', thisfile);
end
allslices(:,K,:) = thisslice;
end
  2 个评论
Augustin Mot
Augustin Mot 2023-1-4
Thanks a lot. Still didnt work for me. All images are RGB. I am so sorry, your terms and codes are too fancy for me, I am a beginner in image processing but I will try to work on it.
I got the following error
Error using fullfile (line 51)
String input not supported.
Image Analyst
Image Analyst 2023-1-5
Give ALL the red text, not just a part of it. What you posted does not give the actual line of text that threw the error because you copied and pasted only a snippet of the error message, not the whole thing. What is your line 51? Walter's code does not have 51 lines.
I don't see anything wrong with that fullfile() and it works for me on my computer. Please attach the complete .m file with the paperclip icon.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Matrix Indexing 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by