# How do derivative masks work for finding edges in image?

but I don't get it why they can detect edges and which one of them can find vertical edges and which one can find harizontal edges?

Basically, the triplet 1,0,-1 (and similarly 2,0,-2) is a discrete, 3-point derivative operator. It can be applied column- (left) or row-wise (right). Let us try first with a row: a flat one, f = 0,0,0,0,0,0,0,0, a step-like s = 0,0,0,1,1,1,1,1 and one hat-like 0,0,0,1,1,0,0,0. 0 here denotes black, and 1 white.
The results will be, on the $6$ central points for each:
• f: 0,0,0,0,0,0
• s: 0,1,1,0,0,0
• h: 0,1,1,-1,-1,0
The same reasoning applies to columns. The Sobel operators are composed of a second filter in the opposite direction: 1,2,1 that smoothes the data. So the global effect: it differentiates in one direction, smoothes in the other. THe resulting value will be high or low, positive or negative, depending on the type of contour in the image.