direction of flow lines in an image
7 次查看(过去 30 天)
显示 更早的评论
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.
0 个评论
采纳的回答
yanqi liu
2021-10-12
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 个评论
更多回答(1 个)
yanqi liu
2021-10-12
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 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Image Segmentation and Analysis 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!