Divide an image into n Sectors
3 次查看(过去 30 天)
显示 更早的评论
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 个评论
采纳的回答
Jos (10584)
2014-4-17
编辑:Jos (10584)
2014-4-17
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 个评论
更多回答(2 个)
Image Analyst
2014-4-18
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 个评论
Image Analyst
2014-5-2
But you accepted an answer, so is this still a problem? If so, please answer my questions.
Jos (10584)
2014-4-18
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 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Convert Image Type 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!