I'm new in matlab. I have a block of image as illustrated below:
Whites show pixel that their values are equal to 1
and Blacks show pixel that their values are equal to 0
,
I want to get vertical only lines
. This means horizontal lines should be removed as illustrated below:
Also I want to get horizontal only lines
. This means vertical lines should be removed as illustrated below:
How can I do it in Matlab
? I prefer morphological operations for this.
Interesting question, because there's so many ways to do that. In essence you need to take out consecutive pixels of a spesific dimension. One way I see to solve this is to convolve with a
[1 1]
or[1 1]'
vector and then take out all the elements where you get the values 2.this will still leave single pixels that you can take out easily using
this is just the main idea, you probably need to be more careful around the edges, or pad with zeros to avoid edge artifacts that conv2 can make)...
Another idea, use the Hough transform to detect lines and keep only those with theta=0 or 90 deg...
Assuming your image is
BW
below:This results in a cell array
B
that contains all the "patches" that are made by connecting neighboring cells with value1
that are connected from one of 4 sides, i.e. not in diagonal.For example:
Each row is one cell coordinates. The first column is its' row, the second its' column, and the first and last cells are always the same.
Now we need to loop through the cells in
B
, and find which of them are lines, either horizontal or vertical, and save them to new matrices.The "Diagonal edges" are what left after we exclude the lines, so we can just look for what we didn't find so far:
This method will ignore anything that is not a one-cell thick line, so for instance, the square in the middle in the image below will be shown only in the Diagonal edges pattern: