# Best way of segmenting veins in leaves?

I have done a lot of research and found out methods like adaptive thresholding , watershed etc that can be used of detecting veins in leaves . However thresholding isn't good as it introduces a lot of noise

All my images are gray image please could anyone suggest what approaches to adopt while considering this problem in urgent need of help

EDIT:My original image After thresholding As suggested by the answer i have tried the following edge detection

1. Canny

Too much noise and unwanted disturbances 1. Sobel 1. Roberts EDIT: Tried one more operation i get the following result its better than what i tried with canny and adaptive What do you feel? • Could you please show us some images? Mar 14, 2012 at 16:11
– vini
Mar 14, 2012 at 16:22
• @vini Are you currently doing this as a pre-processing step in order to get a good template match later on? Also, how did you get the second image, through simple thresholding? Mar 14, 2012 at 17:19
• My aim is to get the best possible result in segmenting the veins so that my output doesnt contain stray artifacts ,i have used Adaptive thresholding to get the second image
– vini
Mar 14, 2012 at 17:26
• From the images you have given, it looks like you are using (various) filters on a thresholded image. This will give extremely poor results. You should use the filters on the original image, then threshold their output. Jan 26, 2016 at 11:06

You're not looking for edges (=borders between extended areas of high and low gray value), you're looking for ridges (thin lines darker or brighter than their neighborhood), so edge filters might not be ideal: An edge filter will give you two flanks (one on each side of the line) and a low response in the middle of the line: ADD: If've been asked to explain the difference between an edge detector and a ridge detector more clearly. I apologize in advance if this answer is getting very long.

An edge detector is (usually) a first derivative operator: If you imagine the input image as a 3D landscape, an edge detector measures the steepness of the slope at each point of that landscape: If you want to detect the border of an extended bright or dark region, this is just fine. But for the veins in the OP's image it will give you just the same: the outlines left and right of each vein: That also explains the "double line pattern" in the Canny edge detector results: So, how do you detect these thin lines (i.e. ridges), then? The idea is that the pixel values can be (locally) approximated by a 2nd order polynomial, i.e. if the image function is $$g$$, then for small values of $$x$$ and $$y$$:

$$g(x,y)\approx \frac{1}{2} x^2 \frac{\partial ^2g}{\partial x^2}+x y \frac{\partial ^2g}{\partial x\, \partial y}+\frac{1}{2} y^2 \frac{\partial ^2g}{\partial y\, ^2}+x \frac{\partial g}{\partial x}+y \frac{\partial g}{\partial y}+g(0,0)$$

or, in matrix form:

$$g(x,y)\approx \frac{1}{2} \left( \begin{array}{c} x & y \end{array} \right).\left( \begin{array}{cc} \frac{\partial ^2g}{\partial x^2} & \frac{\partial ^2g}{\partial x\, \partial y} \\ \frac{\partial ^2g}{\partial x\, \partial y} & \frac{\partial ^2g}{\partial y\, ^2} \end{array} \right).\left( \begin{array}{cc} x \\ y \end{array} \right)+\left( \begin{array}{cc} \frac{\partial g}{\partial x} & \frac{\partial g}{\partial y} \end{array} \right).\left( \begin{array}{c} x \\ y \end{array} \right)+g(0,0)$$

The second order derivative matrix $$\left( \begin{array}{cc} \frac{\partial ^2g}{\partial x^2} & \frac{\partial ^2g}{\partial x\, \partial y} \\ \frac{\partial ^2g}{\partial x\, \partial y} & \frac{\partial ^2g}{\partial y\, ^2} \end{array} \right)$$ is called the "Hessian matrix". It describes the 2nd order structure we're interested in.

The 2nd order part of this function can be transformed into the sum of two parabolas $$\lambda _1 x^2 + \lambda _2 y^2$$ rotated by some angle, by decomposing the Hessian matrix above to a rotation times a diagonal matrix of it's eigenvalues (Matrix decomposition). We don't care about the rotation (we want to detect ridges in any orientation), so we're only interested in $$\lambda _1$$ and $$\lambda _2$$

What kind of shapes can this function approximation have? Actually, not that many: To detect ridges, we want to find areas in the image that look like the last of the plots above, so we're looking for areas where the major eigenvalue of the Hessian is large (compared to the minor eigenvalue). The simplest way to detect that is just to calculate the major eigenvalue at each pixel - and that's what the ridge filter below does.

A ridge filter will probably give better results. I've tried Mathematica's built in RidgeFilter (which calculates the major eigenvalue of the Hessian matrix at each pixel) on your image: As you can see, there's only a single peak for every thin dark line. Binarizing and skeletonizing yields: After pruning the skeleton and removing small components (noise) from the image, I get this final skeleton: Full Mathematica code:

ridges = RidgeFilter[ColorNegate@src];
skeleton = SkeletonTransform[Binarize[ridges, 0.007]];
DeleteSmallComponents[Pruning[skeleton, 50], 50]


I'm not a Matlab expert, I don't know if it has a built in ridge filter, but I can show you how to implement it "by hand" (again, using Matematica). As I said, the ridge filter is the major eigenvalue of the Hessian matrix. I can calculate that eigenvalue symbolically in Mathematica:

