Respected sir, i have code for extracting ROI from dorsal hand image, my problem is to read and save multiple images from folder. in following code only single image has read and save but i want multiple images to read and save please help me.

1 view (last 30 days)
clc; % Clear command window. clear; % Delete all variables. close all; % Close all figure windows except those created by imtool. imtool close all; % Close all figure windows created by imtool. workspace; % Make sure the workspace panel is showing. fontSize = 16; % Read in a standard MATLAB gray scale demo image. folder = fullfile(matlabroot, '\toolbox\images\imdemos'); baseFileName = 'h4.bmp'; % Get the full filename, with path prepended. fullFileName = fullfile(folder, baseFileName); % Check if file exists.
if ~exist(fullFileName, 'file') % File doesn't exist -- didn't find it there. Check the search path for it. fullFileName = baseFileName; % No path this time. if ~exist(fullFileName, 'file') % Still didn't find it. Alert user. errorMessage = sprintf('Error: %s does not exist in the search path folders.', fullFileName); uiwait(warndlg(errorMessage)); return; end end
x=imread('h3.bmp'); grayImage=rgb2gray(x); %grayImage = imread(fullFileName);
axis on; title('Original Grayscale Image', 'FontSize', fontSize); set(gcf, 'Position', get(0,'Screensize')); % Maximize figure.
subplot(2, 3, 1); imshow(grayImage, []);
%e=imrect(gca,[112 120 60 80]);
e = imellipse(gca,[112 120 70.2 80]);
wait(e);
%wait(t);
%resume(e);
addNewPositionCallback(e,@(p) title(mat2str(p,3)));
fcn = makeConstrainToRectFcn('imellipse',get(gca,'XLim'),get(gca,'YLim'));
setPositionConstraintFcn(e,fcn);
% Create a binary image ("mask") from the ROI object.
binaryImage = createMask(e);
figure
subplot(2, 3, 2);
imshow(binaryImage, []);
axis on;
title('Binary mask of the region', 'FontSize', fontSize); % Label the binary image and computer the centroid and center of mass. labeledImage = bwlabel(binaryImage);
measurements = regionprops(binaryImage,'Centroid');
%centroid = measurements.Centroid centroid = measurements.Centroid
% Calculate the area, in pixels, that they drew. numberOfPixels1 = sum(binaryImage(:)) % Another way to calculate it that takes fractional pixels into account. numberOfPixels2 = bwarea(binaryImage)
% Get coordinates of the boundary of the freehand drawn region. structBoundaries = bwboundaries(binaryImage); xy=structBoundaries{1}; % Get n by 2 array of x,y coordinates. x = xy(:, 2); % Columns. y = xy(:, 1); % Rows. subplot(2, 3, 1); % Plot over original image. hold on; % Don't blow away the image. plot(x, y, 'LineWidth', 2); drawnow; % Force it to draw immediately. % Burn line into image by setting it to 255 wherever the mask is true. burnedImage = grayImage; burnedImage(binaryImage) = 255; % Display the image with the mask "burned in." subplot(2, 3, 3); imshow(burnedImage);
axis on; caption = sprintf('New image with\nmask burned into image'); title(caption, 'FontSize', fontSize); % Mask the image and display it. % Will keep only the part of the image that's inside the mask, zero outside mask. blackMaskedImage = grayImage; blackMaskedImage(~binaryImage) = 0; subplot(2, 3, 4); imshow(blackMaskedImage);
axis on; title('Masked Outside Region', 'FontSize', fontSize); % Calculate the mean meanGL = mean(blackMaskedImage(binaryImage)); sdGL = std(double(blackMaskedImage(binaryImage))); % Put up crosses at the centriod and center of mass hold on; plot(centroid(1), centroid(2), 'r+', 'MarkerSize', 30, 'LineWidth', 2); %plot(centerOfMass(1), centerOfMass(2), 'g+', 'MarkerSize', 20, 'LineWidth', 2); % Now do the same but blacken inside the region. insideMasked = grayImage; insideMasked(binaryImage) = 0; subplot(2, 3, 5); imshow(insideMasked);
axis on; title('Masked Inside Region', 'FontSize', fontSize);
% Now crop the image. leftColumn = min(x); rightColumn = max(x); topLine = min(y); bottomLine = max(y); width = rightColumn - leftColumn + 1; height = bottomLine - topLine + 1; croppedImage = imcrop(blackMaskedImage, [leftColumn, topLine, width, height]);
% Display cropped image. subplot(2, 3, 6); imshow(croppedImage); axis on; title('Cropped Image', 'FontSize', fontSize); % Put up crosses at the centriod and center of mass hold on;
plot(centroid(1)-leftColumn, centroid(2)-topLine, 'r+', 'MarkerSize', 30, 'LineWidth', 2);
% save roi file in folder ROI imwrite(croppedImage, 'ROI/one.tif','tif');

Accepted Answer

David Sanchez
David Sanchez on 30 Mar 2016
Hi, firstly, your way of asking the question does not make it easy to help you. Your code presentation is really annoying, but I think you need to do the following:
my_pics = dir('*.bmp'); % get a list of your *bmp images
% go through the images recursively
for kk = 1:numel(my_pics)
x=imread(my_pics(kk).name);
grayImage=rgb2gray(x);
...
...
...
% save roi file in folder ROI
saving_name = sprintf('ROI/%g.tif',kk);
imwrite(croppedImage, saving_name,'tif');
end
  2 Comments
Balaji M. Sontakke
Balaji M. Sontakke on 30 Mar 2016
David Sir, am really thankful to you, your answer really help me for completing my preprocessing tasks. once again thanks.
Image Analyst
Image Analyst on 30 Mar 2016
Before you post again, read this link on code formatting and this on on processing a sequence of files. Thanks for accepting his answer to give him reputation points. You can also vote for his answer to give him even more points.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!