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

 Akzeptierte Antwort

Walter Roberson
Walter Roberson am 13 Jan. 2019
Bearbeitet: Walter Roberson am 13 Jan. 2019

1 Stimme

blockproc(YourImage, [5 5], @(block) nnz(~block.data), 'trimborder', false)

34 Kommentare

Mahmoud Hassan
Mahmoud Hassan am 13 Jan. 2019
can you pls explain for me what is that line doing ?!
Mahmoud Hassan
Mahmoud Hassan am 13 Jan. 2019
Bearbeitet: Walter Roberson am 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
Mahmoud Hassan am 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
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
Mahmoud Hassan am 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]
[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
Mahmoud Hassan am 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
[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
Mahmoud Hassan am 13 Jan. 2019
Something is wrong with arrayfun
a.png
Walter Roberson
Walter Roberson am 14 Jan. 2019
Bearbeitet: Walter Roberson am 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);
Mahmoud Hassan
Mahmoud Hassan am 14 Jan. 2019
a.png
[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
Mahmoud Hassan am 14 Jan. 2019
Bearbeitet: Mahmoud Hassan am 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
Walter Roberson am 14 Jan. 2019
Bearbeitet: Walter Roberson am 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
Mahmoud Hassan am 14 Jan. 2019
Bearbeitet: Mahmoud Hassan am 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 ?!
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
Mahmoud Hassan am 14 Jan. 2019
Bearbeitet: Walter Roberson am 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
Mahmoud Hassan am 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
Image Analyst am 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.
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
Mahmoud Hassan am 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 ??!
wal.png
Walter Roberson
Walter Roberson am 21 Jan. 2019
Bearbeitet: Walter Roberson am 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
Mahmoud Hassan am 21 Jan. 2019
i already binarized them in a step before .... binarize them again using this line ?!
Walter Roberson
Walter Roberson am 21 Jan. 2019
Note: I added to my response.
Mahmoud Hassan
Mahmoud Hassan am 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
wal.png
Walter Roberson
Walter Roberson am 22 Jan. 2019
Bearbeitet: Walter Roberson am 22 Jan. 2019
pixel_counts = bsxfun(@times, rbs(:), cbs);
ratio = counts ./ pixel_counts;
Sorry, imbinarize() did not exist until R2016a. Use im2bw()
Mahmoud Hassan
Mahmoud Hassan am 23 Jan. 2019
Bearbeitet: Mahmoud Hassan am 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
Walter Roberson am 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
Mahmoud Hassan am 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
Walter Roberson am 23 Jan. 2019
Probably, but I do not do fingerprint related work so I do not know what good techniques are.
Mahmoud Hassan
Mahmoud Hassan am 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
Mahmoud Hassan am 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
Walter Roberson am 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.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Matt J
Matt J am 14 Jan. 2019
Bearbeitet: Matt J am 14 Jan. 2019

0 Stimmen

A more efficient method than blockproc or mat2cell is to use sepblockfun (Download),
result = sepblockfun(~yourImage,[60,60],'sum');
This assumes that yourImage divides evenly into 60x60 blocks (if not, you should zero pad).

Produkte

Version

R2014b

Community Treasure Hunt

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

Start Hunting!

Translated by