Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

creating a mask matrix using matlab

1,568 views
Skip to first unread message

ashish

unread,
Mar 24, 2009, 2:57:01 PM3/24/09
to
Hi,
First how do i create a mask matrix with zeroes at four X*X corners and ones at the remaining corners?
Next,How do I multiply the mask matrix at the four corners with the input matrix so that it becomes zero at those specified X*X four corners and ones at the remaining corners?

Sounds simple but I am finding it little difficult to comprehend.
Thanks.

Image Analyst

unread,
Mar 24, 2009, 3:09:02 PM3/24/09
to
"ashish " <ashi...@hotmail.com>

Try this:


clc;
close all;
% Create some arbitrary array for demo.
imageArray = magic(5)
[rows cols] = size(imageArray);
% Create a mask array the same size that is all 1's.
maskArray = ones(rows, cols);
% Zero out the corners.
maskArray(1, 1) = 0;
maskArray(1, cols) = 0;
maskArray(rows, 1) = 0;
maskArray(rows, cols) = 0
% Dot-multiply to mask the image.
maskedImageArray = imageArray .* maskArray

Walter Roberson

unread,
Mar 24, 2009, 3:37:10 PM3/24/09
to


I'd be tempted to use sub2ind() to shorten that code.

maskArray = ones(rows, cols);
maskArray(sub2ind([rows, cols], [1 1 rows rows], [1 cols 1 cols])) = 0;

ashish

unread,
Mar 24, 2009, 3:39:01 PM3/24/09
to
Hi Image Analyst
I understand your example but How do i get four corners i.e submatrices as zeroes rather than just four corner elements as zero?
Thanks a lot.

"Image Analyst" <imagea...@mailinator.com> wrote in message <gqbb4e$8an$1...@fred.mathworks.com>...

Image Analyst

unread,
Mar 24, 2009, 3:52:02 PM3/24/09
to
"ashish " <ashi...@hotmail.com>
Just use normal, run-of-the-mill indexing:

clc;
close all;
% Create some arbitrary array for demo.

imageArray = magic(8)


[rows cols] = size(imageArray);
% Create a mask array the same size that is all 1's.
maskArray = ones(rows, cols);

% Zero out the corners in a 2x2 submatrix at each corner.
submatrixSize = 2;
maskArray(1:submatrixSize, 1:submatrixSize) = 0;
maskArray(1:submatrixSize, (cols - submatrixSize + 1):cols) = 0;
maskArray((rows - submatrixSize + 1):rows, 1:submatrixSize) = 0;
maskArray((rows - submatrixSize + 1):rows, (cols - submatrixSize + 1):cols) = 0

Image Analyst

unread,
Mar 24, 2009, 4:09:01 PM3/24/09
to
Of course, just as I zeroed out the corners of the maskArray, you could do the same thing to the image array directly and skip the multiplication step. Why multiply a bunch of numbers in the middle when you don't need to (unless you need to save the mask or original image to use later for some reason)?

ashish

unread,
Mar 24, 2009, 4:23:01 PM3/24/09
to
Thanks a lot Image Analyst. I was trying to do the same with the first submatrix but I was stuck with the other three submatrices and you replied by the time i was think about other ones.

Thanks once again, I learned something new today.

"Image Analyst" <imagea...@mailinator.com> wrote in message <gqbdl1$bn1$1...@fred.mathworks.com>...

0 new messages