-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathColor Spaces.py
More file actions
67 lines (51 loc) · 1.83 KB
/
Color Spaces.py
File metadata and controls
67 lines (51 loc) · 1.83 KB
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import cv2 as cv
import numpy as np
import matplotlib.pyplot as plt
scale = 0.1
img = cv.imread("large_image.png")
img = cv.resize(img, (int(img.shape[1] * scale), int(img.shape[0] * scale)), interpolation=cv.INTER_AREA)
cv.imshow("Original Image", img)
# The default way of reading images in OpenCV is in BGR
# BGR to GrayScale
gray = cv.cvtColor(img, cv.COLOR_BGR2GRAY)
cv.imshow("b&w Image", gray)
# BGR to HSV (Hue Saturation Value)
hsv = cv.cvtColor(img, cv.COLOR_BGR2HSV)
cv.imshow("HSV Image", hsv)
# BGR to L*a*b
lab = cv.cvtColor(img, cv.COLOR_BGR2LAB)
cv.imshow("LAB Image", lab)
# BGR to RGB
rgb = cv.cvtColor(img, cv.COLOR_BGR2RGB)
cv.imshow("RGB Image", rgb)
# HSV to BGR
hsv_bgr = cv.cvtColor(hsv, cv.COLOR_HSV2BGR)
cv.imshow("HSV to BGR", hsv_bgr)
cv.waitKey(0)
cv.destroyAllWindows()
# Now we'll split the Image in their corresponding blue, green and red components
cv.imshow("Original Image", img)
b, g, r = cv.split(img) # This gives us 3 numpy arrays that have the intensities
# of Blue, Green and Red respectively
cv.imshow("Blue", b)
cv.imshow("Green", g)
cv.imshow("Red", r)
cv.waitKey(0)
cv.destroyAllWindows()
# You'll see that all of these images are actually in b&w. And it's fair since their respective
# numpy arrays only contain the intensity values of their own individual colour
print(img.shape)
print(b.shape)
print(g.shape)
print(r.shape)
# Let's try and visualize these intensity matrices in their own respective colours
# making a blank black picure
blank = np.zeros(img.shape[:2], dtype="uint8")
cv.imshow("Original Image", img)
blue = cv.merge([b, blank,blank])
green = cv.merge([blank, g, blank])
red = cv.merge([blank, blank, r])
cv.imshow("Blue Image", blue)
cv.imshow("Green Image", green)
cv.imshow("Red Image", red)
cv.waitKey(0)