How a binary image can be divided into four equal parts using loop ?

조회 수: 4 (최근 30일)
Zara Khan
Zara Khan 2018년 2월 23일
답변: sumaiya khan 2018년 12월 7일
I have a binary image . I want to divide this into 4 equal parts using a loop and want to store each part individually. later I want to find out the no of white pixels in each parts.
  댓글 수: 3
Zara Khan
Zara Khan 2018년 2월 24일
Image Analyst : its just splitting into four equal quadrants. for each of the quadrants I have to find out f=total no. of pixels/no of object pixels. and this will be done using loop.
Image Analyst
Image Analyst 2018년 2월 25일
OK, but does this mean that the center of the quadrants will be located at the center of the blob, or at the center of the image? And I presume that edges parallel with the edges of the image are okay? And that it's okay if each quadrant does not have the same number of pixels in it?

댓글을 달려면 로그인하십시오.

채택된 답변

John BG
John BG 2018년 2월 24일
편집: John BG 2018년 2월 24일
Hi Zara Khan
I use a for loop to answer your question, as requested, and I have added the variable nq to count the amount of pixels in each quadrant, please have a look, attached script and start image that I have reshaped to 16:9 format, to be able to tell what size was vertical and what horizontal:
1.
start image
clear all;clc;close all
A=imread('im02.png');imshow(A)
.
2.
the for loop with the counting of pixels for each quadrant:
d2=size(A,2);
d1=size(A,1);
vert_bord=floor(d1/2)
horz_bord=floor(d2/2)
nq=[0 0 0 0]; % 1st: top left quadrant red
% 2nd: top right quadrant green
% 3rd: bottom left quadrant blue
% 4th: bottom right quadrant magenta
hold all
A1=A(:,:,1);
for k=1:1:d1*d2
[nk1 nk2]=ind2sub([d1 d2],k);
if nk1<vert_bord && nk2<horz_bord
if A1(nk1,nk2)==255
nq(1)=nq(1)+1;
plot(nk2,nk1,'r.');
end
end;
if nk1<vert_bord && nk2>horz_bord
if A1(nk1,nk2)==255
nq(2)=nq(2)+1;
plot(nk2,nk1,'g.');
end
end;
if nk1>vert_bord && nk2<horz_bord
if A1(nk1,nk2)==255
nq(3)=nq(3)+1;
plot(nk2,nk1,'b.');
end
end;
if nk1>vert_bord && nk2>horz_bord
if A1(nk1,nk2)==255
nq(4)=nq(4)+1;
plot(nk2,nk1,'m.');
end
end;
end
.
3.
resulting quadrants, coloring just to check the counting is correct
.
4.
the amount of white pixels in each quadrant is
nq
=
1007 1084 1235 1666
.
nq(1): top left quadrant, red.
nq(2): top right quadrant, green.
nq(3): bottom left quadrant, blue.
nq(4): bottom right quadrant, magenta.
.
Zara
if you find this answer useful would you please be so kind to consider marking my answer as Accepted Answer?
To any other reader, if you find this answer useful please consider clicking on the thumbs-up vote link
thanks in advance for time and attention
John BG
  댓글 수: 15
Guillaume
Guillaume 2018년 2월 26일
Zara,
With your new question, that's now 4 on the same subject. What your questions seem to show is that you don't appear to know much about image processing, nor how image pixels are stored and accessed. I would suggest you spend more time learning about that from a book.
Zara Khan
Zara Khan 2018년 2월 26일
편집: Zara Khan 2018년 2월 26일
Yes am new to this. The fact is the solution you provided me for my previous problem was ok. But am unable to find out the upper right image. Moreover your code is good with some of my dataset but not for all.as I have 1000 of data so most of the cases am not getting top like left ,right and bottom. So am facing problem. So I asked again if anyone can provide me any other solution . I hope you can link to my previous problem that was the one I accepted your answer.

댓글을 달려면 로그인하십시오.

추가 답변 (2개)

Guillaume
Guillaume 2018년 2월 23일
편집: Guillaume 2018년 2월 23일
I'm not sure why or even how you'd use a loop.
[height, width, ncols] = size(yourimage);
splitimages = mat2cell(yourimage, [height height]/2, [width width]/2, ncols)
Your four images are splitimages{1}, splitimages{2}, splitimages{3}, and splitimages{4}.
To find the number of white pixels in each subimage:
numwhitepixels = cellfun(@(subimg) sum(sum(all(subimg == 1, 3))), splitimages); %assuming images of type double, where white == 1
edit: stupidly forgot the image argument to mat2cell!
  댓글 수: 4
Zara Khan
Zara Khan 2018년 2월 24일
Thanks for the help. Just have done it using a for loop. Then I have used your numwhitepixels. What will be the syntax to find the total no pixels in each part ?
Zara Khan
Zara Khan 2018년 2월 26일
Like no. of white pixels calculation , will I be able to find out area of each cell using a single syntax only ??

댓글을 달려면 로그인하십시오.


sumaiya khan
sumaiya khan 2018년 12월 7일
How can I diagnolly divide the image into 4 quadrants using the centroid of the blob ?

카테고리

Help CenterFile Exchange에서 Computer Vision with Simulink에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by