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:
image processing crop window

Subject: image processing crop window

From: pier

Date: 13 Jan, 2011 15:23:06

Message: 1 of 9

Hi all,

If I want to crop a little window manually(so this little window each time changes as I want because i need to select something in the image that for each image canges) on my image and I want that this window has always the same size how I can do?For example I have an image 512-512 and I want to select manually a little window 6-6 using imcrop.How I can do?I think are 2 lines of code but I m sorry i don t really know

Subject: image processing crop window

From: Sean de

Date: 13 Jan, 2011 15:48:04

Message: 2 of 9

"pier " <pieralessandrogiorgetti@yahoo.it> wrote in message <ign5cq$rnt$1@fred.mathworks.com>...
> Hi all,
>
> If I want to crop a little window manually(so this little window each time changes as I want because i need to select something in the image that for each image canges) on my image and I want that this window has always the same size how I can do?For example I have an image 512-512 and I want to select manually a little window 6-6 using imcrop.How I can do?I think are 2 lines of code but I m sorry i don t really know

Look at this demo from another thread:
You'll have to change it forces you to select two points (corners of rectangle) and then rounds as necessary to make the window the correct size.

%% 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: image processing crop window

From: ImageAnalyst

Date: 13 Jan, 2011 15:59:16

Message: 3 of 9

On Jan 13, 10:23 am, "pier " <pieralessandrogiorge...@yahoo.it> wrote:
> Hi all,
>
> If I want to crop a little window manually(so this little window each time changes as I want because i need to select something in the image that for each image canges) on my image and I want that this window has always the same size how I can do?For example I have an image 512-512 and I want to select manually a little window 6-6 using imcrop.How I can do?I think are 2 lines of code but I m sorry i don t really know

---------------------------------------------------------------
It is. One call to imcrop(), and one call to display the cropped
image. Here's a complete demo:

% by ImageAnalyst
% 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 the command window.
close all; % Close all figures (except those of imtool.)
imtool close all; % Close all imtool figures.
clear; % Erase all existing variables.
workspace; % Make sure the workspace panel is showing.
fontSize = 20;

% Change the current folder to the folder of this m-file.
if(~isdeployed)
cd(fileparts(which(mfilename)));
end

