2012-01-18 11 views
2

這是處理後的圖像,我無法增加bwareaopen(),因爲它不適用於我的其他圖像。如何使用最佳擬合點直線穿過條形碼的質心點Matlab

無論如何,我試圖找到條碼中心點的最短點,以獲得條碼中心點的直線。

示例: 執行質心命令後,條形碼中的點彼此靠近。因此,我只想獲得最短點數(即條形碼)並畫出一條直線。

所有的點不需要加入,最適合的點將做。

步驟1

a busy cat http://i42.tinypic.com/29ekeap.jpg

步驟2

a busy cat http://i44.tinypic.com/16apts5.jpg

步驟3

a busy cat http://i43.tinypic.com/1fztjm.jpg

回答

2

如果你沒有在x,y元素安德烈使用,您可以通過分割圖像,並使用一個天真的閾值找到他們在該區域避免包括條形碼下面的數字。

我砍死了MATLAB中執行以下操作的解決方案:

  1. 加載圖像,並使其成爲二進制
  2. 使用bwlabel提取所有連接組件()。
  3. 通過regionprops()獲取關於它們每個的有用信息[.centroid對於行的middel點是一個很好的近似值]。
  4. 閾值化了的小區域(噪聲和數字)
  5. 提取x,y座標
  6. 二手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

2

如果你已經找到了x,y的中心,你應該使用polyfit函數: 然後,你會找到最佳線的多項式係數。爲了繪製段,你可以採取的最小和最大X

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); 
1

如果你的最終目標是產生垂直於條碼的條和大致經過酒吧的重心線,然後我有另一個選項,供您考慮...

一個簡單的解決方案是執行霍夫變換來檢測條碼中行的主要方向。一旦找到條形碼中的線條角度,您只需旋轉90度即可得到垂直線的斜率。然後條碼的質心可以用作這條線的截距。使用功能HOUGH,並從Image Processing ToolboxHOUGHPEAKS,這裏是從第1步開始你的影像裁剪後的版本代碼:

img = imread('bar_code.jpg'); %# Load the image 
img = im2bw(img);    %# Convert from RGB to BW 

[H, theta, rho] = hough(img); %# Perform the Hough transform 
peak = houghpeaks(H);   %# Find the peak pt in the Hough transform 
barAngle = theta(peak(2));  %# Find the angle of the bars 
slope = -tan(pi*(barAngle + 90)/180); %# Compute the perpendicular line slope 

[y, x] = find(img); %# Find the coordinates of all the white image points 
xMean = mean(x);  %# Find the x centroid of the bar code 
yMean = mean(y);  %# Find the y centroid of the bar code 

xLine = 1:size(img,2);     %# X points of perpendicular line 
yLine = slope.*(xLine - xMean) + yMean; %# Y points of perpendicular line 

imshow(img);    %# Plot bar code image 
hold on;     %# Add to the plot 
plot(xMean, yMean, 'r*'); %# Plot the bar code centroid 
plot(xLine, yLine, 'r'); %# Plot the perpendicular line 

而這裏的結果圖像:

enter image description here