Chapter 2: Filters and Arithmetic

a.Filters

Image processing is mainly articulated around filters that we can apply on images. This part will quickly introduce you to the more interesting filters and how to use them.

The example below show the usage of various filters and the result showed:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
import cv2.cv as cv

image=cv.LoadImage('img/lena.jpg', cv.CV_LOAD_IMAGE_COLOR) #Load the image
cv.ShowImage("Original", image)

grey = cv.CreateImage((image.width ,image.height),8,1) #8depth, 1 channel so grayscale
cv.CvtColor(image, grey, cv.CV_RGBA2GRAY) #Convert to gray so act as a filter
cv.ShowImage('Greyed', grey)

smoothed = cv.CloneImage(image)
cv.Smooth(image,smoothed,cv.CV_MEDIAN) #Apply a smooth alogrithm with the specified algorithm cv.MEDIAN
cv.ShowImage("Smoothed", smoothed)

cv.EqualizeHist(grey, grey) #Work only on grayscaled pictures
cv.ShowImage('Equalized', grey)

threshold1 = cv.CloneImage(grey)
cv.Threshold(threshold1,threshold1, 100, 255, cv.CV_THRESH_BINARY)
cv.ShowImage("Threshold", threshold1)

threshold2 = cv.CloneImage(grey)
cv.Threshold(threshold2,threshold2, 100, 255, cv.CV_THRESH_OTSU)
cv.ShowImage("Threshold 2", threshold2)

element_shape = cv.CV_SHAPE_RECT
pos=3
element = cv.CreateStructuringElementEx(pos*2+1, pos*2+1, pos, pos, element_shape)
cv.Dilate(grey,grey,element,2) #Replace a pixel value with the maximum value of neighboors
#There is others like Erode which replace take the lowest value of the neighborhood
#Note: The Structuring element is optionnal
cv.ShowImage("Dilated", grey)

cv.WaitKey(0)

Additional informations:

  • For smooth function there is various others algorithm like CV_BLUR, CV_GAUSSIAN and many others. We can see that on the smoothed image of lena her face is more plain.
  • EqualizedHist intent to equilibrate every possible gray values(0..255) to get a repartition spectrum as flat as possible.
  • Threshold is for far the more important filter of all ! It works only on gray picture which pixels values are included between 0 and 255. Then it takes a treshold value all pixels under are set to 0 so black and all the pixels above are set to 255 so white. Note: that CV_THRESH_BINARY_INV just switch to resulting color.
  • The second threshold example use CV_THRESH_OTSU which try to equilibrate the balance of black and white to get the same amount on each side.
  • The last Dilate┬áreplace a pixel value with the maximum value of his neighbors. In this example it also use a specific shape to apply to algorithm that’s why it creates in results large squares.

Original Image:

Smoothed:

Gray:

Equalized:

Thresholded Binary:

Thresholded OTSU:

Dilated:

b. HighGUI

OpenCV comes with a built-in trivial GUI tools that allow the user to show a picture in a window without being obliged to use another gui platform. But it brings a lot more functionalities which include grabbing keyboard events, grabbing mouse position on a window, showing a trackbar. The example below show how to use a trackbar to modify the value of a threshold.

Note: In the sources provided, is also include in example using webcam.

1
2
3
4
5
6
7
8
9
10
11
12
13
import cv2.cv as cv

im = cv.LoadImage("img/lena.jpg", cv.CV_LOAD_IMAGE_GRAYSCALE)
thresholded = cv.CreateImage(cv.GetSize(im), 8, 1)

def onChange(val):
    cv.Threshold(im, thresholded, val, 255, cv.CV_THRESH_BINARY)
    cv.ShowImage("Image", thresholded)

onChange(100) #Call here otherwise at startup. Show nothing until we move the trackbar
cv.CreateTrackbar("Thresh", "Image", 100, 255, onChange) #Threshold value arbitrarily set to 100

cv.WaitKey(0)

c. ROI: Region Of Interest

ROI is a pretty interesting feature that allows to select a part of a picture make some operations on it as if it was the entire picture but changes will be applied only to the selected area. Then you can reset the ROI and later changes will be applied on the whole picture. The example below reset to zero a part of an image using ROI.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
import cv2.cv as cv

im = cv.LoadImage("img/lena.jpg",3)

cv.SetImageROI(im, (50,50,150,150)) #Give the rectangle coordinate of the selected area

cv.Zero(im)
#cv.Set(im, cv.RGB(100, 100, 100)) put the image to a given value

cv.ResetImageROI(im) # Reset the ROI

cv.ShowImage("Image",im)

cv.WaitKey(0)

d. Arithmetics

As we do with numbers we can do arithmetics with picture, but in this case the operation is applied on every pixels. The example below show the effect of different operations on two image of the same size.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import cv2.cv as cv#or simply import cv

im = cv.LoadImage("img/lena.jpg")
im2 = cv.LoadImage("img/fruits-larger.jpg")
cv.ShowImage("Image1", im)
cv.ShowImage("Image2", im2)

res = cv.CreateImage(cv.GetSize(im2), 8, 3)

cv.Add(im, im2, res) #Add every pixels together (black is 0 so low change and white overload anyway)
cv.ShowImage("Add", res)

cv.AbsDiff(im, im2, res) # Like minus for each pixel im(i) - im2(i)
cv.ShowImage("AbsDiff", res)

cv.Mul(im, im2, res) #Multiplie each pixels (almost white)
cv.ShowImage("Mult", res)

cv.Div(im, im2, res) #Values will be low so the image will likely to be almost black
cv.ShowImage("Div", res)

cv.And(im, im2, res) #Bit and for every pixels
cv.ShowImage("And", res)

cv.Or(im, im2, res) # Bit or for every pixels
cv.ShowImage("Or", res)

cv.Not(im, res) # Bit not of an image
cv.ShowImage("Not", res)

cv.Xor(im, im2, res) #Bit Xor
cv.ShowImage("Xor", res)

cv.Pow(im, res, 2) #Pow the each pixel with the given value
cv.ShowImage("Pow", res)

cv.Max(im, im2, res) #Maximum between two pixels
#Same form Min MinS
cv.ShowImage("Max",res)

cv.WaitKey(0)

Original images:

Add:

AbsDiff:

Mul:

Div:

And:

Or:

Not:

Xor:

Pow:

Max:

<<Starting with OpenCV | Home | Pixel Access and Matrix Iteration>>