$$\text{eigenvalue}=\text{Last}\left[\text{Eigenvalues}\left[\left( \begin{array}{cc} H_{\text{xx}} & H_{\text{xy}} \\ H_{\text{xy}} & H_{\text{yy}} \end{array} \right)\right]\right]$$

=> $$\frac{1}{2} \left(H_{\text{xx}}+H_{\text{yy}}+\sqrt{H_{\text{xx}}^2+4 H_{\text{xy}}^2-2 H_{\text{xx}} H_{\text{yy}}+H_{\text{yy}}^2}\right)$$

So what you have to do is calculate the second derivatives $$H_{\text{xx}}$$, $$H_{\text{xy}}$$, $$H_{\text{yy}}$$ (using a sobel or derivative of gaussian filter) and insert them into the expression above, and you've got your ridge filter.

• Yes thats exactly what i want however i am doing it in matlab and finding an equivalent of the ridge filter has become difficult
– vini
Mar 16, 2012 at 1:50
• @nikie Very nice answer - question - can you please elaborate on the difference between an edge detector and ridge detector for us non-image processing people? Thanks again Mar 17, 2012 at 0:05
• @Mohammad: I've tried, I hope I made it a little clearer now, despite the math Mar 17, 2012 at 12:46
• i tried ridge filter doesn't give satisfactory results
– vini
Mar 18, 2012 at 17:26
• @vini: "doesn't give satisfactory results" doesn't really tell me much. Do you get the same result image as the one I posted? What is "not satisfactory"? Mar 19, 2012 at 11:32

Following on from the above excellent answer, here is how to do it in python using scikit funcitons.

from skimage.feature import hessian_matrix, hessian_matrix_eigvals

#assume you have an image img

hxx, hxy, hyy = hessian_matrix(img, sigma=3)
i1, i2 = hessian_matrix_eigvals(hxx, hxy, hyy)

#i2 is the variable you want.

#Visualise the result
import matplotlib.pyplot as plt
plt.imshow(i2)

• What img should be? I have a png file and it does not work. Oct 17, 2016 at 23:54
• img should be an 2d numpy array. Nov 14, 2016 at 1:58
• Actually, i1 is the larger of the eigenvalues, so you should use that one.
– Rob
Feb 27, 2017 at 14:29
• This is the clearest explanation I have ever seen! May 3, 2019 at 9:16

When using Canny edge detection (in Halcon), with alpha being 1, and the low threshold 8 and the high threshold 13 (on a scale of 1-255), I get the following result: With tweaking of the parameters, the result you got from Canny can be much more improved. Using this image, you can skip the short edges to remove the noise, and connect the long edges for the final result.

BTW: a different color indicates a different edge.

I can get a fairly similar result using this online Canny edge detector:

• Choose picture I9Pxl.png
• Sigma 1.2
• T-low 0.04
• T-high 0.07
• Other settings default
• Click update view for result
• Thanks :) I guess Canny is just the best ;) Btw, applying Canny on your result might yield even better results.. Mar 15, 2012 at 10:49
• BTW: If you haven't noticed: What are the limitations of a Canny edge detector? You can give your views here! Mar 15, 2012 at 12:18
• If you're telling me: I already gave my view at your question..If you're giving a comment in general: I'll delete this comment. Mar 15, 2012 at 12:24
• Oh yes, - i didn't realize this! Mar 15, 2012 at 12:28
• Thanks for your answer however canny doesnt preserve fine details of veins in leaves which aren't detected as you showed...
– vini
Mar 15, 2012 at 16:18

Instead of thresholding, i have applied simple edge detection.

Used GIMP with Difference of Gaussian - Radious Outer:3.0 and Inner:1.0.

Here it is how it looks like. You can further apply a median filter or erosion/dilation so that you can remove some of the grainy noise.

Here is the page that explains gimp's implementation.

You should refer to different techniques like Laplacian of Gaussian or Difference of Gaussin etc. See this: http://homepages.inf.ed.ac.uk/rbf/HIPR2/log.htm#7

• GIMP ? is which edge detector?
– vini
Mar 14, 2012 at 17:51
• No - it's a image editing package. It was a quick check - just to put a point forward.- use edge detection as opposed to thresholding. Mar 14, 2012 at 17:54
• What edge detector does GIMP use? i am sorry have very little knowledge of it
– vini
Mar 14, 2012 at 18:04
• @vini added reference. Mar 14, 2012 at 18:26

This topic has always attracted a lot of interest, and yet no real consensus exists on the topic. Therefore I decided to drop a few words.

My answers to previously asked similar questions on stackexchange (Q1 and Q2) involved a subpixel curvilinear structure extraction algorithm by Steger. This method performed reasonably well in many cases and luckily, including this one. Therefore I post the output image here: and here with a different parameter setting, and without connected-ness coloring: For the details and proper references please see the stackexchange posts I have referred to.

As part of my last year of engineering studies assignment, I had to study segmentation methods for blood vessels in eye fundus images. I found this tree reconstruction method (by Cohen, Laurent D. and Mille, Julien particularly interesting to use along with Fast-Marching Methods.

Other papers you might want to look into :

• Geodesic Active Contours
• On the implementation of fast marching methods for 3D lattices
• Multistencils FMM : a highly accurate solution to the Eikonal Equation on Cartesian Domains

Useful links : - Front propagation in 2D and 3D

I hope this helps a bit, though it's not exactly state of the art.