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:
dividing an image into 4*4 blocks

Subject: dividing an image into 4*4 blocks

From: love

Date: 13 May, 2011 07:43:05

Message: 1 of 10

hi all
i have an image and i want to divide it into 4*4 block
and save each block in a vector ???
can any one help me????????

Subject: dividing an image into 4*4 blocks

From: Barry Williams

Date: 13 May, 2011 11:34:04

Message: 2 of 10

"love " <mony_fci@hotmail.com> wrote in message <iqine9$qjh$1@newscl01ah.mathworks.com>...
> hi all
> i have an image and i want to divide it into 4*4 block
> and save each block in a vector ???
> can any one help me????????

Imageanalyst will probably come up with a more elegant solution, but I'll give it a shot.

>> I = imread(image.jpg)

>> size(I)

ans =

        2136 2848 3

The top left block of your 4 X 4 will be 534 X 712 pixels, so

I1=I(1:534,1:712,1:3);

And I'll let you do the rest, since I'm not sure what you mean by 'save in a vector'. Do you want to export the image data to a non-MatLab file or to a MatLab variable? Note that the above has saved the top left block of I to a new variable I1.

If I'm understanding what you are trying to do, you could also use imcrop and accomplish the same thing.

Barry

Subject: dividing an image into 4*4 blocks

From: love

Date: 13 May, 2011 11:42:02

Message: 3 of 10

"Barry Williams" <barry.r.williamsnospam@saic.com> wrote in message <iqj4vc$bp$1@newscl01ah.mathworks.com>...
> "love " <mony_fci@hotmail.com> wrote in message <iqine9$qjh$1@newscl01ah.mathworks.com>...
> > hi all
> > i have an image and i want to divide it into 4*4 block
> > and save each block in a vector ???
> > can any one help me????????
>
> Imageanalyst will probably come up with a more elegant solution, but I'll give it a shot.
>
> >> I = imread(image.jpg)
>
> >> size(I)
>
> ans =
>
> 2136 2848 3
>
> The top left block of your 4 X 4 will be 534 X 712 pixels, so
>
> I1=I(1:534,1:712,1:3);
>
> And I'll let you do the rest, since I'm not sure what you mean by 'save in a vector'. Do you want to export the image data to a non-MatLab file or to a MatLab variable? Note that the above has saved the top left block of I to a new variable I1.
>
> If I'm understanding what you are trying to do, you could also use imcrop and accomplish the same thing.
>
> Barry

Barry thnx for ur reply
but I want to divide an image to blocks the size of each block [4 4]

Subject: dividing an image into 4*4 blocks

From: ImageAnalyst

Date: 13 May, 2011 12:13:19

Message: 4 of 10

Like Barry, I'm also puzzled by your description of saving a 2D
subimage as a vector. And even more puzzled why you didn't explain it
better when he hinted that it was vague.

He gave you code for cropping out a sub image from the full image.
Now, try again to explain (better this time) what you want to do after
that.

Subject: dividing an image into 4*4 blocks

From: love

Date: 13 May, 2011 13:19:04

Message: 5 of 10

ImageAnalyst <imageanalyst@mailinator.com> wrote in message <576865a2-6fd1-4d4d-9367-3966eed4b283@x1g2000yqb.googlegroups.com>...
> Like Barry, I'm also puzzled by your description of saving a 2D
> subimage as a vector. And even more puzzled why you didn't explain it
> better when he hinted that it was vague.
>
> He gave you code for cropping out a sub image from the full image.
> Now, try again to explain (better this time) what you want to do after
> that.

i have an image and convert it to binary image zeros ,ones l(matrix of 11111011111000)
and i want to divide this matrix to blocks the size of each block [4 4]
and save each block as v=[1 1 1 1 1 0 1 1 1 1 1 1 1 0 0 0]

Subject: dividing an image into 4*4 blocks

From: Steven_Lord

Date: 13 May, 2011 13:40:20

Message: 6 of 10



"love " <mony_fci@hotmail.com> wrote in message
news:iqjb48$ibg$1@newscl01ah.mathworks.com...
> ImageAnalyst <imageanalyst@mailinator.com> wrote in message
> <576865a2-6fd1-4d4d-9367-3966eed4b283@x1g2000yqb.googlegroups.com>...
>> Like Barry, I'm also puzzled by your description of saving a 2D
>> subimage as a vector. And even more puzzled why you didn't explain it
>> better when he hinted that it was vague.
>>
>> He gave you code for cropping out a sub image from the full image.
>> Now, try again to explain (better this time) what you want to do after
>> that.
>
> i have an image and convert it to binary image zeros ,ones l(matrix of
> 11111011111000)
> and i want to divide this matrix to blocks the size of each block [4 4]
> and save each block as v=[1 1 1 1 1 0 1 1 1 1 1 1 1 0 0 0]

It's still unclear what exactly you're trying to do. Take this matrix:

A = magic(5)

A =

    17 24 1 8 15
    23 5 7 14 16
     4 6 13 20 22
    10 12 19 21 3
    11 18 25 2 9

and show the group EXACTLY what you want the result of performing the
procedure to be. Once you've written up your description, go do something
else for five minutes. When you return, reread your description and ask
yourself "If I had no idea what this was supposed to do, would I understand
it?" If the answer is no, clarify your description and repeat this process
starting with the 5 minute wait. If the answer is yes, post it.

--
Steve Lord
slord@mathworks.com
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

Subject: dividing an image into 4*4 blocks

From: ImageAnalyst

Date: 13 May, 2011 14:24:36

Message: 7 of 10

