Got Questions? Get Answers.
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:
magic wand tool in matlab?

Subject: magic wand tool in matlab?

From: lav

Date: 19 Sep, 2011 00:25:28

Message: 1 of 5

hi,


similar to the magic wand tool in photoshop, is there any tool/script which does the same in matlab?

magic wand tool selects the pixels of the same color and it gives a threshold level too.

thx..

Subject: magic wand tool in matlab?

From: ImageAnalyst

Date: 19 Sep, 2011 02:04:48

Message: 2 of 5

On Sep 18, 8:25 pm, "lav " <vijay.easw...@gmail.com> wrote:
> hi,
>
> similar to the magic wand tool in photoshop, is there any tool/script which does the same in matlab?
>
> magic wand tool selects the pixels of the same color and it gives a threshold level too.
>
> thx..

-----------------------------------------------------------------
Not that I'm aware of. Not exactly. Though you could probably do the
same with ginput(), a couple of binarizations to get the gray levels
within the tolerance range, and imreconstruct() to get the region
attached to where you clicked. For example, I whipped this up in
about 5 minutes:

% Demo to do similar to what the Magic Wand does in Photoshop.
% Only tested on gray scale images.

% 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.

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 = 15;
% Read in a standard MATLAB gray scale demo image.
folder = fullfile(matlabroot, '\toolbox\images\imdemos');
baseFileName = 'cameraman.tif';
fullFileName = fullfile(folder, baseFileName);
% Get the full filename, with path prepended.
fullFileName = fullfile(folder, baseFileName);
if ~exist(fullFileName, 'file')
% 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.', fullFileName);
uiwait(warndlg(errorMessage));
return;
end
end
grayImage = imread(fullFileName);
% Get the dimensions of the image. numberOfColorBands should be = 1.
[rows columns numberOfColorBands] = size(grayImage);
% Display the original gray scale image.
subplot(2, 2, 1);
imshow(grayImage, []);
title('Original Grayscale Image', 'FontSize', fontSize);
% Enlarge figure to full screen.
set(gcf, 'Position', get(0,'Screensize'));
set(gcf,'name','Demo by ImageAnalyst','numbertitle','off')

uiwait(msgbox('Click a point'));
[x, y] = ginput(1);
row = max([int32(y) 1]);
column = max([int32(x) 1]);
% Get the gray level of the point they clicked on.
grayLevel = grayImage(row, column);
tolerance = 10;

% Construct abinary image within the gray level tolerance
% of where they clicked.
lowGL = grayLevel - tolerance;
highGL = grayLevel + tolerance;
binaryImage = grayImage >= lowGL & grayImage <= highGL;
subplot(2, 2, 2);
imshow(binaryImage, []);
caption = sprintf('Binary Image within \n+/- %d Gray Levels of
%d', ...
tolerance, grayLevel);
title(caption, 'FontSize', fontSize);

% Get a marker image to reconstruct just the connected region
% and not all the other disconnected regions.
binaryMarkerImage = logical(zeros(rows, columns));
binaryMarkerImage(row, column) = true;

outputImage = imreconstruct(binaryMarkerImage, binaryImage);
subplot(2, 2, 3);
imshow(outputImage, []);
title('Magic Wand Reconstructed Binary Image', 'FontSize', fontSize);

% Get the masked image - the original image in the region.
maskedImage = zeros(rows, columns, 'uint8');
maskedImage(outputImage) = grayImage(outputImage);
subplot(2, 2, 4);
imshow(maskedImage, [0 255]);
title('Magic Wand Grayscale Image', 'FontSize', fontSize);

msgbox('Done with demo');

Subject: magic wand tool in matlab?

From: lav

Date: 19 Sep, 2011 21:17:29

Message: 3 of 5

