Plot average line using for loop iterations

I'm trying to plot a red line that is the average length of where two green line intersect and average angle to the y axis between each two of the green line iterations starting at the red point. I.e., I want to find where green line 1 and 2 intersect, the average length of the two lines up to where they intersect, the average angle of each line to the y axis up to where they intersect, and then plot a red line between the first and second green line the average of these lengths and angles, then another red line between the second and third green line, and so on. Original image is above.
img = double(mat2gray(imread('071318.bmp')));
mask_size=[5 5];
sigma=3;
Gaussian_filter = fspecial('gaussian',mask_size,sigma);
A=conv2(img, Gaussian_filter, 'same');
Gx = [-1, 0, 1; -2, 0, 2; -1, 0, 1];
Gy = [1, 2, 1; 0, 0, 0; -1, -2, -1];
SobelX = conv2(A, Gx, 'same');
SobelY = conv2(A, Gy, 'same');
gradient_direction = (atan2(SobelY, SobelX))*180/pi;
gradient_magnitude = sqrt((SobelX.^2) + (SobelY.^2));
Threshold_High = min(min(gradient_magnitude))+max(max(gradient_magnitude))*.09;
Threshold_Low = Threshold_High/2;
rows=size(A,1);
columns=size(A,2);
for i=1:rows
for j=1:columns
if (gradient_direction(i,j)<0)
gradient_direction(i,j)=360+gradient_direction(i,j);
end
end
end
edge_direction=zeros(rows,columns);
for i = 1 : rows
for j = 1 : columns
if ((gradient_direction(i,j)>=0)&&(gradient_direction(i,j)<22.5)||(gradient_direction(i,j)>=157.5)&&(gradient_direction(i,j)<202.5)||(gradient_direction(i,j)>=337.5)&&(gradient_direction(i,j)<=360))
edge_direction(i,j)=0;
elseif((gradient_direction(i,j)>=22.5)&&(gradient_direction(i,j)<67.5)||(gradient_direction(i,j)>=202.5)&&(gradient_direction(i,j)< 247.5))
edge_direction(i,j)=45;
elseif((gradient_direction(i,j)>=67.5&&gradient_direction(i,j)<112.5)||(gradient_direction(i,j)>=247.5&&gradient_direction(i,j)<292.5))
edge_direction(i,j)=90;
elseif((gradient_direction(i,j)>=112.5&&gradient_direction(i,j)<157.5)||(gradient_direction(i,j)>=292.5&&gradient_direction(i,j)<337.5))
edge_direction(i,j)=135;
end
end
end
edge1 = zeros (rows, columns);
for i=2:rows-1
for j=2:columns-1
if (edge_direction(i,j)==0)
edge1(i,j)=(gradient_magnitude(i,j)==max([gradient_magnitude(i,j),gradient_magnitude(i,j+1),gradient_magnitude(i,j-1)]));
elseif (edge_direction(i,j)==45)
edge1(i,j)=(gradient_magnitude(i,j)==max([gradient_magnitude(i,j),gradient_magnitude(i+1,j-1),gradient_magnitude(i-1,j+1)]));
elseif (edge_direction(i,j)==90)
edge1(i,j)=(gradient_magnitude(i,j)==max([gradient_magnitude(i,j),gradient_magnitude(i+1,j),gradient_magnitude(i-1,j)]));
elseif (edge_direction(i,j)==135)
edge1(i,j)=(gradient_magnitude(i,j)==max([gradient_magnitude(i,j),gradient_magnitude(i+1,j+1),gradient_magnitude(i-1,j-1)]));
end
end
end
edge2 = edge1.*gradient_magnitude;
Threshold_Low = Threshold_Low * max(max(edge2));
Threshold_High = Threshold_High * max(max(edge2));
edge_binary = zeros (rows, columns);
for i = 1 : rows
for j = 1 : columns
if (edge2(i, j) < Threshold_Low)
edge_binary(i, j) = 0;
elseif (edge2(i, j) > Threshold_High)
edge_binary(i, j) = 1;
elseif(edge2(i+1,j)>Threshold_High||edge2(i-1,j)>Threshold_High||edge2(i,j+1)>Threshold_High||edge2(i,j-1)>Threshold_High||edge2(i-1,j-1)>Threshold_High||edge2(i-1,j+1)>Threshold_High||edge2(i+1,j+1)>Threshold_High||edge2(i+1,j-1)>Threshold_High)
edge_binary(i,j)=1;
end
end
end
edge_binary(1600:2054,:)=[];
edge_binary(:,1:300)=[];
edge_binary(602,526)=1;
ind = find(edge_binary',1,'last');
[x0,y0] = ind2sub(size(edge_binary'),ind);
ind = find(edge_binary',1,'first');
[xmax,ymax] = ind2sub(size(edge_binary'),ind);
imshow(imcomplement(edge_binary));
hold on
axis equal
title('Captive Bubble Air-Liquid Interface')
xlabel('x')
ylabel('y')
plot([x0 x0],[y0 ymax],'k')
hline_step=50;
n=round((y0-ymax)/hline_step,-1);
x=zeros(n,1);
y=x;
for i = 1:n-1
y(i) = y0-i*hline_step;
x(i) = find(edge_binary(y(i),:),1,'first');
plot([x(i) x0], [y(i) y(i)],'b');
end
for i = 1:n-2
xg = (x(i+1)+x(i))/2;
yg = (y(i+1)+y(i))/2;
yg_p = (x(i+1)-x(i))/(y(i+1)-y(i))*(xg-x0) + yg;
plot([xg x0],[yg yg_p],'g');
end
for i=1:n-3
plot(x(i+1),y(i+1),'.r');
end
x=nonzeros(x');
x=reshape(x,size(x,1),1);
y=nonzeros(y');
y=reshape(y,size(y,1),1);
plot(x,y,'r')
hold off

댓글 수: 1

I am unable to understand your question. From whatever i understood i would suggest creating a table with the average lengths of green lines and their angles with the y-axis and then plot the red lines according to your requirement. I'll be in a better position to help if you can rephrase your question.

댓글을 달려면 로그인하십시오.

답변 (0개)

카테고리

도움말 센터File Exchange에서 Annotations에 대해 자세히 알아보기

질문:

2019년 8월 5일

댓글:

2019년 8월 9일

Community Treasure Hunt

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

Start Hunting!

Translated by