-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpixelProcessing.py
More file actions
61 lines (58 loc) · 1.43 KB
/
pixelProcessing.py
File metadata and controls
61 lines (58 loc) · 1.43 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
import cv2 as cv
import numpy as np
#Takes a list of pixels and a BGR image and returns the average
# RGB pixel values
def avgPixels(pixels, img):
totalB = 0
totalG = 0
totalR = 0
for pixel in pixels:
x = pixel[0]
y = pixel[1]
b,g,r = img[x,y,:]
totalB += b
totalG += g
totalR += r
if len(pixels) != 0:
totalB /= len(pixels)
totalG /= len(pixels)
totalR /= len(pixels)
return int(totalR + 0.5), int(totalG + 0.5), int(totalB + 0.5)
#Takes a list of pixels and a BGR image and returns the average
# HSV pixel values
def avgPixelsHSV(pixels, img):
workingImg = cv.cvtColor(img, cv.COLOR_BGR2HSV)
totalH = 0
totalS = 0
totalV = 0
for pixel in pixels:
x = pixel[0]
y = pixel[1]
h, s, v = workingImg[x,y,:]
totalH += h
totalS += s
totalV += v
if len(pixels) != 0:
totalH /= len(pixels)
totalS /= len(pixels)
totalV /= len(pixels)
return totalH, totalS, totalV
#Takes a list of pixels and a BGR image and returns the average
# Lab pixel values
def avgPixelsLAB(pixels, img):
workingImg = cv.cvtColor(img, cv.COLOR_BGR2Lab)
totalL = 0
totalA = 0
totalB = 0
for pixel in pixels:
x = pixel[0]
y = pixel[1]
l, a, b = workingImg[x,y,:]
totalL += l
totalA += a
totalB += b
if len(pixels) != 0:
totalL /= len(pixels)
totalA /= len(pixels)
totalB /= len(pixels)
return int(totalL + 0.5), int(totalA + 0.5), int(totalB + 0.5)