Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
free-form selection

Subject: free-form selection

From: Mohammed Yahia

Date: 16 Dec, 2010 15:58:04

Message: 1 of 6

Suppose that I have an image and I want to select an area (not a rectangular area) on that image. So how can I select a free-form selection in the image.
I tried to use the command (imcrop) but it is specific for a rectangular area.

Subject: free-form selection

From: Sean de

Date: 16 Dec, 2010 17:19:04

Message: 2 of 6

"Mohammed Yahia" <osamabinmohammed@hotmail.com> wrote in message <iedcuc$knt$1@fred.mathworks.com>...
> Suppose that I have an image and I want to select an area (not a rectangular area) on that image. So how can I select a free-form selection in the image.
> I tried to use the command (imcrop) but it is specific for a rectangular area.

GINPUT and POLY2MASK?

Subject: free-form selection

From: Mohammed Yahia

Date: 17 Dec, 2010 15:59:04

Message: 3 of 6

"Sean de " <sean.dewolski@nospamplease.umit.maine.edu> wrote in message <iedhm8$9ph$1@fred.mathworks.com>...
> "Mohammed Yahia" <osamabinmohammed@hotmail.com> wrote in message <iedcuc$knt$1@fred.mathworks.com>...
> > Suppose that I have an image and I want to select an area (not a rectangular area) on that image. So how can I select a free-form selection in the image.
> > I tried to use the command (imcrop) but it is specific for a rectangular area.
>
> GINPUT and POLY2MASK?

Pls, Can you give an example that using these two command to free-selection in the image?

Subject: free-form selection

From: Sean de

Date: 17 Dec, 2010 16:46:05

Message: 4 of 6

"Mohammed Yahia" <osamabinmohammed@hotmail.com> wrote in message
> Pls, Can you give an example that using these two command to free-selection in the image?

%% Demo
%Make sure to reconnect lines broken by the newsreader

I = imread('cameraman.tif'); %Read sample Image

figure;
subplot(1,2,1);
imshow(I);
msgbox(sprintf('Instructions:\n\n1) Select points you want for the vertices of your polygon\n\n2) Hit Enter or Return on the keyboard when done selecting points'));
[x y] = ginput; %polygon points, you have to select them

M = poly2mask(x,y,size(I,1),size(I,2)); %mask

Imasked = uint8(M).*I; %Apply mask
subplot(1,2,2);
imshow(Imasked);

%SCd 12/16/2010
%%%

Subject: free-form selection

From: ImageAnalyst

Date: 17 Dec, 2010 17:39:03

Message: 5 of 6

On Dec 16, 10:58 am, "Mohammed Yahia" <osamabinmoham...@hotmail.com>
wrote:
> Suppose that I have an image and I want to select an area (not a rectangular area) on that image. So how can I select a free-form selection in the image.
> I tried to use the command (imcrop) but it is specific for a rectangular area.

------------------------------------------------------------------------------------
See my demo:

IMPORTANT: The newsreader may break long lines into multiple lines.
Be sure to join any long lines that got split into multiple single
lines.
These can be found by the red lines on the left side of your
text editor, which indicate syntax errors, or else just run the
code and it will stop at the split lines with an error.

% by ImageAnalyst
% Change the current folder to the folder of this m-file.
if(~isdeployed)
cd(fileparts(which(mfilename)));
end
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 = 20;

% Read in standard MATLAB gray scale demo image.
grayImage = imread('cameraman.tif');
subplot(2, 2, 1);
imshow(grayImage, []);
title('Original Grayscale Image', 'FontSize', fontSize);
set(gcf, 'Position', get(0,'Screensize')); % Maximize figure.
message = sprintf('Left click and hold to begin drawing.\nSimply lift
the mouse button to finish');
uiwait(msgbox(message));
hFH = imfreehand();

% Create a binary image ("mask") from the ROI object.
binaryImage = hFH.createMask();
% Display the freehand mask.
subplot(2, 2, 2);
imshow(binaryImage);
title('Binary mask of the region', 'FontSize', fontSize);

% 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, 2, 1); % Plot over original image.
hold on; % Don't blow away the image.
plot(x, y, 'LineWidth', 2);

