-1

Here is the input image 5.png:

input

Here is my code:

clear all; close all; clc;
%Input Image
A = imread('C:\Users\efu\Desktop\5.png');
% figure, imshow(A);

C=medfilt2(A,[3 5]);
% figure,imshow(C);

D=imfill(C);
% figure,imshow(D);

%Image obtained using MATLAB function 'edge'    
E=edge(D,'canny',[0.01 .02],3);
figure, imshow(E); title('Image obtained using MATLAB function');

image=E;
img=im2bw(image);
% imshow(img)

se = strel('line',3,0);
zz = imerode(img,se);
figure, imshow(zz);

Output after canny edge detection:

output1

After Eroding:

output2

Here the problem is that after eroding all horizontal edges are broken, but I don't want that. I want to extract all horizontal lines without breaking, besides want to remove all vertical and diagonal lines.

Please someone modify the code.

Cris Luengo
  • 55,762
  • 10
  • 62
  • 120
  • first thing i am not shouting. yes its related. and 5.png added. – Mohammad Yakub Jun 09 '17 at 23:54
  • My two cents: Diagonal lines are found by combining two filters one of which is designed to find vertical and the other horizontal lines. So when you remove one of the filters it produces discontinuities. Therefore the lines on the last image are discontinued (w.r.t the previous image) because the diagonal or vertical parts are removed. – NKN Jun 10 '17 at 00:22

1 Answers1

1

It's a hack but it works. A short note, I would advise against using clear all. It has some negative effects such as clearing references to your mex functions. I'm not really sure what that means but there seems to be no real use for it unless you want to clear global variables.

Basically what I did here was threefold. I added a gaussian filter to give a bit more tolerance, increased the erosion rate of strel, and searched across angles to give a bit of angle tolerance. In the end, you have a bit more than you started with on the horizontal part but it does clear the image up a lot better. If you wanted you could just add up the zz matrices and threshold it to get a new binary image which could be a bit closer to your original. Really enjoyed the question by the way and made me look forward to image processing in the fall.

clear; close all;
%Input Image
A = imread('5.png');
% figure, imshow(A);

C=medfilt2(A,[3 5]);
% figure,imshow(C);

D=imfill(C);
% figure,imshow(D);

%Image obtained using MATLAB function 'edge'    
E=edge(D,'canny',[0.01 .02],4);
figure, imshow(E); title('Image obtained using MATLAB function');

image=E;
img=double(image);
img = imgaussfilt(img,.5);
% imshow(img)
zz_out = zeros(size(img));
% se = strel('line',3,-90);
% zz = imerode(img,se);
% se2 = strel('line',3,0);
% zz2 = imerode(img,se2);
for ii = -5:.1:5
    se = strel('line',20,ii);
    zz = imerode(img,se);
    zz_out = or(zz,zz_out);
end
% zz_out = img-zz;
figure, imshow(zz_out);

Output of combined transforms

Durkee
  • 778
  • 6
  • 14