如果你沒有在x,y元素安德烈使用,您可以通過分割圖像,並使用一個天真的閾值找到他們在該區域避免包括條形碼下面的數字。
我砍死了MATLAB中執行以下操作的解決方案:
- 加載圖像,並使其成爲二進制
- 使用bwlabel提取所有連接組件()。
- 通過regionprops()獲取關於它們每個的有用信息[.centroid對於行的middel點是一個很好的近似值]。
- 閾值化了的小區域(噪聲和數字)
- 提取x,y座標
- 二手Andreys線性擬合解
代碼:
set(0,'DefaultFigureWindowStyle','docked');
close all;clear all;clc;
Im = imread('29ekeap.jpg');
Im=rgb2gray(Im);
%%
%Make binary
temp = zeros(size(Im));
temp(Im > mean(Im(:)))=1;
Im = temp;
%Visualize
f1 = figure(1);
imagesc(Im);colormap(gray);
%Find connected components
LabelIm = bwlabel(Im);
RegionInfo = regionprops(LabelIm);
%Remove background region
RegionInfo(1) = [];
%Get average area of regions
AvgArea = mean([RegionInfo(1:end).Area]);
%Vector to keep track of likely "bar elements"
Bar = zeros(length(RegionInfo),1);
%Iterate over regions, plot centroids if area is big enough
for i=1:length(RegionInfo)
if RegionInfo(i).Area > AvgArea
hold on;
plot(RegionInfo(i).Centroid(1),RegionInfo(i).Centroid(2),'r*')
Bar(i) = 1;
end
end
%Extract x,y points for interpolation
X = [RegionInfo(Bar==1).Centroid];
X = reshape(X,2,length(X)/2);
x = X(1,:);
y = X(2,:);
%Plot line according to Andrey
p = polyfit(x,y,1);
xMin = min(x(:));
xMax = max(x(:));
xRange = xMin:0.01:xMax;
yRange = p(1).*xRange + p(2);
plot(xRange,yRange,'LineWidth',2,'Color',[0.9 0.2 0.2]);
結果是一個不錯的擬合線。你應該能夠通過使用'p'polynomal來擴展它的結尾,並且在你不需要再遇到'1'時進行評估。
結果:
![enter image description here](https://i.stack.imgur.com/o51WM.png)