% 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, 2, 3);
imshow(burnedImage);
title('New image with mask burned into image', 'FontSize', fontSize);

% Mask the image and display it.
% Will keep only the part of the image that's inside the mask, zero
outside mask.
maskedImage = grayImage;
maskedImage(~binaryImage) = 0;
subplot(2, 2, 4);
imshow(maskedImage);
title('Masked Image', 'FontSize', fontSize);

% Calculate the mean
meanGL = mean(maskedImage(binaryImage));

% Report results.
message = sprintf('Mean value within drawn area = %.3f\nNumber of
pixels = %d\nArea in pixels = %.2f', ...
    meanGL, numberOfPixels1, numberOfPixels2);
msgbox(message);

Subject: free-form selection

From: Mohammed Yahia

Date: 18 Dec, 2010 17:11:05

Message: 6 of 6

ImageAnalyst <imageanalyst@mailinator.com> wrote in message <4f1947bd-ff8a-4f5a-a5ba-2de2fb7ff0aa@z9g2000yqz.googlegroups.com>...
> On Dec 16, 10:58 am, "Mohammed Yahia" <osamabinmoham...@hotmail.com>
> wrote:
> > Suppose that I have an image and I want to select an area (not a rectangular area) on that image. So how can I select a free-form selection in the image.
> > I tried to use the command (imcrop) but it is specific for a rectangular area.
>
> ------------------------------------------------------------------------------------
> See my demo:
>
> IMPORTANT: The newsreader may break long lines into multiple lines.
> Be sure to join any long lines that got split into multiple single
> lines.
> These can be found by the red lines on the left side of your
> text editor, which indicate syntax errors, or else just run the
> code and it will stop at the split lines with an error.
>
> % by ImageAnalyst
> % Change the current folder to the folder of this m-file.
> if(~isdeployed)
> cd(fileparts(which(mfilename)));
> end
> 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 = 20;
>
> % Read in standard MATLAB gray scale demo image.
> grayImage = imread('cameraman.tif');
> subplot(2, 2, 1);
> imshow(grayImage, []);
> title('Original Grayscale Image', 'FontSize', fontSize);
> set(gcf, 'Position', get(0,'Screensize')); % Maximize figure.
> message = sprintf('Left click and hold to begin drawing.\nSimply lift
> the mouse button to finish');
> uiwait(msgbox(message));
> hFH = imfreehand();
>
> % Create a binary image ("mask") from the ROI object.
> binaryImage = hFH.createMask();
> % Display the freehand mask.
> subplot(2, 2, 2);
> imshow(binaryImage);
> title('Binary mask of the region', 'FontSize', fontSize);
>
> % 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, 2, 1); % Plot over original image.
> hold on; % Don't blow away the image.
> plot(x, y, 'LineWidth', 2);
>
> % 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, 2, 3);
> imshow(burnedImage);
> title('New image with mask burned into image', 'FontSize', fontSize);
>
> % Mask the image and display it.
> % Will keep only the part of the image that's inside the mask, zero
> outside mask.
> maskedImage = grayImage;
> maskedImage(~binaryImage) = 0;
> subplot(2, 2, 4);
> imshow(maskedImage);
> title('Masked Image', 'FontSize', fontSize);
>
> % Calculate the mean
> meanGL = mean(maskedImage(binaryImage));
>
> % Report results.
> message = sprintf('Mean value within drawn area = %.3f\nNumber of
> pixels = %d\nArea in pixels = %.2f', ...
> meanGL, numberOfPixels1, numberOfPixels2);
> msgbox(message);


Thanks alot for all of you guys

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us