how to divide image into 5*5 blocks
1 次查看(过去 30 天)
显示 更早的评论
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
采纳的回答
Walter Roberson
2019-1-13
编辑:Walter Roberson
2019-1-13
blockproc(YourImage, [5 5], @(block) nnz(~block.data), 'trimborder', false)
34 个评论
Mahmoud Hassan
2019-1-13
编辑:Walter Roberson
2019-1-21
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
2019-1-13
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
2019-1-13
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
2019-1-13
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
2019-1-13
[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
2019-1-13
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
2019-1-13
[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));
Walter Roberson
2019-1-14
编辑:Walter Roberson
2019-1-14
[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
2019-1-14
[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
2019-1-14
编辑:Mahmoud Hassan
2019-1-14
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
2019-1-14
编辑:Walter Roberson
2019-1-14
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
2019-1-14
编辑:Mahmoud Hassan
2019-1-14
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
2019-1-14
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
2019-1-14
编辑:Walter Roberson
2019-1-14
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
2019-1-21
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
2019-1-21
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
2019-1-21
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
2019-1-21
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
2019-1-21
编辑:Walter Roberson
2019-1-21
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
2019-1-21
i already binarized them in a step before .... binarize them again using this line ?!
Mahmoud Hassan
2019-1-22
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
2019-1-22
编辑:Walter Roberson
2019-1-22
pixel_counts = bsxfun(@times, rbs(:), cbs);
ratio = counts ./ pixel_counts;
Sorry, imbinarize() did not exist until R2016a. Use im2bw()
Mahmoud Hassan
2019-1-23
编辑:Mahmoud Hassan
2019-1-23
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
2019-1-23
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
2019-1-23
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
2019-1-23
Probably, but I do not do fingerprint related work so I do not know what good techniques are.
Mahmoud Hassan
2019-1-23
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
2019-1-26
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
2019-1-26
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
2019-1-26
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Blue 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!发生错误
由于页面发生更改,无法完成操作。请重新加载页面以查看其更新后的状态。
您也可以从以下列表中选择网站:
如何获得最佳网站性能
选择中国网站(中文或英文)以获得最佳网站性能。其他 MathWorks 国家/地区网站并未针对您所在位置的访问进行优化。
美洲
- América Latina (Español)
- Canada (English)
- United States (English)
欧洲
- 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)
亚太
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)