The document discusses several basic image processing operations in OpenCV including flipping, rotating, resizing, cropping, and extracting color channels. Flipping uses cv2.flip() and takes an image and flip direction. Rotation uses cv2.rotate() and takes an image and rotation angle. Resizing uses cv2.resize() and takes an image and new dimensions. Cropping extracts a region of an image by specifying dimensions. Color channel extraction uses cv2.split() and cv2.merge() with NumPy arrays of zeros to isolate individual color channels in BGR order.
2. Flipping of images
The flipping operation is done to flip the images in defined orientations.
The cv2.flip() function is used to flip the images.
There are two arguments required for this function to work.
The first argument is the location of the read image.
The second argument is the type of flipping, this is given by indices.
0 means flipping around the x-axis (horizontal) axis.
Any positive value means flipping around the y-axis (vertical) axis.
Any negative number means flipping around both the axes.
6. Rotating images
The operation of rotation is done by the function called as cv2.rotate().
There are two arguments required for this function to work.
The first argument is the variable containing the read image.
The second argument is the mode.
There are indices for the modes of rotating an image.
0- rotating 90 degree clockwise
1- rotating 180 degree clockwise
2- rotating 270 degree clockwise
10. Resizing of images
The operation of resizing allows us to change the shape and dimensions of an
image.
This can be done only with the read image or it can be done with the
processed image also.
The function used for this is the cv2.resize().
There are two arguments required here.
The first one is the read image.
The second one is the desired dimensions.
It can be given in (height, width) format.
13. Cropping of images
The cropping operation is done to get a particular portion of an image.
This can be done by altering the height and width of the image.
The dimensions are to be given in square brackets.
There are three dimensions in a colour image namely the height, width and
colour channel.
The , separates the dimensions and the : is used to indicate all values.
So [:,:,:] refers to the original image.
In order to crop the image we have to add numbers in between the :s.
14. Horizontal cropping
This can be achieved by altering the height of the image. Taking any ranges of
values can result in this type of cropping.
16. Combined cropping
This type of cropping is what we do normally in our mobile phones.
This can be achieved by altering both the height and width of image.
This image contains the face of the dog cropped from the entire image.
17. Extracting images
The process of extraction refers to the extraction of the colour channels from
the given colour image. In other words, every colour image is obtained from
the b, g & r equivalents.
It is important to note that in OpenCV the indexing for colour channels are
different when compared to others.
The order here is BGR meaning that blue has index 0, green has index 1 and
red has index 2.
So the extraction must be done in this particular order.
Also this process of extraction is applicable for colour images only.
An error will pop-up if grayscale images are used referring to the
incompatible dimensions.
18. Blue
Blue is the first colour channel present in OpenCV.
First we separate the image into R,G and B using the cv2.split(img) function.
In order to get the blue image we have keep the values of green and red
channels as zero.
This is done using numpy where we create an array of specified shape
containing zeroes.
Then we merge the arrays with that of the split image.
This is point in which the indexing plays a major role.
So for blue it is cv2.merge([b,array,array])
20. Green
Green is the second colour channel present in OpenCV.
First we separate the image into R,G and B using the cv2.split(img) function.
In order to get the green image we have keep the values of blue and red
channels as zero.
This is done using numpy where we create an array of specified shape
containing zeroes.
Then we merge the arrays with that of the split image.
This is point in which the indexing plays a major role.
So for green it is cv2.merge([array,g,array])
22. Red
Red is the last colour channel present in OpenCV.
First we separate the image into R,G and B using the cv2.split(img) function.
In order to get the red image we have keep the values of blue and green
channels as zero.
This is done using numpy where we create an array of specified shape
containing zeroes.
Then we merge the arrays with that of the split image.
This is point in which the indexing plays a major role.
So for red it is cv2.merge([array, array,r])