在matlab中减少彼此非常接近的数据以获得一组数据

时间:2014-10-05 23:55:52

标签: matlab image-processing

我正在尝试执行图像分析以获取图像的边界。问题是,数据上有一个以上的重叠点。由于边界的像素宽度,会出现问题。也就是说,我的算法读取图像,并且由于边界很厚,我得到多个(有时是2或3个)数据点来表示边界。正如您在图像中看到的,我已经分析了一个图像并且散点图显示该坐标。边界有多个代表它的元素。我需要减少我的矢量(坐标),使边界只用一条线表示。我附上代码和问题供您参考。我这样做是为了获得一个可以代表整个图像的单个数据。

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

示例图像和输出: enter image description here

enter image description here

1 个答案:

答案 0 :(得分:0)

如果您有图像处理工具箱,则可以拍摄原始图像,并使用bwmorph - 例如thinskel将其缩小为单个像素线。请注意,您可能需要反转图像(使其在黑色背景上为白线)。

假设你所显示的原始图像已经是二进制的并且存储在BW中(但是白色的线条为黑色),它应该像(~BW只是反转图像一样简单)。

BW2 = bwmorph(~BW,'thin', Inf);

如果您的图像不是二进制图像,则需要先将其转换为二进制图像。