You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
how to divide image into 5*5 blocks
1 view (last 30 days)
Show older comments
hello, i have a binarized image of fingerprint that i want to divide into 5*5 blocks and then count the number of zeros " black points " in each block
thanks in advance
Accepted Answer
Walter Roberson
on 13 Jan 2019
Edited: Walter Roberson
on 13 Jan 2019
blockproc(YourImage, [5 5], @(block) nnz(~block.data), 'trimborder', false)
34 Comments
Mahmoud Hassan
on 13 Jan 2019
can you pls explain for me what is that line doing ?!
Mahmoud Hassan
on 13 Jan 2019
Edited: Walter Roberson
on 21 Jan 2019
something is wrong bro, i should get 25 blocks , so 1 value for block and 25 value for the black points in the whole image but this line gives me about 1500 value in the file txt !!
code:
m= blockproc(I, [5 5], @(block) nnz(~block.data), 'trimborder', false);
fprintf(file,'%2.0f, \n',m);
Mahmoud Hassan
on 13 Jan 2019
we have divided the image into 5*5 block so we get 25 blocks. i want the count of blacks in each block so we get 25 value
Walter Roberson
on 13 Jan 2019
No, you asked to divide the image into 5 x 5 blocks. It is a 300 x 300 image as posted, so that would give (300/5) * (300/5) = 60 blocks by 60 blocks.
If you want to divide into 5 blocks per side, use
blockproc(YourImage, [60 60], @(block) nnz(~block.data), 'trimborder', false)
Mahmoud Hassan
on 13 Jan 2019
my fault, you are right thank you so much brother. but one more question pls. if i am doing this process to number of images at a time, and the image size is different for each, then what should be edited otherwise [60 60]
Walter Roberson
on 13 Jan 2019
[r, c, p] = size(YourImage);
r5 = floor(r/5);
c5 = floor(c/5);
if r5 * 5 ~= r || c5 * 5 ~= c
error('Image is not an exact multiple of 5 rows and 5 columns');
end
counts = blockproc(YourImage, [r5 c5], @(block) nnz(~block.data), 'trimborder', false)
In the case where the image is not an exact multiple of 5 rows or 5 columns, other options would include making some of the blocks larger, or discarding part of the image. If part of the image is to be discarded, you would need to decide which part -- e.g., throw away at the bottom always, or at the top always, or alternately from the top and bottom?
Mahmoud Hassan
on 13 Jan 2019
yup i got the error message you made in some images, but i cannot discard anypart of the image so lets keep it ... really thank you so much
Walter Roberson
on 13 Jan 2019
[r, c, p] = size(YourImage);
r5 = floor(r/5);
c5 = floor(c/5);
rbs = [r5 r5 r5 r5 r-4*r5];
cbs = [c5 c5 c5 c5 c-4*c5];
blocks = mat2cell(YourImage, rbs, cbs, p);
counts = arrayfun(@(block) nnz(~block));
Mahmoud Hassan
on 13 Jan 2019
Something is wrong with arrayfun
Walter Roberson
on 14 Jan 2019
Edited: Walter Roberson
on 14 Jan 2019
[r, c, p] = size(J);
r5 = floor(r/5);
c5 = floor(c/5);
rbs = [r5 r5 r5 r5 r-4*r5];
cbs = [c5 c5 c5 c5 c-4*c5];
blocks = mat2cell(J, rbs, cbs);
counts = arrayfun(@(block) nnz(~block), blocks);
Walter Roberson
on 14 Jan 2019
[r, c, p] = size(J);
r5 = floor(r/5);
c5 = floor(c/5);
rbs = [r5 r5 r5 r5 r-4*r5];
cbs = [c5 c5 c5 c5 c-4*c5];
blocks = mat2cell(J, rbs, cbs);
counts = cellfun(@(block) nnz(~block), blocks);
Mahmoud Hassan
on 14 Jan 2019
Edited: Mahmoud Hassan
on 14 Jan 2019
it worked for some images but then got that error, but anyway i am really thankful Walter, i know that i took much of your time and asked you many questions that may be silly for you so thank you so much
Walter Roberson
on 14 Jan 2019
Edited: Walter Roberson
on 14 Jan 2019
some of your images are rgb but most are not. The version I had that puts p at the end of mat2cell would handle rgb but gives the warning for grayscale . You could test ndims(J) to figure out which to use to avoid the warning .
However the rgb ones are likely to give misleading counts unless you im2bw them. You should probably im2bw all of the images.
Mahmoud Hassan
on 14 Jan 2019
Edited: Mahmoud Hassan
on 14 Jan 2019
all the images are binarized bro, tell me walter pls how can i discard maybe the part down right so we can make then all the images is equal 25 blocks ?!
Walter Roberson
on 14 Jan 2019
blocks = mat2cell(J(:,:,1), rbs, cbs);
Note: TIFF can be binary, grayscale, pseudocolor, RGB, RGBA, CMYK, or even multichannel. Just because it displays as if it were binary, that does not mean that it is binarized.
Mahmoud Hassan
on 14 Jan 2019
Edited: Walter Roberson
on 14 Jan 2019
no i mean that i binarized all of them walter, now with this line
blocks = mat2cell(J(:,:,1), rbs, cbs);
it works perfect ... every image is 25 block ... i dont know how to thank you ... you are brilliant walter ... thats was a part of my graduation project and you helped me so much .. can i just ask you here in comments if i needed your help later ?!
Mahmoud Hassan
on 21 Jan 2019
Walter, when i add this line of code that says " ratio = counts / (r5*c5); " then i am saying now that my ratio is equal to the total number of black points in each block divided by number of rows * number of columns ... am i wrong ?
Image Analyst
on 21 Jan 2019
That is the number of black pixels (because the image was inverted with ~ in blockproc) divided by the number of pixels in the block.
Walter Roberson
on 21 Jan 2019
If the code
mat2cell(J(:,:,1), rbs, cbs)
worked for you but
mat2cell(J, rbs, cbs)
did not, then your images are either not binarized or else you have image stacks, and you need to pay close attention to the difference between array sizes and the number of pixels. You would need to track down why ndims(J) > 2 .
Mahmoud Hassan
on 21 Jan 2019
walter i want to calculate the ratio which equals to number of black points in each block divided by number of rows/5 * number of columns/5 ,so in line 23 i just said ratio = counts / (r5*c5) ... am i wrong ??!
Walter Roberson
on 21 Jan 2019
Edited: Walter Roberson
on 21 Jan 2019
Yes, you are wrong. You are not binarizing the images after you read them in.
J = imbinarize(I, 'ForegroundPolarity', 'dark');
Also note that counts will be a 2D array, so ratio will be a 2D array, so your fprintf() are going to repeat over and over again, once for each element of the array.
Furthermore ratio is only correct for the blocks that are r5*c5 which might not be the case for the bottom edge and right edge.
pixel_counts = bxsfun(@times, rbs(:), cbs);
ratio = counts ./ pixel_counts;
Mahmoud Hassan
on 21 Jan 2019
i already binarized them in a step before .... binarize them again using this line ?!
Walter Roberson
on 21 Jan 2019
Note: I added to my response.
Mahmoud Hassan
on 22 Jan 2019
I was using matlab r2014b ... it told me undefined function for the bxsfub and when searched i found that i must have r2016b at least to use this function ... i have downloaded r2018b as free trial now it gives me the same error and one more error for J = imbinarize
Walter Roberson
on 22 Jan 2019
Edited: Walter Roberson
on 22 Jan 2019
pixel_counts = bsxfun(@times, rbs(:), cbs);
ratio = counts ./ pixel_counts;
Sorry, imbinarize() did not exist until R2016a. Use im2bw()
Mahmoud Hassan
on 23 Jan 2019
Edited: Mahmoud Hassan
on 23 Jan 2019
Walter, when i use this line " m = J(:,:,1)>160; " , is that makes a binarization for the image J ? i want to understand it pls
Walter Roberson
on 23 Jan 2019
Not necessarily. We would need to understand why the image is not already grayscale.
If what you see is black and white when you display the image, then probably what you propose would work.
Mahmoud Hassan
on 23 Jan 2019
walter, i was doing all these steps to calculate the angle between two images ... i mean if there are two fingerprints one for walter and the other fingerprint for hassan maybe ... the first image for example is rotated by 30 degree clockwise and hassan’s image isnt rotated so i have to rotate walter’s image by -30 degree and start matching techniques ... so i was dividing the image into blocks and calculate the ratio as we did then calculate the distance between the ratio for the both images ... but i didnt get the efficient result as i expect ... do you think there is something more accurate for this purposoe ?!
Walter Roberson
on 23 Jan 2019
Probably, but I do not do fingerprint related work so I do not know what good techniques are.
Mahmoud Hassan
on 23 Jan 2019
I asked you more than 20 questions and you always replay to me ... you really made me a favour thank you so much walter ... really thank you so much ... thanking wouldn't give you your right anyway
Mahmoud Hassan
on 26 Jan 2019
walter ... when i rotate the original image ... the rotated one is created with this black background ... i dont want that it should be white not black ! how can i avoid it ??
Walter Roberson
on 26 Jan 2019
You should post a new Question with details showing how you rotate. It is not related to this Question as this question does not involve rotation.
Walter Roberson
on 26 Jan 2019
See Also
Categories
Find more on Blue in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)