# direction of flow lines in an image

18 views (last 30 days)
Abhishek Saini on 12 Oct 2021
Commented: yanqi liu on 12 Oct 2021
Hi,
I have a 2D image or image data. I want to find the average direction of flow lines with respect to horizontal. how can i do that.
Attached is the iamge I generated by using
red lines I draw to show the approximate flow of lines in the image and \theta is the average angle , which I want to determine.

yanqi liu on 12 Oct 2021
clc; clear all; close all;
jm = rgb2lab(im);
s = mat2gray(jm(:,:,2));
bw = im2bw(s);
bw = bwareafilt(bw,1);
bw2 = imopen(bw, strel('line', 19, 0));
bw(bw2) = 0;
bw = bwareafilt(bw,1);
[H,T,R] = hough(bw);
P = houghpeaks(H,5,'threshold',ceil(0.3*max(H(:))));
lines = houghlines(bw,T,R,P,'FillGap',5,'MinLength',7);
max_len = 0;
line_r = [];
for k = 1:length(lines)
xy = [lines(k).point1; lines(k).point2];
% Determine the endpoints of the longest line segment
len = norm(lines(k).point1 - lines(k).point2);
if ( len > max_len)
max_len = len;
xy_long = xy;
line_r = lines(k);
end
end
figure; imshow(im); hold on;
plot(xy_long(:,1),xy_long(:,2),'LineWidth',2,'Color','cyan');
title(sprintf('theta=%.1f°', 90-line_r.theta)); yanqi liu on 12 Oct 2021
sir，thankyou
sorry，the book has not English version
but，we can also discuss

yanqi liu on 12 Oct 2021
clc; clear all; close all;
mk = zeros(16,16);
mk(8:9,:) = 1;
mk = logical(mk);
ms = mk;
% rotae theta
mk = imrotate(mk, 30);
mk = bwmorph(mk,'thin',inf);
% find direction
res = imfilter(double(Gdir), double(mk), 'replicate');
res = res > max(res(:))*0.5;
figure;
subplot(2, 2, 1); imshow(mat2gray(Gdir)); title('origin image');
subplot(2, 2, 2); imshow(mat2gray(ms)); title('h base filter');
subplot(2, 2, 3); imshow(mat2gray(mk)); title('theta filter');
subplot(2, 2, 4); imshow(mat2gray(res)); title('result'); ##### 2 CommentsShow 1 older commentHide 1 older comment
yanqi liu on 12 Oct 2021

### Categories

Find more on Image Arithmetic in Help Center and File Exchange

R2020b

### Community Treasure Hunt

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

Start Hunting!