ImageAnalyst <imageanalyst@mailinator.com> wrote in message <dcebd0b9-9b6f-482f-9d28-4b168e98b8ec@et6g2000vbb.googlegroups.com>...
> On Sep 18, 8:25 pm, "lav " <vijay.easw...@gmail.com> wrote:
> > hi,
> >
> > similar to the magic wand tool in photoshop, is there any tool/script which does the same in matlab?
> >
> > magic wand tool selects the pixels of the same color and it gives a threshold level too.
> >
> > thx..
>
> -----------------------------------------------------------------
> Not that I'm aware of. Not exactly. Though you could probably do the
> same with ginput(), a couple of binarizations to get the gray levels
> within the tolerance range, and imreconstruct() to get the region
> attached to where you clicked. For example, I whipped this up in
> about 5 minutes:
>
> % Demo to do similar to what the Magic Wand does in Photoshop.
> % Only tested on gray scale images.
>
> % 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.
>
> 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 = 15;
> % Read in a standard MATLAB gray scale demo image.
> folder = fullfile(matlabroot, '\toolbox\images\imdemos');
> baseFileName = 'cameraman.tif';
> fullFileName = fullfile(folder, baseFileName);
> % Get the full filename, with path prepended.
> fullFileName = fullfile(folder, baseFileName);
> if ~exist(fullFileName, 'file')
> % 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.', fullFileName);
> uiwait(warndlg(errorMessage));
> return;
> end
> end
> grayImage = imread(fullFileName);
> % Get the dimensions of the image. numberOfColorBands should be = 1.
> [rows columns numberOfColorBands] = size(grayImage);
> % Display the original gray scale image.
> subplot(2, 2, 1);
> imshow(grayImage, []);
> title('Original Grayscale Image', 'FontSize', fontSize);
> % Enlarge figure to full screen.
> set(gcf, 'Position', get(0,'Screensize'));
> set(gcf,'name','Demo by ImageAnalyst','numbertitle','off')
>
> uiwait(msgbox('Click a point'));
> [x, y] = ginput(1);
> row = max([int32(y) 1]);
> column = max([int32(x) 1]);
> % Get the gray level of the point they clicked on.
> grayLevel = grayImage(row, column);
> tolerance = 10;
>
> % Construct abinary image within the gray level tolerance
> % of where they clicked.
> lowGL = grayLevel - tolerance;
> highGL = grayLevel + tolerance;
> binaryImage = grayImage >= lowGL & grayImage <= highGL;
> subplot(2, 2, 2);
> imshow(binaryImage, []);
> caption = sprintf('Binary Image within \n+/- %d Gray Levels of
> %d', ...
> tolerance, grayLevel);
> title(caption, 'FontSize', fontSize);
>
> % Get a marker image to reconstruct just the connected region
> % and not all the other disconnected regions.
> binaryMarkerImage = logical(zeros(rows, columns));
> binaryMarkerImage(row, column) = true;
>
> outputImage = imreconstruct(binaryMarkerImage, binaryImage);
> subplot(2, 2, 3);
> imshow(outputImage, []);
> title('Magic Wand Reconstructed Binary Image', 'FontSize', fontSize);
>
> % Get the masked image - the original image in the region.
> maskedImage = zeros(rows, columns, 'uint8');
> maskedImage(outputImage) = grayImage(outputImage);
> subplot(2, 2, 4);
> imshow(maskedImage, [0 255]);
> title('Magic Wand Grayscale Image', 'FontSize', fontSize);
>
> msgbox('Done with demo');
>

Thank you Image Analyst for that, but I am not able to get the kind of segmentation for the picture that I had with me. I will work on other codes and will update if i find something better.

-lav

Subject: magic wand tool in matlab?

From: Florin Neacsu

Date: 20 Sep, 2011 00:47:30

Message: 4 of 5

"lav " <vijay.easwari@gmail.com> wrote in message <j5625o$uf$1@newscl01ah.mathworks.com>...
> hi,
>
>
> similar to the magic wand tool in photoshop, is there any tool/script which does the same in matlab?
>
> magic wand tool selects the pixels of the same color and it gives a threshold level too.
>
> thx..

Hi,

Have a look at http://staff.science.uva.nl/~rein/mp/files/book-7-grouping.pdf

Regards,
Florin

Subject: magic wand tool in matlab?

From: lav

Date: 20 Sep, 2011 20:01:30

Message: 5 of 5

"Florin Neacsu" wrote in message <j58nr1$gdd$1@newscl01ah.mathworks.com>...
> "lav " <vijay.easwari@gmail.com> wrote in message <j5625o$uf$1@newscl01ah.mathworks.com>...
> > hi,
> >
> >
> > similar to the magic wand tool in photoshop, is there any tool/script which does the same in matlab?
> >
> > magic wand tool selects the pixels of the same color and it gives a threshold level too.
> >
> > thx..
>
> Hi,
>
> Have a look at http://staff.science.uva.nl/~rein/mp/files/book-7-grouping.pdf
>
> Regards,
> Florin

That's the one I was expecting for.. Thank you so much!! :)

Tags for 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