Azzera filtri
Azzera filtri

How can I divide a 1424*2144 image into a 32*32 blocks?

4 visualizzazioni (ultimi 30 giorni)
Need to divide an image into 32 by 32 blocks and then need to calculate the mean and variance of each block.
  1 Commento
John D'Errico
John D'Errico il 15 Set 2016
Of course, you cannot divide the image as desired, since 1424 is not divisible by 32. There will be one row of blocks at one end that will all be 16x32. Or you will need to pad the image.

Accedi per commentare.

Risposte (6)

Mikael Erkkilä
Mikael Erkkilä il 15 Set 2016
So at first you determine the number of blocks per dimension. Then you go through the image in a loop. This could look like this:
A=ones(1424,2144); % your image
for i=1:size(A,1)/32 % loop from 1 to Nr of blocks in x-dimension
for j=1:size(A,2)/32 % loop from 1 to Nr of blocks in y-dimension
AVG(i,j)=mean(mean(A(i:i+32,j:j+32))); % compute the mean of the block
end
end
Note that with A(i:i+32,j:j+32) you get a 32x32 matrix from the image starting at point (i,j)
  5 Commenti
Prachi Sharma
Prachi Sharma il 27 Set 2016
Hey I am finally able to write code using blockproc. I have written the code below. But how do I find the gradient of the image and then show its figure-
Here is the code-
function stds(b)
a=imresize(b,[512 512]);
myfun = @(block_struct)... uint8(std2(block_struct.data)*ones(size(block_struct.data)));
I2 = blockproc(a,[32 32],myfun);
figure;
imshow(a);
figure;
imshow(I2,[]);

Accedi per commentare.


Walter Roberson
Walter Roberson il 15 Set 2016
If those are to be non-overlapping blocks, then you cannot do that, as the height 1424 is not divisible by 32.
Anyhow, you should be looking at blockproc()

Matt J
Matt J il 15 Set 2016
Modificato: Matt J il 15 Set 2016
Assuming you made a mistake with the dimensions and that they're supposed to divide evenly into 32x32 blocks, then you should use SEPBLOCKFUN ( Download ) from the File Exchange.
result = sepblockfun(A,[32,32],'mean');
This will be more efficient than blockproc, since it takes advantage of the separable properties of mean().
  19 Commenti
Prachi Sharma
Prachi Sharma il 20 Set 2016
Modificato: Matt J il 20 Set 2016
Also you can check the type of output I want.I first tried solving this problem with the code I wrote below.It gives the output as you can see below.For the code you helped me with using sepblockfun,I need the similar output.
Here is the code I tried first-
function AVG=blok(b)
a=imresize(b,[512 512]);
scale=512/32;
for i=1:scale
for j=1:scale
block2=a((i-1)*32+1:i*32,(j-1)*32+1:j*32);
AVG(i,j)=mean(mean(a(i:i+32,j:j+32)));
end
end
The output I get for this goes like this-
>> ymean=blok(a)
ymean=
Columns 1 through 12
235.6400 235.7971 235.9541 236.1038 236.2259 236.3710 236.5096 236.5721 236.6134 236.6713 236.5014 235.7971
234.4298 234.6520 234.8714 235.0799 235.2571 235.4444 235.6593 235.7824 235.8365 235.8935 235.7236 235.0174
233.1524 233.4564 233.7576 234.0487 234.2810 234.4784 234.7089 234.8724 234.9513 235.0110 234.8411 234.1313
231.8815 232.2746 232.6575 233.0129 233.3104 233.5225 233.7438 233.9054 233.9761 234.0331 233.8531 233.1405
230.4757 230.9633 231.4197 231.8246 232.1938 232.4509 232.6860 232.8650 232.9376 233.0028 232.8044 232.0725
229.1093 229.6795 230.1910 230.6382 231.0478 231.3416 231.5840 231.7833 231.8714 231.9504 231.7594 231.0119
227.8255 228.4536 229.0083 229.4913 229.9366 230.2571 230.4803 230.6703 230.7640 230.8540 230.6667 229.9118
226.2498 226.9486 227.5785 228.1442 228.6685 229.0496 229.2663 229.4389 229.5418 229.6437 229.4729 228.7309
Matt J
Matt J il 21 Set 2016
In other words, you now want mean and standard deviation over 32x32x3 blocks? Just give the blocksize [32,32,3] or [32,32,inf] to sepblockfun instead, rather than just [32,32].

Accedi per commentare.


Image Analyst
Image Analyst il 15 Set 2016

Image Analyst
Image Analyst il 20 Set 2016
See attached demos for blockproc(). Adapt as needed.
  1 Commento
Prachi Sharma
Prachi Sharma il 21 Set 2016
Modificato: Prachi Sharma il 21 Set 2016
@Imageanalyst...I am studying and trying to understand blockproc along with distinct block processing for further understanding from the files you have attached.

Accedi per commentare.


Image Analyst
Image Analyst il 21 Set 2016
Why not just use conv2() or imfilter() to compute the mean in a moving window, then subsample? For variance, you can use stdfilt() and then subsample, then square it to get the variance.
  3 Commenti
Prachi Sharma
Prachi Sharma il 21 Set 2016
@MattJ...Trying to apply what you suggested in this part below-
In other words, you now want mean and standard deviation over 32x32x3 blocks? Just give the blocksize [32,32,3] or [32,32,inf] to sepblockfun instead, rather than just [32,32].
Prachi Sharma
Prachi Sharma il 21 Set 2016
Modificato: Matt J il 21 Set 2016
@ImageAnalyst and @MattJ....In between I have tried coding it with a little bit different technique.It is working.Still trying to do with the approaches of blockproc and sepblockfun as you guys suggested.
This is my code-
function [AVG,nstd]=readim(b)
a=imresize(b,[512 512]);
scale=512/32;
for i=1:scale
for j=1:scale
block2=a((i-1)*32+1:i*32,(j-1)*32+1:j*32);
AVG(i,j)=mean(mean(a(i:i+32,j:j+32)));
nstd(i,j)=std2(a(i:i+32,j:j+32));
end
end

Accedi per commentare.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by