Divide an image into n Sectors
8 views (last 30 days)
Show older comments
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.
0 Comments
Accepted Answer
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
More Answers (2)
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
on 2 May 2014
But you accepted an answer, so is this still a problem? If so, please answer my questions.
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
See Also
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!