Borrar filtros
Borrar filtros

Number of pixels inside a pixel circle help!!!

4 visualizaciones (últimos 30 días)
Mojtaba Houballah
Mojtaba Houballah el 28 de Mayo de 2017
Editada: Rik el 28 de Mayo de 2017
my_image =imread('C:\Users\mojtaba.houballah\Desktop\Matlab\road map.PNG');
for R=1:617
for C=1:306
pixel=my_image(R,C);
if pixel<=200 , pixel=0;
[xc,yc]=circle(R,C,0.000000000001);
[xroad,yroad] = find(Roads(:)==0 | Roads(:)==255); end
if (235 < pixel) && (pixel<=250) , pixel= 237; end
if pixel>254 , pixel=255; end
Roads(R,C)=pixel;
end
end
[o,p]=inpolygon(xroad,yroad,xc,yc);
Hello, i have the above code, my goal is to cimpute the number of pixels inside the circle that i draw (i dont know if i defined the circles correctly), the problem that this code is giving me the whole pixel count of the picture, can someone help, thank you in advance
ps: im very new to matlab
find attached the picture i am using
  2 comentarios
Mojtaba Houballah
Mojtaba Houballah el 28 de Mayo de 2017
my_image =imread('C:\Users\mojtaba.houballah\Desktop\Matlab\road map.PNG');
for R=1:617
for C=1:306
pixel=my_image(R,C);
if pixel<=200 , pixel=0; end
if (235 < pixel) && (pixel<=250) , pixel= 237; end
if pixel>254 , pixel=255; end
Roads(R,C)=pixel;
end
end
im_Roads=mat2gray(Roads);
figure,imshow(im_Roads);
title('Road map (2008)');
disp(Roads)
% a = sum(Roads(:)==0);
% b = sum(Roads(:)==255);
% c = sum(Roads(:)==237);
% A = a/(a+b);
% NA = b/(a+b);
% k = randi([1000 1100],1,1);
% A50YEARSnew = (a+10*k)/(a+b); %%keeping in mind that every 5 years we have a new road that increases the accessibility
as you can see i the for loops, i grouped the pixels into 3 colors , one for the roads, one for the non roads, and one for the boundaris of the picture ( which i dont need in my analysis)
so now i am trying to define accessibility areas from the roads, so i am trying to define small circles centered at the pixels that are related to the roads with a radius that i wish to set
i am not sure i understand the code you sent me, i tried it and i got an error (Error using conv2 , First and second arguments must be single or double.)
I am realy very new to matlab (like a week new) so if you can put everything in detail, i would be grateful)
I await your feedback Thank yo for the help
Rik
Rik el 28 de Mayo de 2017
Editada: Rik el 28 de Mayo de 2017
You have an RGB-image, but when you are assigning it to your temporary variable pixel, you're not selecting a color, you should be doing something like pixel=my_image(R,C,:) to get all 3 colors in that variable. So you will have to change something there if you're not going to use my code.
As for what my code does: it counts the number of blue pixels within a predefined radius for each pixel. It first converts your color image to a binary image where blueish pixels are white and the rest is black (just some trial and error to find something that looked OK). Then I create a filter kernel that is a circle with a pre-set radius. You can increase the resolution of this thing by using linspace(0,radius,n_steps) in the meshgrid function.
The final step is a 2D convolution. You should really take a look on Wikipedia or another source that will explain this better, but the gist is this: it will loop through your image pixel by pixel. For every pixel it centers the filter kernel on it, element-wise multiplies the pixels, sums the result and writes that to the result matrix.
If your head is spinning, don't worry, this is terrible to explain in text, these guys on YouTube do a much better job of explaining it. The big difference is that my code does not normalize, it counts how many white pixels there are within the radius (up to 12 in this specific case).
To fix the error, simply convert the filter kernel and the image to doubles by changing the call to conv2:
conv2(double(my_image),double(filter_kernel));

Iniciar sesión para comentar.

Respuestas (1)

Rik
Rik el 28 de Mayo de 2017
Editada: Rik el 28 de Mayo de 2017
I you find out the center and radius of the circle you drew, you can use meshgrid to find the distance of each pixel to the center. That way you can avoid the double loop.
Also, I couldn't find how your syntax with the circle function would produce a circle on a plot.
If you want to find out the road density, I would take a look at convolution. It seems like that is what you need:
%%read the image
my_image =imread('C:\Users\mojtaba.houballah\Desktop\Matlab\road map.PNG');
%my_image =imread('road map.PNG');
%convert to binairy: all pixels that are mostly blue are counted as road
%if you need something else, just tweak it until it works for you
my_image=sum(my_image(:,:,[1 2]),3)*3/4<my_image(:,:,3);
%imshow(my_image)
%%make a filter
radius=2;%number of pixels
[X,Y]=meshgrid(1:ceil(2*radius));%round up so fractional radii work as well
X=X-mean(X(:));Y=Y-mean(Y(:));
filter_kernel=sqrt(X.^2+Y.^2)<=radius;%apparently some Matlab releases don't want you to combine logical and double
%%do the convolution
%apparently some releases will only accept doubles and singles
filtered=conv2(double(my_image),double(filter_kernel));
imshow(filtered,[])

Categorías

Más información sobre Convert Image Type en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by