direction of flow lines in an image

7 views (last 30 days)
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
imgradient
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.

Accepted Answer

yanqi liu
yanqi liu on 12 Oct 2021
sir,please check the follow code to get some information
clc; clear all; close all;
im = imread('https://ww2.mathworks.cn/matlabcentral/answers/uploaded_files/764756/untitled.jpg');
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));
  6 Comments
Abhishek Saini
Abhishek Saini on 12 Oct 2021
thank you very much for a great answer. Are these things in the book. Is there English version of the book available?
yanqi liu
yanqi liu on 12 Oct 2021
sir,thankyou
sorry,the book has not English version
but,we can also discuss

Sign in to comment.

More Answers (1)

yanqi liu
yanqi liu on 12 Oct 2021
sir,please check the follow code to get some information
clc; clear all; close all;
img = imread('cameraman.tif');
[Gx,Gy] = imgradientxy(img);
[Gmag,Gdir] = imgradient(Gx,Gy);
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 Comments
Abhishek Saini
Abhishek Saini on 12 Oct 2021
Thanks yanqi liu for the response. But I donot understand the concept behind this. Why did you use
mk = imrotate(mk, 30);
Whatis the work of theta filter? My question is to find the angle of flow lines with respect to (w.r.t.) horizintal or vertical.
If you rotate the original image, the final results are still same, but it should change w.r.t. image angular direction
yanqi liu
yanqi liu on 12 Oct 2021
sorry,sir,i made a mistake

Sign in to comment.

Products


Release

R2020b

Community Treasure Hunt

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

Start Hunting!