Divide an image into n Sectors

8 views (last 30 days)
Ritz 1234
Ritz 1234 on 17 Apr 2014
Commented: Image Analyst on 4 May 2014
Hello,
I have an and I have to divide the image into 90 sectors that is each sector of 4 deg.
I found the centre this way :-
clc
clear
origIm = imread('1.bmp'); % test an image
bw = im2bw(origIm); % we use the image from thresholding
[height, width] = size(bw); % store the size of the image
centroid = ceil([height, width]./2); %get the center if the image
I need to find out the density and co-ordinates of all the black pixels in each sector and store them in a matrix. Can you help me with some code if possible ?
I am having trouble thinking how to work on it and get the density and specially co-ordinates of the pixels.
Thank you.

Accepted Answer

Jos (10584)
Jos (10584) on 17 Apr 2014
Edited: Jos (10584) on 17 Apr 2014
Not tested, and off the top of my head, but something along these lines will do:
[hh,ww] = meshgrid(1:height,1:width) ; % indices of all pixels
[theta, r] = cart2pol(hh-centroid(1), ww-centroid(1)) ; % convert to polar coordinates relative to the image centre
% to which sector does each pixel belong
E = 0:4:360 ;
[~, SectorIdx] = histc(theta * (180/pi), E) ;
N = arrayfun(@(k) nnz(bw(SectorIdx==k)==0), 1:max(SectorIdx)) ; % count for each sector the number of black (0) pixels
  2 Comments
Ritz 1234
Ritz 1234 on 17 Apr 2014
Edited: Ritz 1234 on 17 Apr 2014
When I run your code I get this error. Could you please explain your code once, also I dont know anything about bw() in matlab but you used it what is it.
Error I got
Error using accumarray First input SUBS must be a real, full, numeric matrix or a cell vector.
Error in f2 (line 15)
N = accumarray(@(k) nnz(bw(SectorIdx==k)==0), 1:max(SectorIdx)) ;
Jos (10584)
Jos (10584) on 17 Apr 2014
sorry, not accumarray, but arrayfun. I have edited the answer.

Sign in to comment.

More Answers (2)

Image Analyst
Image Analyst on 18 Apr 2014
Why do you need the coordinates of the black pixels? What are you going to do once you know them?
Many BMP images are color, even if they appear to be binary or grayscale. So you never use size() like you did. It's risky. See http://blogs.mathworks.com/steve/2011/03/22/too-much-information-about-the-size-function/ Steve's blog> for an explanation. Do this - it's much safer and more robust:
grayImage = imread(fullFileName);
% Get the dimensions of the image.
% numberOfColorBands should be = 1.
[rows, columns, numberOfColorBands] = size(grayImage);
if numberOfColorBands > 1
% It's not really gray scale like we expected - it's color.
% Convert it to gray scale by taking only the green channel.
grayImage = grayImage(:, :, 2); % Take green channel.
end
I thought of a different way than Jos. If you need it (i.e. only if his does not work), let me know.
  6 Comments
Image Analyst
Image Analyst on 2 May 2014
But you accepted an answer, so is this still a problem? If so, please answer my questions.
Ritz 1234
Ritz 1234 on 4 May 2014
Yes I accepted, but I still have the same problem. I tried it and asked another question http://www.mathworks.in/matlabcentral/answers/127914-find-black-pixel-co-ordinates-in-image
I didn't get much response and hence I choose that as the accepted answer to help other guys if they have some similar problem, so that it wll guide them.
But I still have the problem that I asked in the link above.

Sign in to comment.


Jos (10584)
Jos (10584) on 18 Apr 2014
A slightly other approach:
Retrieve the indices of the black pixels using FIND
[r,c] = find(BW==0) % BW is a 2D array with zeros (black) and ones (white)
Now convert these indices into polar coordinate using cart2pol; subtract the origin (e.g., the centre of the picture [r0, c0]) first:
[theta, rho] = cart2pol(r-r0, c-c0)
Then you can count how many values of theta are within certain boundaries, using HISTC
Boundaries = linspace(0,2*pi,90) % 90 bins
N = histc(theta, Boundaries)
  7 Comments
Ritz 1234
Ritz 1234 on 4 May 2014
Yes sir, I will. Can I send you a message to answer why I want this ? (please)
Image Analyst
Image Analyst on 4 May 2014
Sure, just go ahead and post it here.

Sign in to comment.

Categories

Find more on Convert Image Type 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!