이 질문을 팔로우합니다.
- 팔로우하는 게시물 피드에서 업데이트를 확인할 수 있습니다.
- 정보 수신 기본 설정에 따라 이메일을 받을 수 있습니다.
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 (한국어)