How the streamline function is implemented?
Show older comments
Hello everyone,
I post this question some time ago, I am still searching about it. I found exactly what I want! However it is a MATLAB built in function and I need to know how it is done?!
Here is the link to streamline function which produces an amazing result for my problem. How these lines are generated? I need the points of these lines not only just drawing them!

Any idea how streamline finds this curved smooth lines?
Thanks a lot.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%5
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%555%%
THIS IS THE OLD QUESTION, WHICH IS NOW TURNED INTO THIS NEW ONE
Hello People,
I have an image processing question. I want to go from 1 to 2 making curved lines (curvature is probably determined by the gradient of the image).
I have the following image. I used edge detection to find the edges of the image, red and green edges are the ones that I need and I got them extracted.
I select a pixel from the red edge (lets name it Pr) and find the closest pixel to it on the green edge (lets call it Pg).
Now the problem is, I don't know how to follow the gradient path of the image (roughly showed in yellow) from red point (Pr) to green point (Pg). I don't want to do straight lines, I connected them directly before but that's not satisfying.
Any ideas how can I do that please? I am thinking of some sort of region growing?!!!
Figure.1. A non-convext gray scale image. Start and finish points are marked with red and green and yellow roughly shows the direction of gradient vectors.

Thanks a lot for your help.
Answers (2)
Image Analyst
on 9 May 2015
Just threshold and call bwboundaries
binaryImage = grayImage > 0;
boundary = bwboundaries(binaryImage);
x = boundary{1}(:, 2);
y = boundary{1}(:, 1);
If you want just the top and bottom edge, you have to identify all indexes which have the x value of the sides and then take the pixels in between. Then, if the points go closckwise along the border, you will have to call fliplr() on the flat bottom section.
9 Comments
Nick Earnhardt
on 9 May 2015
Edited: Nick Earnhardt
on 10 May 2015
Image Analyst
on 9 May 2015
You can draw lines with the quiver() function once you have the vectors.
Nick Earnhardt
on 9 May 2015
Edited: Nick Earnhardt
on 10 May 2015
Image Analyst
on 10 May 2015
Do you know the coordinates of every red pixel and every green pixel?
Nick Earnhardt
on 10 May 2015
Edited: Nick Earnhardt
on 10 May 2015
Image Analyst
on 10 May 2015
Here's a demo that will draw a line from each red dot to the closest green dot, assuming you have the coordinates for both red and green dots, which you said you do already.
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 20;
% Create a circle:
xCenter = 10;
yCenter = 8;
theta = linspace(0, 2*pi, 50);
radius = 7;
redx = radius * cos(theta) + xCenter;
redy = radius * sin(theta) + yCenter;
plot(redx, redy, 'r.', 'MarkerSize', 25);
axis equal;
xlim([0 20]);
ylim([0 16]);
grid on;
% Enlarge figure to full screen.
set(gcf, 'Units', 'Normalized', 'Outerposition', [0, 0, 1, 1]);
% Create green points near the middle
greenx = rand(1, 10) + xCenter - 0.5;
greeny = rand(1, 10) + yCenter - 0.5;
hold on;
plot(greenx, greeny, '.', 'Color', [0, 0.5, 0], 'MarkerSize', 20);
for redRow = 1 : length(redx)
distances = sqrt((redx(redRow) - greenx).^2 + ...
(redy(redRow) - greeny) .^ 2);
[minDistance, indexOfMin] = min(distances);
gx = greenx(indexOfMin);
gy = greeny(indexOfMin);
% Draw line to the closest green dot.
line([redx(redRow), gx], [redy(redRow), gy], 'color', 'b');
end

Nick Earnhardt
on 10 May 2015
Edited: Nick Earnhardt
on 10 May 2015
Image Analyst
on 10 May 2015
Edited: Image Analyst
on 10 May 2015
To hug that curve on the top of your first picture, you may want to treat it as an image - it may help. See this series on finding the shortest path by Steve Eddins, the leader of the imaging team at the Mathworks. http://blogs.mathworks.com/steve/2011/11/01/exploring-shortest-paths-part-1/ first find the two points, like I just showed. Then use bwgeodesicdist() to force it to go within some region of interest.
Nick Earnhardt
on 10 May 2015
Walter Roberson
on 9 May 2015
1 vote
Following the gradient is equivalent to a shortest-path algorithm, such as the Dijkstra Shortest-Path algorithm.
2 Comments
Nick Earnhardt
on 9 May 2015
Edited: Nick Earnhardt
on 10 May 2015
Nick Earnhardt
on 12 May 2015
Categories
Find more on Image Processing Toolbox in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!


