0% found this document useful (0 votes)
47 views

Computer Vision Undergrad

The document discusses various color models and how to convert between them using Python and libraries like scikit-image and OpenCV. It provides examples of converting between RGB, HSV, XYZ, LAB, and adjusting image properties like gamma, contrast, and brightness. Key color models covered include RGB, HSV, XYZ, and LAB. Conversion between color models and adjusting image properties like gamma, contrast, and brightness are demonstrated through examples using functions from scikit-image and OpenCV.

Uploaded by

Charles Oppong
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
47 views

Computer Vision Undergrad

The document discusses various color models and how to convert between them using Python and libraries like scikit-image and OpenCV. It provides examples of converting between RGB, HSV, XYZ, LAB, and adjusting image properties like gamma, contrast, and brightness. Key color models covered include RGB, HSV, XYZ, and LAB. Conversion between color models and adjusting image properties like gamma, contrast, and brightness are demonstrated through examples using functions from scikit-image and OpenCV.

Uploaded by

Charles Oppong
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 23

Color Models

Reading, Writing and Display


• We can load images using skimage, pandas
• The opencv is an important library in computer vision to work with in
image recognition, detection etc
skimage
• Most functions of skimage can take 3D images as input arguments.
• Open a color image on your disk as a NumPy array.
• Find a skimage function computing the histogram of an image and
plot the histogram of each color channel.
• Convert the image to grayscale and plot its histogram.
• etc
• The skimage module contains different image processing algorithms.
• To upload and view the image, we use a class from the skimage
module called io.
• Inside this class, we use the imread function to upload and read an
image
• The function imshow is used to view the image.
Example
1. # import libraries
2. from skimage import io 10. # Convert back to RGB
3. from skimage import color 11. img_rgb = color.hsv2rgb(img_hsv)
4. from skimage import data
5. from pylab import * 12. # Show both figures
13. figure(0)
6. # Read image 14. io.imshow(img_hsv)
7. img = io.imread('ceci.jpg')
15. figure(1)
8. # Convert to HSV 16. io.imshow(img_rgb)
9. img_hsv = color.rgb2hsv(img)
• Output
Example 2: RGB to XYZ and Vice Versa

1. # import libraries
2. from skimage import io 9. # Convert back to RGB
3. from skimage import color 10. img_rgb = color.xyz2rgb(img_xyz)
4. from skimage import data
11. # Show oth figures
12. figure(0)
5. # Read image 13. io.imshow(img_xyz)
6. img = io.imread('im5.jpg')
14. figure(1)
7. # Convert to XYZ 15. io.imshow(img_rgb)
8. img_xyz = color.rgb2xyz(img)
• Hue, Saturation, and Value (HSV) is a color model that is often used in
place of the RGB color model in graphics and paint programs. In using
this color model, a color is specified then white or black is added to
easily make color adjustments. HSV may also be called HSB (short for
hue, saturation and brightness).
• XYZ coordinates that are outside the locus of colors mapped by
the color matching experiments that led to the creation of the XYZ
color space are called imaginary colors. XYZ coordinates that are
inside the locus of colors mapped by the color matching experiments
are called real colors.
• Xyz is an additive color space based on how the eye intereprets
stimulus from light. Unlike other additive rgb like Rgb, Xyz is a purely
mathmatical space and the primary components are "imaginary",
meaning you can't create the represented color in the physical by
shining any sort of lights representing x, y, and z.
RGB to LAB and Vice Versa
• For the LAB color, L* indicates lightness, a* is the red/green
coordinate, and b* is the yellow/blue coordinate.
RGB to LAB and Vice Versa
• # import libraries
• from skimage import io • # Convert back to RGB
• from skimage import color • img_rgb = color.lab2rgb(img_lab)
• from skimage import data
• # Show oth figures
• figure(0)
• # Read image • io.imshow(img_lab)
• img = io.imread('im2.jpg')
• figure(1)
• # Convert to LAB • io.imshow(img_rgb)
• img_lab = color.rgb2lab(img)
Output
Example
1. from skimage import exposure 7. figure(0)
2. from skimage import io 8. io.imshow(gamma_corrected1)
3. from skimage import * 9. figure(1)
4. img = io.imread('ceci.jpg') 10. io.imshow(gamma_corrected2)
5. gamma_corrected1 =
exposure.adjust_gamma(img,
0.5)
6. gamma_corrected2 =
exposure.adjust_gamma(img, 5)
Output
Advanced image processing using openCV
• Advanced computer vision image processing is pursued using openCV.
OpenCV can examine the following concepts:
• Blending two images
• Changing the contrast and brightness of an image
• Adding text to images
• Smoothing images
• Changing the shape of images
• Effecting image thresholding
• Calculating gradients to detect edges
• Performing histogram equalization
Terms and definitions
• cv2.addWeighted(): This function blends the two images.
• alpha and beta parameters indicate the transparency in both images.
• gamma is a scalar, which is added to formulas to transform images
more effectively. In general, gamma is zero.
• cv2.imshow(): Similar to skimage.io.imshow(), cv2. imshow() helps to
display the image in a new window.
• cv2.waitKey(): waitKey() is used so that the window displaying the
output remains until we click Close or press Escape. If we do not
include this function after cv2.imshow(), the images are not displayed.
• cv2.DestroyAllWindows(): After we have clicked Close or pressed
Escape, this function destroys all the windows that have been opened
and saved in the memory.
Changing contrast and brightness
• To change contrast and brightness in an image, we should have an
understanding of what these two terms mean:

• Contrast: Contrast is the difference between maximum and minimum pixel


intensity.
• Brightness: Brightness refers to the lightness or darkness of an image. To
make an image brighter, we add a constant number to all the pixels present in
it.
Example
1. # import required packages 10. bright = 2
2. import cv2
3. import numpy as np 11. # Change the contrast and bightness
12. for y in range(image.shape[0]):
4. # Read image 13. for x in range(image.shape[1]):
5. image = cv2.imread('im2.jpg') 14. for c in range(image.shape[2]):
15. new_image[y,x,c] =
6. # Create a dummy image that stores np.clip(contrast*image[y,x,c] +
different contrast and brightness 16. bright, 0, 255)
7. new_image = np.zeros(image.shape, 17. figure(0)
image.dtype) 18. io.imshow(image)
19. figure(1)
8. # Brightness and contrast parameters 20. io.imshow(new_image)
9. contrast = 3.0
Output
• In this code, we did not use any cv2functions to change the brightness
or contrast. We used the numpy library and a slicing concept to
change the parameters. The first thing we did was define the
parameters. We gave contrast a value of 3 and brightness a value of 2.
The first forloop gave the image width, the second gave the image
height, and the third gave the image channels. Therefore, the first
loop runs width a number of times, the second loop runs height a
number of times, and the last loop runs the number of color channels
a number of times. If the RGB image is there, then loop runs three
times for the three channels.
• np.clip() limits the values in a particular range. In the previous code,
the range is 0 to 255, which is nothing but the pixel values for each
• channel. So, a formula is derived:
• (Specific pixel value × Contrast) + Brightness.
• Using the this formula, we can change each and every pixel value, and
np.clip() makes sure the output value doesn’t go beyond 0 to 255.
• Hence, the loops traverse through each and every pixel, for each and
every channel, and does the transformation

You might also like