问题描述
我有带带的图像。条带并不总是 100% 笔直,也不总是 100% 水平。它们可以倾斜和弯曲。每个带具有恒定的宽度。 我需要图像上每个波段的宽度,并尽可能平滑地显示每个波段的边缘: Example_edge_detection.png Example_straight.png Example_tilted.png
我已经有一个 100% 直线水平带的解决方案。但我不知道如何用倾斜的带子做到这一点。也许需要 polyfit 或 houghlines?请帮我。谢谢!
这是直线水平带的工作代码!
clc;
close all;
clear;
workspace;
format long g;
format compact;
fontSize = 20;
folder = pwd;
baseFileName = 'Example_straight.png';
grayImage = imread(baseFileName);
[rows,columns,numberOfColorChannels] = size(grayImage);
if numberOfColorChannels > 1
grayImage = min(grayImage,[],3);
end
hfig = gcf;
hfig.WindowState = 'maximized';
grayImage = adapthisteq(grayImage);
verticalProfile = mean(grayImage,2);
threshold = 118;
binaryImage = imfill(grayImage < threshold,'holes');
% Take the 3 largest blobs
binaryImage = bwareafilt(binaryImage,3);
% Snip off small tendrils using imopen()
binaryImage = imopen(binaryImage,true(1,3));
% Take the 3 largest blobs
binaryImage = bwareafilt(binaryImage,3);
subplot(1,1,1);
imshow(baseFileName);
hfig = gcf;
hfig.WindowState = 'maximized'; %
axis('on','image');
title('Edge Detection','FontSize',fontSize,'Interpreter','None');
binaryProfile = (verticalProfile > threshold)';
bandStarts = strfind(binaryProfile,[0,1]);
bandStops = strfind(binaryProfile,[1,0]);
for k = 1 : length(bandStarts)
yline(bandStarts(k),'Color','r','linewidth',1);
yline(bandStops(k),1);
end
解决方法
看起来您的算法目前只是做了一个简单的阈值来查找水平线,其中阈值应用于两个相邻行之间的平均水平像素值的差异。如果这足以完成工作,那对我来说就足够了。
对于非水平线,也许你可以尝试像this这样的例程,先简单地理顺图像。