reducing data that are very close to one another
3 次查看(过去 30 天)
显示 更早的评论
hi! so, I have this problem. I have to do image analysis to get the boundary of an image. I perform image analysis to get a boundary representation of the object. The problem is, there is an overlap of more than one point on the data. the problem occurs due to the pixel width of the boundary. As you can see in the image, I have analysed an image and have the scatter plot showing the coordinates.As you can see, the boundary has more than one element representing it. I need to reduce my vectors such that a boundary is represented by only one line. I attach the code along with the question for your reference.
clc;close all;
clear all;
folder = 'C:\Users\Aaditya\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);
<<
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/145591/image.png)
>>
<<
how do i delete the "extra" data points so that my boundary is represented by only one "line". note that in some cases i have 3 data points also representing a point.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/145592/image.jpeg)
0 个评论
回答(0 个)
另请参阅
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!