% Read in a standard MATLAB gray scale demo image.
grayImage = imread('cameraman.tif');
% Get the dimensions of the image. numberOfColorBands should be = 1.
[rows columns numberOfColorBands] = size(grayImage);
% Display the original gray scale image.
handleToAxes = subplot(1, 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')

message = sprintf('Draw a small box.\nDouble click in the middle when
you have it how you like it.');
uiwait(msgbox(message));

croppedImage = imcrop(handleToAxes);
subplot(1, 2, 2);
imshow(croppedImage);

Subject: image processing crop window

From: pier

Date: 13 Jan, 2011 16:14:04

Message: 4 of 9

Thanks Sean but i need to select manually a rettangle in the picture that must be a multiple of the original image because then I use blkproc to divide the image in blocks with the same size of the window that I crop and then apply the euclidian distance between each block and the cropped window.Could you help me please?this is my code:


[file,path]= uigetfile('*.*','Load');
filename= sprintf ('%s%s',path,file);
img1=imread(filename);
img=rgb2gray(img1);
img=imresize(img, [512 512]);
figure('name','Original image'),imshow(img);

%Crop the window

[text1,rect] = imcrop(img);
figure('name','Pigmented area'), imshow(text1);

fun=...........
   Ip = blkproc(img,[? ?],fun));

Subject: image processing crop window

From: ImageAnalyst

Date: 13 Jan, 2011 17:48:29

Message: 5 of 9

pier:
Looks like you found imcrop, so that part should be solved.
I don't know what "euclidian distance between each block and the
cropped window" means. There is a function bwdist() that computes the
Euclidean Distance Transform (among others) but it's definition
doesn't seem to match your context.
-ImageAnalyst

Subject: image processing crop window

From: pier

Date: 13 Jan, 2011 18:06:05

Message: 6 of 9

I know how to crop but what I need is to crop a window that is multiple of the image.The image is 512-512 so I want to crop 8-8 window manually because if the image change I need to change the position of the window.It's important because then I ve to apply some operations between this crop window and the block of the image that are also
 multiple (8-8) ,Could you help me?

Subject: image processing crop window

From: ImageAnalyst

Date: 13 Jan, 2011 18:09:39

Message: 7 of 9

On Jan 13, 1:06 pm, "pier " <pieralessandrogiorge...@yahoo.it> wrote:
> I know how to crop but what I need is to crop a window that is multiple of the image.The image is 512-512 so I want to crop 8-8 window manually because if the image change I need to change the position of the window.It's important because then I ve to apply some operations between this crop window and the block of the image that are also
>  multiple (8-8) ,Could you help me?

---------------------------------------
But that's what blockproc does - it moves the window over in "jumps"
that you specify and allows your function to process just the part of
the image in that window.

Here's a demo:

function blockproc_demo()
try
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
imtool close all; % Close all imtool figures.
clear; % Erase all existing variables.
workspace; % Make sure the workspace panel is showing.
fontSize = 20;

% Change the current folder to the folder of this m-file.
if(~isdeployed)
cd(fileparts(which(mfilename)));
end

% Read in standard MATLAB demo image.
grayImage = imread('cameraman.tif');
[rows columns numberOfColorChannels] = size(grayImage);
% Display the original image.
subplot(2, 2, 1);
imshow(grayImage, []);
caption = sprintf('Original Image\n%d by %d pixels', ...
rows, columns);
title(caption, 'FontSize', fontSize);
% Enlarge figure to full screen.
set(gcf, 'Position', get(0,'Screensize'));
set(gcf, 'name','Demo by ImageAnalyst', 'numbertitle','off')

% Block process the image.
windowSize = 3;
% Each 3x3 block will get replaced by one value.
% Output image will be smaller by a factor of windowSize.
myFilterHandle = @myFilter;
blockyImage = blockproc(grayImage,[windowSize windowSize],
myFilterHandle);
[rowsP columnsP numberOfColorChannelsP] = size(blockyImage);

% Display the processed image.
% It is smaller, but the display routine imshow() replicates
% the image so that it looks bigger than it really is.
subplot(2, 2, 2);
imshow(blockyImage, []);
caption = sprintf('Image Processed in %d by %d Blocks\n%d by %d
pixels\nBox Filter', ...
windowSize, windowSize, rowsP, columnsP);
title(caption, 'FontSize', fontSize);

% Now let's do it an alternate way where we use an anonymous
function.
% We'll take the standard deviation in the blocks.
windowSize = 8;
myFilterHandle2 = @(block_struct) ...
std2(block_struct.data) * ones(size(block_struct.data));
blockyImageSD = blockproc(grayImage, [windowSize windowSize],
myFilterHandle2);
[rowsSD columnsSD numberOfColorChannelsSD] = size(blockyImageSD);
subplot(2, 2, 4);
imshow(blockyImageSD, []);
caption = sprintf('Image Processed in %d by %d Blocks\n%d by %d
pixels\nStandard Deviation Filter', ...
windowSize, windowSize, rowsSD, columnsSD);
title(caption, 'FontSize', fontSize);

% Note: the image size of blockyImageSD is 256x256, NOT smaller.
% Does blockproc behave differently if an anonymous function is
used?

uiwait(msgbox('Done with demo'));
catch ME
errorMessage = sprintf('Error in blockproc_demo():\n\nError Message:
\n%s', ME.message);
uiwait(warndlg(errorMessage));
end
return;

% Takes one 3x3 block of image data and multiplies it
% element by element by the kernel and
% returns a single value.
function singleValue = myFilter(blockStruct)
try
% Assign default value.
% Will be used near sides of image (due to boundary effects),
% or in the case of errors, etc.
singleValue = 0;

% Create a 2D filter.
kernel = [0 0.2 0; 0.2 0.2 0.2; 0 0.2 0];
% kernel = ones(blockStruct.blockSize); % Box filter.

% Make sure filter size matches image block size.
if any(blockStruct.blockSize ~= size(kernel))
% If any of the dimensions don't match.
% You'll get here near the edges,
% if the image is not a multiple of the block size.
% warndlg('block size does not match kernel size');
return;
end
% Size matches if we get here, so we're okay.

% Extract our block out of the structure.
array3x3 = blockStruct.data;

% Do the filtering. Multiply by kernel and sum.
singleValue = sum(sum(double(array3x3) .* kernel));

catch ME
% Some kind of problem...
errorMessage = sprintf('Error in myFilter():\n\nError Message:\n%s',
ME.message);
% uiwait(warndlg(errorMessage));
fprintf(1, '%s\n', errorMessage);
end
return;

Subject: image processing crop window

From: Brendan Hannigan

Date: 14 Jan, 2011 14:07:04

Message: 8 of 9

> Here's a demo:
>
> function blockproc_demo()
> ...
> % Note: the image size of blockyImageSD is 256x256, NOT smaller.
> % Does blockproc behave differently if an anonymous function is
> used?
> ...


Hi ImageAnalyst,

In your BLOCKPROC demo you pose this question wondering why the result of BLOCKPROC is a different size after the 2nd call to the function (when called with an anonymous function handle).

The reason is that your 2 user functions that you pass to BLOCKPROC are fundamentally different functions. In your first call (to MYFILTER) you are returning a scalar, here:

> singleValue = sum(sum(double(array3x3) .* kernel));

In your second call, you return a block of the same size as the input block where all pixels are the std.

> myFilterHandle2 = @(block_struct) ...
> std2(block_struct.data) * ones(size(block_struct.data));

The anonymous function has no impact on the result. BLOCKPROC behaves the same regardless of the type of function handle passed in.

cheers!
-brendan

Subject: image processing crop window

From: ImageAnalyst

Date: 14 Jan, 2011 14:13:56

Message: 9 of 9

On Jan 14, 9:07 am, "Brendan Hannigan"
<bhannigaHE...@THEREmathworks.com> wrote:
> > Here's a demo:
>
> > function blockproc_demo()
> >  ...
> >            % Note: the image size of blockyImageSD is 256x256, NOT smaller.
> >            % Does blockproc behave differently if an anonymous function is
> > used?
> >  ...
>
> Hi ImageAnalyst,
>
> In your BLOCKPROC demo you pose this question wondering why the result of BLOCKPROC is a different size after the 2nd call to the function (when called with an anonymous function handle).  
>
> The reason is that your 2 user functions that you pass to BLOCKPROC are fundamentally different functions.  In your first call (to MYFILTER) you are returning a scalar, here:
>
> > singleValue = sum(sum(double(array3x3) .* kernel));
>
> In your second call, you return a block of the same size as the input block where all pixels are the std.
>
> > myFilterHandle2 = @(block_struct) ...
> >     std2(block_struct.data) * ones(size(block_struct.data));
>
> The anonymous function has no impact on the result.  BLOCKPROC behaves the same regardless of the type of function handle passed in.
>
> cheers!
> -brendan
-----------------------------------------------------------------------
brendan:
Thanks for the explanation. I'll modify the comments in my demo.
It's good that it has this flexibility.
ImageAnalyst

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