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');
Best Answer