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

1 view (last 30 days)
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

Accepted Answer

MarKf
MarKf on 4 Jan 2023
Edited: MarKf on 4 Jan 2023
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 Comments
Walter Roberson
Walter Roberson on 5 Jan 2023
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.

Sign in to comment.

More Answers (1)

Walter Roberson
Walter Roberson on 4 Jan 2023
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 Comments
Augustin Mot
Augustin Mot on 4 Jan 2023
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 on 5 Jan 2023
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.

Sign in to comment.

Categories

Find more on Data Type Conversion 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!