Laboratory 1. Working With Images in Opencv
Laboratory 1. Working With Images in Opencv
The OpenCV library offers a large set of functions useful in Computer Vision applications,
originally written in C++. This laboratory uses an OpenCV binding in Python and is based on PyCharm
integrated development environment. A digital image is the equivalent of a matrix. OpenCV allows pixels
or groups of pixels manipulations, displaying and saving images. In order to use the OpenCV functionalities
and also, to easily operate with large arrays, the fallowing libraries should be imported at the beginning of
the program:
import cv2
import numpy as np
NumPy is a Python optimized library for large multi-dimensional arrays, allowing the use of high-level
functions for those arrays.
1
Fundamentals of Image Processing and Computer Vision – Laboratory 1
- retval is the image if it is successfully read, otherwise it is None (if the filename is wrong or the file
is corrupt).
- Path of the image file: this can be an absolute or relative path. This is a mandatory argument.
- Flags: needed for reading an image in a particular format (for example, grayscale/color/with alpha
channel). This argument is optional, with a default value of cv2.IMREAD_COLOR or 1 which loads
the image as a color image. The set of flags available are:
cv2.IMREAD_GRAYSCALE or 0: reads the image in grayscale mode
cv2.IMREAD_COLOR or 1: read a color image (having 3 matrices for 3 color components). Any
transparency of that image will be neglected. It is the default flag.
cv2.IMREAD_UNCHANGED or -1: reads the image as such, including the alpha channel.
with only 1 parameter mat that represents the image to be displayed. When Matplotlib is used in a Terminal
or in a script, as the case is for PyCharm, then it is also required to use the function plt.show() for the
figures to be displayed.
Example:
testImage = cv2.imread("…test_image.jpg",0)
plt.imshow(testImage)
plt.show()
2
Fundamentals of Image Processing and Computer Vision – Laboratory 1
The 2nd option to display an image is OpenCV’s cv2.imshow() function, that will be used while
running the Python script from command line. This function’s syntax is:
with the parameters winname for the name of the window, and mat for the image to be displayed.
Example:
testImage = cv2.imread("…test_image.jpg",0)
cv2.imshow("Test image", testImage)
cv2.waitKey(0)
cv2.destroyAllWindows()
• E1. Task
In the previous example there are 2 new functions (cv2.waitKey() and cv2.destroyAllWindows()).
Search for their syntax and parameters, then explain their utility! Also analyze the syntax / parameters for
the following functions: cv2.namedWindow() and cv2.destroyWindow().
The function parameters are: filename – representing a string with the absolute or relative path where the
image should be saved; img – the image matrix to be saved; params – additional information, as the JPEG
compression quality and others.
3
Fundamentals of Image Processing and Computer Vision – Laboratory 1
itself is an intensity image (or grayscale). The combination of intensity values from the 3 channels gives
the color that is displayed on the screen. The typical color space is RGB, but other color spaces may be
used (HSV, YUV, Lab, etc).
In OpenCV, the order of the channels R, G and B is reversed: the Blue channel is indexed first,
followed by the Green Channel and finally the Red Channel. So OpenCV uses BGR format, while
Matplotlib assumes the image to be in RGB format. There are 2 possible fixes:
- convert the image to RGB color-space using cv2.cvtColor(img, cv2.COLOR_BGR2RGB) or
- reverse the order of channels as in plt.imshow(img[:,:,::-1]) (swaps the 1st and 3rd channel).
Each separate channel can be accessed separately using the function cv2.split(), while
cv2.merge() allows merging back the color components into an image.
Example:
b,g,r = cv2.split(image)
imgMerged = cv2.merge((b,g,r))
plt.figure()
plt.subplot(141);plt.imshow(b,cmap='gray');plt.title("Blue channel")
plt.subplot(142);plt.imshow(g,cmap='gray');plt.title("Green channel")
plt.subplot(143);plt.imshow(r,cmap='gray');plt.title("Red channel")
plt.subplot(144);plt.imshow(imgMerged[:,:,::-1]);plt.title("Merged
Output")
In images with an alpha channel, each pixel has a color value given by the 3 amounts of red, green
and blue, and also a numerical transparency value (between 0 to 255) that defines what will happen when
the pixel is placed over another pixel. The 4th channel is called alpha channel and indicates the transparency.
The alpha mask is a very accurate segmentation of the image (foreground / background) and it is useful for
creating overlays (as in Augmented Reality type of applications).
• E2. Application
Use the alpha-channel from the image Moustache.png to put the moustache on top of the nose-lip region
on the face in trump.jpg ! Try to change the code slightly so the original skin region in the nose-lip area is
partially visible through the moustache! Use the transparency characteristic of the alpha channel.
import cv2
import numpy as np
import matplotlib.pyplot as plt
faceImage = cv2.imread('trump.jpg')
# Load the moustache image with Alpha channel
moustache = cv2.imread('Moustache.png',-1)
print("image Dimension ={}".format(moustache.shape))
# Separate the Color and alpha channels
moustacheBGR = moustache[:,:,0:3]
moustacheMask1 = moustache[:,:,3]
4
Fundamentals of Image Processing and Computer Vision – Laboratory 1
# faceImage is a 3-channel color image, we need a 3 channel image for the mask
moustacheMask = cv2.merge((moustacheMask1,moustacheMask1,moustacheMask1))
# Make the values [0,1] since we are using arithmetic operations
moustacheMask = np.uint8(moustacheMask/255)
face = faceImage.copy()
# Get the nose-lip region from the face image
roi = face[65:110,20:100]
plt.figure()
plt.subplot(121); plt.imshow(faceImage[:,:,::-1])
plt.subplot(122); plt.imshow(face[:,:,::-1])
plt.show()
• E3. Application
- Read the input color image “shoes.jpg” in variable bgrImg, convert it into a grayscale image grayImg
and display it.
- Resize grayImg to have half pixels per horizontal and vertical coordinates, then display the resized image.
Use the function cv2.resize.
- Display the transposed matrix of grayImg. Use the function cv2.transpose.
- Increase by 50 the intensity in grayImg, then decrease the intensity by 50. Display both output images
and comment on the results.
- Generate and display the negative of grayImg.
- Save one of the previous output images in JPEG format with a quality factor of 80.
5
Fundamentals of Image Processing and Computer Vision – Laboratory 1
Figure 1. Primary colors in RGB color space and resulting color combinations
As already mentioned, in OpenCV the image is loaded into BGR format by default, so it is stored
in reverse order. For images with 8-bit per channel, the intensity values for a given channel can range from
0 to 255. Brighter pixels signify higher intensity values in a particular channel and vice-versa. In RGB color
space, all 3 channels contain information about the color as well as brightness. It is better for some
applications if we can separate the color component, also known as Chrominance , from the lightness or
brightness component also known as Luminance. This separation is present in the following color spaces.
6
Fundamentals of Image Processing and Computer Vision – Laboratory 1
HSV is more intuitive than RGB color space because it separates the color and brig–htness into different
axes. This makes it easier to describe any color directly.
Figure 2. Illustration of Hue, Saturation and Value components in HSV color model
In order to convert an image from BGR to HSV format, we will use OpenCV’s cv2.cvtColor function:
7
Fundamentals of Image Processing and Computer Vision – Laboratory 1
Several common parameters are already assigned, while the main input parameters are: x - source
image as an array, bins - number of bins, color - the color for plotting the histogram. The main output
parameters are: hist - histogram array, bins - edges of bins.
Observation: the input to the histogram function is an array, not a complete image matrix. Hence,
it is necessary to flatten the matrix into an array before passing it to the function.
• E4. Application
Imagine building a visual search engine for shoes in an online store where people can search for
shoes by color. Every item in that store will be tagged with the dominant color(s). In this application, you
will identify the dominant color using the hue component in a photo and the image histogram.
- import libraries cv2, matplotlib, and numpy
- read the color image “shoes.jpg” in bgrImage and display it
8
Fundamentals of Image Processing and Computer Vision – Laboratory 1
import matplotlib
matplotlib.rcParams['figure.figsize'] = (10.0, 10.0)
matplotlib.rcParams['image.interpolation']='bilinear'
bgrImage = cv2.imread(“shoes.jpg”)
plt.figure(figsize=[20,10])
plt.imshow(bgrImage [...,::-1])
plt.axis('off')
- convert the image to HSV color space using the cvtColor function and split it into H, S and V channels
hsvImage = cv2.cvtColor(bgrImage,cv2.COLOR_BGR2HSV)
H, S, V = cv2.split(hsvImage)
print(H.shape)
- remove all background pixels with white / light gray values from the Hue array. This can be easily done
by not considering all pixels below a certain saturation level. Use the numpy array method flatten to
transform the foreground significant pixels in the Hue array into a vector
- display the histogram of the Hue vector and identify the dominant color in the original image. Justify your
choice!
plt.figure()
plt.subplot(121);plt.imshow(bgrImage [...,::-1]);
plt.title("Image"); plt.axis('off')
plt.subplot(122);plt.hist(H_array, bins=180, color='r');plt.title("Histogram")
9
Fundamentals of Image Processing and Computer Vision – Laboratory 1
with the parameters src - source 8-bit single channel image, and dst - destination image of the same size
and type as src.
• E5. Application
Apply histogram equalization for the grayscale image “rose.jpg” and compare the images and their
histograms before and after the grayscale transformation. The original image is quite dark, so the result
should be an image with improved contrast (with gray shades covering the entire range from 0-black to
255-white).
- import libraries cv2, matplotlib, matplotlib.pyplot, and numpy
- read the image in grayscale format
im = cv2.imread("rose.jpg", cv2.IMREAD_GRAYSCALE)
imOut = cv2.equalizeHist(im)
- display the original image and the image with enhanced contrast, in the same figure with subplot, as
shown in Figure 5. Then, display their histograms in the same figure, using subplot. Use the numpy method
ravel() that reshapes the original image matrix into a flatten one-dimensional array.
Figure 5. Original ‘rose.jpg’ (left) and image after histogram equalization (right)
10
Fundamentals of Image Processing and Computer Vision – Laboratory 1
Figure 6. Original dark image histogram (left) and equalized histogram (right)
plt.figure()
ax = plt.subplot(1,2,1); plt.imshow(im, vmin=0, vmax=255);
ax.set_title("Original Image");ax.axis('off')
ax = plt.subplot(1,2,2); plt.imshow(imOut, vmin=0, vmax=255);
ax.set_title("Histogram Equalized")
ax.axis('off')
plt.figure()
plt.subplot(1,2,1); plt.hist(im.ravel(),256,[0,256]);
plt.subplot(1,2,2); plt.hist(imOut.ravel(),256,[0,256]);
plt.show()
• E6. Application
Apply histogram equalization for a color image! Do not simply perform histogram equalization for
each channel separately. When each color channel is non-linearly transformed independently, the results
are completely new and unrelated colors. A better solution is to transform the images to a space like HSV
color space, where colors/hue/tint is separated from the intensity, and then perform histogram equalization.
Perform the following steps:
- read the image “flowers.jpg”
- tranform the image to HSV color space
- perform histogram equalization only on the V channel
- transform the image back to RGB color space
- convert the equalized HSV image back to BGR format
- display both images, original and output, in the same figure, using subplot
- display both histograms, original V channel and equalized V channel, in the same figure, using subplot
11
Fundamentals of Image Processing and Computer Vision – Laboratory 1
• E7. Application
Apply the adaptive histogram equalization algorithm called CLAHE to the original image
“night_sky.jpg” and compare the result with usual histogram equalization. Perform the following steps:
- read the color image “night_sky.jpg”
- convert it to HSV color space and save it in hsvImg. Make a copy of hsvImg and denote it hsvImgCopy,
using the method copy() provided by NumPy arrays.
- perform histogram equalization only on the V channel in hsvImg, convert back to BGR format and store
the image with normal equalized histogram in normalEqImg
- create a CLAHE object using
- use the apply() method of the clahe object to perform adaptive histogram equalization for the V
channel of the hsvImgCopy (hsvImgCopy[:,:,0] represents the Hue channel, hsvImgCopy[:,:,1]
is the Saturation channel)
imhsvCLAHE[:,:,2] = clahe.apply(hsvImgCopy[:,:,2])
12
Fundamentals of Image Processing and Computer Vision – Laboratory 1
• E8. Task
Knowing the meaning of the histogram, search for the definitions of Probability Mass Function
(PMF) and Cumulative Distribution Function (CDF). Write their definitions in your report.
13