On May 13, 9:19 am, "love " <mony_...@hotmail.com> wrote:
> i have an image and convert it to binary image zeros ,ones l(matrix of 11111011111000)
> and i want to divide this matrix to blocks the size of each block [4 4]
> and save each block as v=[1 1 1 1 1 0 1 1 1 1 1 1 1 0 0 0]
------------------------------------------------
Crop the image using the method Barry showed you. Then use
reshape(). You might need to transpose the matrix before you send it
in to reshape().

Subject: dividing an image into 4*4 blocks

From: love

Date: 13 May, 2011 17:24:04

Message: 8 of 10

"Steven_Lord" <slord@mathworks.com> wrote in message <iqjcc4$mkc$1@newscl01ah.mathworks.com>...
>
>
> "love " <mony_fci@hotmail.com> wrote in message
> news:iqjb48$ibg$1@newscl01ah.mathworks.com...
> > ImageAnalyst <imageanalyst@mailinator.com> wrote in message
> > <576865a2-6fd1-4d4d-9367-3966eed4b283@x1g2000yqb.googlegroups.com>...
> >> Like Barry, I'm also puzzled by your description of saving a 2D
> >> subimage as a vector. And even more puzzled why you didn't explain it
> >> better when he hinted that it was vague.
> >>
> >> He gave you code for cropping out a sub image from the full image.
> >> Now, try again to explain (better this time) what you want to do after
> >> that.
> >
> > i have an image and convert it to binary image zeros ,ones l(matrix of
> > 11111011111000)
> > and i want to divide this matrix to blocks the size of each block [4 4]
> > and save each block as v=[1 1 1 1 1 0 1 1 1 1 1 1 1 0 0 0]
>
> It's still unclear what exactly you're trying to do. Take this matrix:
>
> A = magic(5)
>
> A =
>
> 17 24 1 8 15
> 23 5 7 14 16
> 4 6 13 20 22
> 10 12 19 21 3
> 11 18 25 2 9
>
> and show the group EXACTLY what you want the result of performing the
> procedure to be. Once you've written up your description, go do something
> else for five minutes. When you return, reread your description and ask
> yourself "If I had no idea what this was supposed to do, would I understand
> it?" If the answer is no, clarify your description and repeat this process
> starting with the 5 minute wait. If the answer is yes, post it.
>
> --
> Steve Lord
> slord@mathworks.com
> To contact Technical Support use the Contact Us link on
> http://www.mathworks.com


Sorry for my weakness in English, I tried to clear my problem but i cann't
thank you for your comment

Subject: dividing an image into 4*4 blocks

From: love

Date: 13 May, 2011 17:25:05

Message: 9 of 10

ImageAnalyst <imageanalyst@mailinator.com> wrote in message <20d56dac-2953-4556-a21f-af1d63b80ccb@24g2000yqk.googlegroups.com>...
> On May 13, 9:19 am, "love " <mony_...@hotmail.com> wrote:
> > i have an image and convert it to binary image zeros ,ones l(matrix of 11111011111000)
> > and i want to divide this matrix to blocks the size of each block [4 4]
> > and save each block as v=[1 1 1 1 1 0 1 1 1 1 1 1 1 0 0 0]
> ------------------------------------------------
> Crop the image using the method Barry showed you. Then use
> reshape(). You might need to transpose the matrix before you send it
> in to reshape().
>
thnx alot

Subject: dividing an image into 4*4 blocks

From: ImageAnalyst

Date: 13 May, 2011 18:07:09

Message: 10 of 10

You can also do it using blockproc(). OK, so this is somewhat
advanced, so I wrote a demo for you.

% 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.
% function test1
function test1()
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')

    % Let's compute and display the histogram, just for fun (not
needed).
    [pixelCount grayLevels] = imhist(grayImage);
    subplot(2, 2, 2);
    bar(pixelCount);
    title('Histogram of original image', 'FontSize', fontSize);
    xlim([0 grayLevels(end)]); % Scale x axis manually.

    thresholdValue = 85;
    binaryImage = grayImage > thresholdValue;

% Display the binary image.
subplot(2, 2, 3);
imshow(binaryImage, []);
caption = sprintf('Binary Image\n%d by %d pixels', ...
rows, columns);
title(caption, 'FontSize', fontSize);

    % Block process the image.
windowSize = 4;
% Each 3x3 block will get replaced by one value.
% Output image will be smaller by a factor of windowSize.
myFilterHandle = @myFilter;
blockyImage = blockproc(binaryImage,[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, 4);
imshow(blockyImage, []);
caption = sprintf('Image Processed in %d by %d Blocks\n%d by %d pixels
\nCustom Block Process', ...
windowSize, windowSize, rowsP, columnsP);
title(caption, 'FontSize', fontSize);

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 block of image data and multiplies it
% element by element by the kernel and
% returns a single value.
function vector8Long = myFilter(blockStruct)
try
        desiredWindowSize = 4;

% Assign default value.
% Will be used near sides of image (due to boundary effects),
% or in the case of errors, etc.
vector8Long = zeros(1, desiredWindowSize^2, 'uint8');

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

        [rows columns numberOfColorBands] = size(subImage2D);
% fprintf('rows=%d, columns = %d\n', rows, columns);
% Make sure filter size matches image block size.
if rows ~= desiredWindowSize || columns ~= desiredWindowSize
% 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');
            fprintf('Partial window: rows=%d, columns = %d\n', rows,
columns);
return;
        else
            % Size matches if we get here, so we're okay.
            % Do the filtering. Multiply by kernel and sum.
            vector8Long = reshape(subImage2D, [1 rows*columns]);
end

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;

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