Calculating mean for lines generated from Hough Transform

Hello everyone,
I'm trying to determine the average RGB colour of lines detected in an image through a Hough transform. I have calcuated the mean of each individual line successful, but I want the mean of all of the lines that are found. The amount of lines could change each time.
for k = 1:length(lines)
xy = [lines(k).point1; lines(k).point2];
plot(xy(:,1),xy(:,2),'LineWidth',2,'Color','green');
% Plot beginnings and ends of lines
plot(xy(1,1),xy(1,2),'x','LineWidth',2,'Color','yellow');
plot(xy(2,1),xy(2,2),'x','LineWidth',2,'Color','red');
% find average road colour - get all co-ords between endpoints
[x, y]=bresenham(xy(1,1),xy(1,2),xy(2,1),xy(2,2));
LineCoOrds{k}=[x, y];
% Get RGB values for these points & their mean
P{k} = impixel(I,x,y);
Meanpix{k}=mean(P{k});
end
I have tried methods such as below, but havent got it quite right.
TotalMeanPixel=cellfun(@mean, Meanpix);
I'm fairly new to Matlab so any help is appreciated a lot.
EDIT - Just to clarify, I want the mean of each individual column, e.g. first column, for all the cells in Meanpix, so I ended up with one value for RGB. I need the average colour of all the lines detected.
Many thanks!
Liz

 Respuesta aceptada

Paul
Paul el 12 de Feb. de 2014
Editada: Paul el 12 de Feb. de 2014
Maybe something like this is what you need:
mean_vec=[];
for ii=1:length(Meanpix)
x=cell2mat(Meanpix{ii});
col1 = x(:,1);
mean_vec=[mean_vec; col1];
end
TotalMeanPixel=mean(mean_vec)

4 comentarios

Liz
Liz el 12 de Feb. de 2014
Editada: Liz el 12 de Feb. de 2014
Thanks a lot for your help, that works for calculating the R component of RGB, but I also need the other 2. I originally tried just following the same example for G and B but at the end it gave me one mean value, whereas I need a 1x3 output. I'm now trying something similar to this but I get 'Subscripted assignment dimension mismatch.'
mean_vec=[];
for ii=1:length(Meanpix)
x=cell2mat(Meanpix(ii));
col1 = x(:,1);
mean_vec(:,1)=[mean_vec; col1];
col2 = x(:,2);
mean_vec(:,2)=[mean_vec; col2];
col3 = x(:,3);
mean_vec(:,3)=[mean_vec; col3];
end
TotalMeanPixel(:,1)=mean(mean_vec(:,1));
TotalMeanPixel(:,2)=mean(mean_vec(:,2));
TotalMeanPixel(:,3)=mean(mean_vec(:,3));
Any help is greatly appreciated! If I work it out I'll post it and give you 'answered' :)
I believe this code works (I calculated by hand too). Thanks for your help!
mean_vec1=[];
mean_vec2=[];
mean_vec3=[];
for ii=1:length(Meanpix)
x=cell2mat(Meanpix(ii));
col1 = x(:,1);
mean_vec1=[mean_vec1; col1];
col2 = x(:,2);
mean_vec2=[mean_vec2; col2];
col3 = x(:,3);
mean_vec3=[mean_vec3; col3];
end
TotalMeanPixelR=mean(mean_vec1);
TotalMeanPixelG=mean(mean_vec2);
TotalMeanPixelB=mean(mean_vec3);
You can do this too which is easier:
mean_mat=[];
for ii=1:length(Meanpix)
x=cell2mat(Meanpix(ii));
mean_mat=[mean_mat; x];
end
TotalMeanPixel=mean(mean_mat);
R_mean=TotalMeanPixel(1);
G_mean=TotalMeanPixel(2);
B_mean=TotalMeanPixel(3);
Liz
Liz el 18 de Feb. de 2014
Even better, thanks a lot!

Iniciar sesión para comentar.

Más respuestas (0)

Preguntada:

Liz
el 12 de Feb. de 2014

Comentada:

Liz
el 18 de Feb. de 2014

Community Treasure Hunt

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

Start Hunting!

Translated by