我正在尝试执行图像分析来获得图像的边界。问题是,数据上存在不止一个点的重叠。这个问题是由于边界的像素宽度造成的。也就是说,我的算法读取图像,因为边界很厚,我获得了多个(有时是2到3个)数据点,这些数据点表示您可以在图像中看到的边界.As,我已经分析了一张图像,并用散点图显示了坐标。边界有多个表示它的元素。我需要减少我的向量(坐标),以便边界只由一条线表示。我随问题附上代码供您参考。我这样做是为了得到一个可以代表整个图像的单一数据。
clc;close all;
clear all;
folder = 'C:\Users\Adi\Desktop'; % image folder
baseFileName = 'part8.jpg';
% Get the full filename, with path prepended.
fullFileName = fullfile(folder, baseFileName);
rgbImage = imread(fullFileName); % read the image
% Get the dimensions of the image. numberOfColorBands should be = 3.
[rows, columns, numberOfColorBands] = size(rgbImage);
figure;
greenChannel = rgbImage(:, :, 2);
binaryImage = greenChannel < 200; % convert image to BW
imshow(binaryImage);
i=1;
j=1;
data=zeros(rows,columns);
n=1;% sample every n rows or col of the image
img2=zeros(rows/n,columns/n);
for col=1:n:columns %reducing the size of the image
for row=1:n:rows
if(binaryImage(row,col)==1)
% data(row,col)=1;
x(i)=row;
y(i)=col; % locate where the image is present
img2(x(i),y(i))=1;
i=i+1;
end
end
end
figure;
scatter(x,y);
figure;
imshow(img2);
blankImage=zeros(length(1:rows),length(1:columns));
m=1; % counter variable
for k=2:rows-1
for l=2:columns-1
if((binaryImage(k+1,l)==0)&&(binaryImage(k,l+1)==0)&&(binaryImage(k-1,l)==0)&&(binaryImage(k,l-1)==0)&&(binaryImage(k,l)==0))
% % if all surrounding pixels are black, ignore them
blankImage(k,l)=0;
elseif((binaryImage(k+1,l)==1)&&(binaryImage(k,l+1)==1)&&(binaryImage(k-1,l)==1)&&(binaryImage(k,l-1)==1)&&(binaryImage(k,l)==1))
%if all surrounding pix are white ,ignore them
blankImage(k,l)=0;
else
blankImage(k,l)=1; % get the boundary elements
x_brep(m)=k;
y_brep(m)=l;
m=m+1;
end
end
end
figure;
imshow(blankImage);
figure;
scatter(x_brep,y_brep);示例图像和输出:


发布于 2014-11-25 20:18:20
如果您有图像处理工具箱,您可以获取原始图像,并使用bwmorph -例如,thin或skel,将其减少到单个像素行。请注意,您可能需要反转图像(以便它是黑色背景上的白线)。
假设您的原始图像如图所示已经是二进制的,并且存储在BW中(但是线条是黑线和白线),那么它应该像(~BW只是反转图像)一样简单。
BW2 = bwmorph(~BW,'thin', Inf);如果您的图像不是二进制的,则需要首先将其转换为二进制。
https://stackoverflow.com/questions/26208452
复制相似问题