I have a matrix that is roughly 1000 x 2000. What I want to
do is reduce that matrix to something like 100 x 200 (the
bin size I want to be some what optional) by summing up a
window of pixels to create a "super pixel" for a new image.
So, say I have a bin size of 10, than I want to take a
10x10 sample of pixels (a total of 100) add them up to
create the super pixel and then put that pixel in a separate
matrix. Right now I use nested for loops to do this but I
was wondering if Matlab has some sort of windowing tool or
filtering tool that can do this more efficiently so that I
dont have to go through millions of values one by one. A
portion of my code is below. I hope this makes sense.
binsize = 10;
[nrows, ncols, nframes] = size(img);
%Sum all the frames together to create a one summed image
sumframe = zeros(nrows, ncols);
for i = 1:nframes
sumframe = img(:, :, i)+sumframe;
end
%Set the size of the reduced image
smallnrows = nrows/binsize;
smallncols = ncols/binsize;
newimage = zeros(smallnrows, smallncols);
%Create reduced image based on bin size set above
for steprows = 1:smallnrows
for stepcols = 1:smallncols
endrow = steprows*binsize;
endcol = stepcols*binsize;
startrow = endrow - (binsize-1);
startcol = endcol - (binsize-1);
superpixel=0;
for PixelRow=startrow:endrow
for PixelCol = startcol:endcol
pixel = sumframe(PixelRow,PixelCol);
superpixel = pixel + superpixel;
end
end
newimage(steprows, stepcols) = superpixel;
end
end
Subject: I need help Windowing Data in a 2-D matrix
In article <ft6542$68a$1@fred.mathworks.com>,
David House <skyhouse71@hotmail.com> wrote:
>I have a matrix that is roughly 1000 x 2000. What I want to
>do is reduce that matrix to something like 100 x 200 (the
>bin size I want to be some what optional) by summing up a
>window of pixels to create a "super pixel" for a new image.
> So, say I have a bin size of 10, than I want to take a
>10x10 sample of pixels (a total of 100) add them up to
>create the super pixel and then put that pixel in a separate
>matrix. Right now I use nested for loops to do this but I
>was wondering if Matlab has some sort of windowing tool or
>filtering tool that can do this more efficiently so that I
>dont have to go through millions of values one by one.
If your matrix divides into an exact grid (no partial bins left
over), then you can do what you want with a system of reshape()
and permutes... but it won't necessarily be fun to get everything
in the right location. Perhaps easier is to use mat2cell()
which will create a cell array of matrices; then you can
cellfun(@(v) add(v(:)), matrix) the result to get your superpixel
array.
--
"Walter is a great man." -- Dennis Green
Subject: I need help Windowing Data in a 2-D matrix
> So, say I have a bin size of 10, than I want to take a
> 10x10 sample of pixels (a total of 100) add them up to
> create the super pixel and then put that pixel in a
separate matrix...
one of the many solutions
% the data
m=magic(5);
bs=3; % <- your bin size
% the engine
r=filter2(ones(bs),m,'same');
% the result
disp(m);
disp(r);
% the test
% - use submat (1:bs,1:bs)
roi1=sum(sum(m(1:bs,1:bs)))
roi2=r(ceil(bs/2),ceil(bs/2))
us
Subject: I need help Windowing Data in a 2-D matrix
Not quite what he is looking for, though pretty close
> > So, say I have a bin size of 10, than I want to take a
> > 10x10 sample of pixels (a total of 100) add them up to
> > create the super pixel and then put that pixel in a
> separate matrix...
>
% the data
m=magic(9);
bs=3; % <- your bin size
% the engine
r = filter2(ones(bs),a,'valid');
r = r(1:bs:end,1:bs:end);
% the result
disp(m);
disp(r);
Volkan
Subject: I need help Windowing Data in a 2-D matrix
On Apr 4, 5:04=A0pm, "David House" <skyhous...@hotmail.com> wrote:
> I have a matrix that is roughly 1000 x 2000. =A0What I want to
> do is reduce that matrix to something like 100 x 200 (the
> bin size I want to be some what optional) by summing up a
> window of pixels to create a "super pixel" for a new image.
> =A0So, say I have a bin size of 10, than I want to take a
> 10x10 sample of pixels (a total of 100) add them up to
> create the super pixel and then put that pixel in a separate
> matrix. =A0Right now I use nested for loops to do this but I
> was wondering if Matlab has some sort of windowing tool or
> filtering tool that can do this more efficiently so that I
> dont have to go through millions of values one by one. =A0A
> portion of my code is below. =A0I hope this makes sense.
>
> binsize =3D 10;
>
> [nrows, ncols, nframes] =3D size(img);
>
> %Sum all the frames together to create a one summed image
> sumframe =3D zeros(nrows, ncols);
> for i =3D 1:nframes
> =A0 =A0 sumframe =3D img(:, :, i)+sumframe;
> end
>
> %Set the size of the reduced image
> smallnrows =3D nrows/binsize;
> smallncols =3D ncols/binsize;
> newimage =3D zeros(smallnrows, smallncols);
>
> %Create reduced image based on bin size set above
> for steprows =3D 1:smallnrows
> =A0 =A0 for stepcols =3D 1:smallncols
> =A0 =A0 =A0 =A0 endrow =3D steprows*binsize;
> =A0 =A0 =A0 =A0 endcol =3D stepcols*binsize;
> =A0 =A0 =A0 =A0 startrow =3D endrow - (binsize-1);
> =A0 =A0 =A0 =A0 startcol =3D endcol - (binsize-1);
> =A0 =A0 =A0 =A0 superpixel=3D0;
> =A0 =A0 =A0 =A0 for PixelRow=3Dstartrow:endrow
> =A0 =A0 =A0 =A0 =A0 =A0 for PixelCol =3D startcol:endcol
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 pixel =3D sumframe(PixelRow,PixelCol);
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 superpixel =3D pixel + superpixel;
> =A0 =A0 =A0 =A0 =A0 =A0 end
> =A0 =A0 =A0 =A0 end
> =A0 =A0 =A0 =A0 newimage(steprows, stepcols) =3D superpixel;
> =A0 =A0 end
> end
David:
Do you have the image processing toolkit? If so it's a trivial one
line solution.
Just use the imresize() function.
There are several options for subsampling your image, e.g. nearest
neighbor, block averaging down, blinear interpolation, (just going by
memory - I don't remember all the downsizing options), etc. That's
what I'd do (of course, I have the Image Processing Toolkit).
If you don't have that toolkit, you might look into the convolution
function to average your image and then downsample by trying something
like this pseudocode
kernel =3D ones(10,10);
convolved_big_Image =3D conv2(big_Image, kernel);
small_Image =3D convolved_big_Image(1:10:1000, 1:10:2000)
Of course the Image Processing Toolkit also has a conv2 function to do
it in 2 dimensions. If the base MATLAB doesn't have that function
then you could do it in two passes with the 1D conv() function, which
I think is in the base MATLAB.
Regards,
ImageAnalyst
Subject: I need help Windowing Data in a 2-D matrix
I see that MATLAB still hasn't upgraded their newsreader to
properly handle the quoted printable usenet standard. Just
take out the 3D after the = in my prior response. I'll try
to remember to post code from now on via my (primitive,
feature-poor) MATLAB account instead of Google.
Subject: I need help Windowing Data in a 2-D matrix
In article <ftamr5$6cm$1@fred.mathworks.com>,
Image Analyst <imageanalyst@mailinator.com> wrote:
>I see that MATLAB still hasn't upgraded their newsreader to
>properly handle the quoted printable usenet standard.
There is no quoted printabe usenet standard. Character encodings
and MIME concepts did not exist when the usenet standards were created.
>I'll try
>to remember to post code from now on via my (primitive,
>feature-poor) MATLAB account instead of Google.
The problem is actually at Google's end -- it shouldn't be
using quoted printable in what it sends out (at least not without
the user having requested it.)
--
"MAMA: Oh--So now it's life. Money is life. Once upon a time freedom
used to be life--now it's money. I guess the world really do change.
WALTER: No--it was always money, Mama. We just didn't know about it."
-- Lorraine Hansberry
Subject: I need help Windowing Data in a 2-D matrix
ImageAnalyst <imageanalyst@mailinator.com> wrote in message
<06c81e40-f6ab-4a03-9a50-be268b0d60ad@m44g2000hsc.googlegroups.com>...
> On Apr 4, 5:04=A0pm, "David House"
<skyhous...@hotmail.com> wrote:
> David:
> Do you have the image processing toolkit? If so it's a
trivial one
> line solution.
>
> Just use the imresize() function.
>
> There are several options for subsampling your image, e.g.
nearest
> neighbor, block averaging down, blinear interpolation,
(just going by
> memory - I don't remember all the downsizing options),
etc. That's
> what I'd do (of course, I have the Image Processing Toolkit).
>
> If you don't have that toolkit, you might look into the
convolution
> function to average your image and then downsample by
trying something
> like this pseudocode
> kernel =3D ones(10,10);
> convolved_big_Image =3D conv2(big_Image, kernel);
> small_Image =3D convolved_big_Image(1:10:1000, 1:10:2000)
> Of course the Image Processing Toolkit also has a conv2
function to do
> it in 2 dimensions. If the base MATLAB doesn't have that
function
> then you could do it in two passes with the 1D conv()
function, which
> I think is in the base MATLAB.
> Regards,
> ImageAnalyst
I dont know if this helps because I am not just trying to
resize the image, i want to make a 10x10 box, add up those
values and make that summed value a new "pixel" so to speak.
So that would make a 1000 x 2000 image, a 100 x 200 image
with the "super pixels" if you will. Thanks anyway though.
Subject: I need help Windowing Data in a 2-D matrix
In article <fu013n$91d$1@fred.mathworks.com>,
David House <skyhouse71@hotmail.com> wrote:
>I dont know if this helps because I am not just trying to
>resize the image, i want to make a 10x10 box, add up those
>values and make that summed value a new "pixel" so to speak.
> So that would make a 1000 x 2000 image, a 100 x 200 image
>with the "super pixels" if you will.
Did my earlier suggestion of mat2cell and cellfun not work for you?
--
"No one has the right to destroy another person's belief by
demanding empirical evidence." -- Ann Landers
Subject: I need help Windowing Data in a 2-D matrix
roberson@ibd.nrc-cnrc.gc.ca (Walter Roberson) wrote in
message <fu01o5$ar6$1@canopus.cc.umanitoba.ca>...
> In article <fu013n$91d$1@fred.mathworks.com>,
> David House <skyhouse71@hotmail.com> wrote:
>
> >I dont know if this helps because I am not just trying
to
> >resize the image, i want to make a 10x10 box, add up
those
> >values and make that summed value a new "pixel" so to
speak.
> > So that would make a 1000 x 2000 image, a 100 x 200
image
> >with the "super pixels" if you will.
>
> Did my earlier suggestion of mat2cell and cellfun not
work for you?
> --
> "No one has the right to destroy another person's
belief by
> demanding empirical evidence." -- Ann
Landers
I actually hadnt tried that yet and I was planning on
doing that today. However, I just found out that things
are changing so I may not need to do this after all. Oh
well, I think I will still try it so that I know for
future use.
Subject: I need help Windowing Data in a 2-D matrix
roberson@ibd.nrc-cnrc.gc.ca (Walter Roberson) wrote in
message <fu01o5$ar6$1@canopus.cc.umanitoba.ca>...
> In article <fu013n$91d$1@fred.mathworks.com>,
> David House <skyhouse71@hotmail.com> wrote:
>
> >I dont know if this helps because I am not just trying
to
> >resize the image, i want to make a 10x10 box, add up
those
> >values and make that summed value a new "pixel" so to
speak.
> > So that would make a 1000 x 2000 image, a 100 x 200
image
> >with the "super pixels" if you will.
>
> Did my earlier suggestion of mat2cell and cellfun not
work for you?
> --
> "No one has the right to destroy another person's
belief by
> demanding empirical evidence." -- Ann
Landers
I actually hadnt tried that yet and I was planning on
doing that today. However, I just found out that things
are changing so I may not need to do this after all. Oh
well, I think I will still try it so that I know for
future use.
Subject: I need help Windowing Data in a 2-D matrix
roberson@ibd.nrc-cnrc.gc.ca (Walter Roberson) wrote in
message <fu01o5$ar6$1@canopus.cc.umanitoba.ca>...
> In article <fu013n$91d$1@fred.mathworks.com>,
> David House <skyhouse71@hotmail.com> wrote:
>
> >I dont know if this helps because I am not just trying
to
> >resize the image, i want to make a 10x10 box, add up
those
> >values and make that summed value a new "pixel" so to
speak.
> > So that would make a 1000 x 2000 image, a 100 x 200
image
> >with the "super pixels" if you will.
>
> Did my earlier suggestion of mat2cell and cellfun not
work for you?
> --
> "No one has the right to destroy another person's
belief by
> demanding empirical evidence." -- Ann
Landers
I actually hadnt tried that yet and I was planning on
doing that today. However, I just found out that things
are changing so I may not need to do this after all. Oh
well, I think I will still try it so that I know for
future use.
Subject: I need help Windowing Data in a 2-D matrix
roberson@ibd.nrc-cnrc.gc.ca (Walter Roberson) wrote in
message <fu01o5$ar6$1@canopus.cc.umanitoba.ca>...
> In article <fu013n$91d$1@fred.mathworks.com>,
> David House <skyhouse71@hotmail.com> wrote:
>
> >I dont know if this helps because I am not just trying
to
> >resize the image, i want to make a 10x10 box, add up
those
> >values and make that summed value a new "pixel" so to
speak.
> > So that would make a 1000 x 2000 image, a 100 x 200
image
> >with the "super pixels" if you will.
>
> Did my earlier suggestion of mat2cell and cellfun not
work for you?
> --
> "No one has the right to destroy another person's
belief by
> demanding empirical evidence." -- Ann
Landers
I actually hadnt tried that yet and I was planning on
doing that today. However, I just found out that things
are changing so I may not need to do this after all. Oh
well, I think I will still try it so that I know for
future use.
Subject: I need help Windowing Data in a 2-D matrix
roberson@ibd.nrc-cnrc.gc.ca (Walter Roberson) wrote in
message <fu01o5$ar6$1@canopus.cc.umanitoba.ca>...
> In article <fu013n$91d$1@fred.mathworks.com>,
> David House <skyhouse71@hotmail.com> wrote:
>
> >I dont know if this helps because I am not just trying
to
> >resize the image, i want to make a 10x10 box, add up
those
> >values and make that summed value a new "pixel" so to
speak.
> > So that would make a 1000 x 2000 image, a 100 x 200
image
> >with the "super pixels" if you will.
>
> Did my earlier suggestion of mat2cell and cellfun not
work for you?
> --
> "No one has the right to destroy another person's
belief by
> demanding empirical evidence." -- Ann
Landers
I actually hadnt tried that yet and I was planning on
doing that today. However, I just found out that things
are changing so I may not need to do this after all. Oh
well, I think I will still try it so that I know for
future use.
Subject: I need help Windowing Data in a 2-D matrix
roberson@ibd.nrc-cnrc.gc.ca (Walter Roberson) wrote in
message <fu01o5$ar6$1@canopus.cc.umanitoba.ca>...
> In article <fu013n$91d$1@fred.mathworks.com>,
> David House <skyhouse71@hotmail.com> wrote:
>
> >I dont know if this helps because I am not just trying
to
> >resize the image, i want to make a 10x10 box, add up
those
> >values and make that summed value a new "pixel" so to
speak.
> > So that would make a 1000 x 2000 image, a 100 x 200
image
> >with the "super pixels" if you will.
>
> Did my earlier suggestion of mat2cell and cellfun not
work for you?
> --
> "No one has the right to destroy another person's
belief by
> demanding empirical evidence." -- Ann
Landers
I actually hadnt tried that yet and I was planning on
doing that today. However, I just found out that things
are changing so I may not need to do this after all. Oh
well, I think I will still try it so that I know for
future use.
Subject: I need help Windowing Data in a 2-D matrix
roberson@ibd.nrc-cnrc.gc.ca (Walter Roberson) wrote in
message <fu01o5$ar6$1@canopus.cc.umanitoba.ca>...
> In article <fu013n$91d$1@fred.mathworks.com>,
> David House <skyhouse71@hotmail.com> wrote:
>
> >I dont know if this helps because I am not just trying
to
> >resize the image, i want to make a 10x10 box, add up
those
> >values and make that summed value a new "pixel" so to
speak.
> > So that would make a 1000 x 2000 image, a 100 x 200
image
> >with the "super pixels" if you will.
>
> Did my earlier suggestion of mat2cell and cellfun not
work for you?
> --
> "No one has the right to destroy another person's
belief by
> demanding empirical evidence." -- Ann
Landers
I actually hadnt tried that yet and I was planning on
doing that today. However, I just found out that things
are changing so I may not need to do this after all. Oh
well, I think I will still try it so that I know for
future use.
Public Submission Policy
NOTICE: Any content you submit to MATLAB Central, including personal information, is not subject to the protections which may be afforded information collected under other sections of The MathWorks, Inc. Web site. You are entirely responsible for
all content that you upload, post, e-mail, transmit or otherwise make available via MATLAB Central. The MathWorks does not control the content posted by visitors to MATLAB Central and, does not guarantee the accuracy, integrity, or quality of such content.
Under no circumstances will The MathWorks be liable in any way for any content not authored by The MathWorks, or any loss or damage of any kind incurred as a result of the use of any content posted, e-mailed, transmitted or otherwise made available
via MATLAB Central. Read the complete Disclaimer prior to use.