Noober
Noober

Reputation: 1626

Line Simplification image processing

I am trying to use douglas-peucker algorithm to simplify my connected components.I get my connected components using bwlabel and I send it to the douglas algorithm.Here is the link of the algorithm which I am using- Douglas-Peucker Algorithm matlab

Here is my code-

clc;
image=imread('mmm1.jpg');
image = im2bw(image);

[imx imy]=size(image);
n1=zeros(imx,imy);
I=zeros(imx,imy);
L = bwlabel(image,8) ;%Calculating connected components
[r,c] = find(L==1);        %Using 1st connected component
n1=zeros(imx,imy);
rc = [r c];

[ps mm]     = dpsimplify(rc,1);          %Douglas-Peucker algorithm

 %To display original component 
%___________________________________________________________________  
[sx sy]=size(rc);
for j=1:sx
    x1=rc(j,1);
    y1=rc(j,2);
    n1(x1,y1)=1;
end
figure,imshow(n1);
%___________________________________________________________________
 %To display component after simplification 
 n1=zeros(imx,imy);
 [sx sy]=size(mm);
 for j=1:sx
    x1=rc(j,1);
    y1=rc(j,2);
    n1(x1,y1)=1;
 end
 figure,imshow(n1);

This was my original input image- enter image description here

This was my 1st component to which I applied Douglas-Peucker algorithm-

enter image description here

This was the result of algorithm- enter image description here

The code of Douglas-Peucker can be found in the link I mentioned above.So,My question is why is the simplification not happening for the entire component?How can I fix this?

Upvotes: 2

Views: 443

Answers (1)

dhanushka
dhanushka

Reputation: 10682

I believe you want to identify how many straight line segments are there in the image. I did something similar to morphological hit and was able to segment these lines, though not in their full length.

I prepared a vertical line structuring element (SE), then created another two SEs by rotating it by 60 and 120 degrees about its center. I eroded the original image with these SEs and then obtained the connected components.

im = imread('IWVlt.jpg');
bw = im2bw(im, graythresh(im));

% SEs
w = 15;
line = zeros(w);
line(:, round(w/2)) = 1;

bw1 = zeros(size(bw));

for i = 1:3
    bw1 = bw1 + imerode(bw, line);
    line = imrotate(line, 60, 'nearest');
end

[lbl, n] = bwlabel(bw1, 8);

figure, imshow(bw1)
figure, imshow(label2rgb(lbl))

As the result I get 25 components. By varying w in the code you can minimize the error. bonds

For w = 9 which is the lowest value w can take while detecting all segments, I get 26 components. You can filter out components that are too small. bonds2

Upvotes: 2

Related Questions