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.

 採用された回答

Jos (10584)
Jos (10584) 2014 年 4 月 17 日
編集済み: Jos (10584) 2014 年 4 月 17 日

1 投票

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 件のコメント

Ritz 1234
Ritz 1234 2014 年 4 月 17 日
編集済み: Ritz 1234 2014 年 4 月 17 日
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) 2014 年 4 月 17 日
sorry, not accumarray, but arrayfun. I have edited the answer.

サインインしてコメントする。

その他の回答 (2 件)

Image Analyst
Image Analyst 2014 年 4 月 18 日

1 投票

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 件のコメント

Ritz 1234
Ritz 1234 2014 年 4 月 18 日
I am working a feature of image and after dividing the image into sectors and finding the co-ordinates of the black pixels then I will find its distance from the centroid and then try to find something useful from that. Or even If I can find the density of the black pixels in the region. I am having trouble understanding how to do so. I am relatively new to MATLAB.
Thank you.
Image Analyst
Image Analyst 2014 年 4 月 18 日
You're going to have to define "something useful from that" more precisely if you are going to program it up. Why don't you show your images and tell us what you need to measure. I presume that you need to invent some new feature because none of the standard ones distinguish between two images that you know are different. But to give advice on that I'd need to see your images. You can attach them with the image icon above the edit box. And tell me what features you measured do not do a good job and force you to invent a new one.
Ritz 1234
Ritz 1234 2014 年 4 月 19 日
Actually I did attach the images earlier during posting there might have been some error. Okay I will attach it soon.
Ritz 1234
Ritz 1234 2014 年 4 月 29 日
Here's the sample image:-
Image Analyst
Image Analyst 2014 年 5 月 2 日
But you accepted an answer, so is this still a problem? If so, please answer my questions.
Ritz 1234
Ritz 1234 2014 年 5 月 4 日
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.

サインインしてコメントする。

Jos (10584)
Jos (10584) 2014 年 4 月 18 日

1 投票

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 件のコメント

Ritz 1234
Ritz 1234 2014 年 4 月 19 日
Sir can you please explain your earlier codes and this code in short in reference to the commands and works you have done.
Thank you.
Jos (10584)
Jos (10584) 2014 年 4 月 20 日
doc find
doc cart2pol
doc linspace
doc histc
doc arrayfun
Ritz 1234
Ritz 1234 2014 年 4 月 29 日
Thank you sir, it really helped me. Another thing that I wanted to know is that after we divide them into sectors how to get the co-ordinates of all the black pixels in that sector and store them in a matrix. Can you please tell me a way to do so ?
Thank you for your answers I am honored.
Jos (10584)
Jos (10584) 2014 年 5 月 2 日
[N, idx] = N = histc(theta, Boundaries)
SegmentNumber = 3 ; % example
tf = idx == SegmentNumber ; % true for all thetas that fall within the segment
[theta(tf) r(tf) c(tf)] % corresponding indices r and c
Image Analyst
Image Analyst 2014 年 5 月 2 日
I just gave an answer where I divided an image into pie-shaped sector(s). Adapt this if you want: http://www.mathworks.com/matlabcentral/answers/127864#comment_211537 If you really need the (x,y) coordinates (which I doubt), then you can do
[blackRows, blackColumns] = find(~mask);
You'd have to explain why you need every single black pixel coordinate in an N by 2 list of (x,y) coordinates to me, because I've never needed that as far as I can recall.
Ritz 1234
Ritz 1234 2014 年 5 月 4 日
Yes sir, I will. Can I send you a message to answer why I want this ? (please)
Image Analyst
Image Analyst 2014 年 5 月 4 日
Sure, just go ahead and post it here.

サインインしてコメントする。

カテゴリ

ヘルプ センター および File ExchangeRead, Write, and Modify Image についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by