Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
An even stupider way to detect Red Bulls... in an empty fridge. It's like, extremely stupid.
import numpy as np
import matplotlib.pyplot as plt
from skimage import io
import skimage
img = io.imread('./test1.png')
img_gray = io.imread('./test1.png', as_gray=True)
window = 0.15
max = img_gray.max()
min = max - window
if min < 0:
min = 0
print(max)
print(min)
w = img.shape[1]
h = img.shape[0]
minx = w
miny = h
maxx = 0
maxy = 0
print(maxx)
print(maxy)
plt.imshow(img)
plt.colorbar()
plt.show()
for x in range(0, w):
for y in range(0, h):
#img[y, x, 0] = 0
#img[y, x, 1] = 0
if img_gray[y, x] >= min and img_gray[y, x] <= max:
if x < minx:
minx = x
if x > maxx:
maxx = x
if y < miny:
miny = y
if y > maxy:
maxy = y
cropped = img[miny:maxy, minx:maxx]
plt.imshow(cropped)
plt.colorbar()
plt.show()
!pip install colormath
import colormath.color_objects
import colormath.color_conversions
import colormath.color_diff
from colormath.color_objects import sRGBColor, LabColor
from colormath.color_conversions import convert_color
from colormath.color_diff import delta_e_cie2000
# Can Color
color1_rgb = sRGBColor(20/255, 36/255, 89/255);
tr = 20
tg = 36
tb = 100
canc = np.array([tr/255,tg/255,tb/255])
can = skimage.color.rgb2hsv(canc)
ww = cropped.shape[1]
hh = cropped.shape[0]
blueness = np.zeros((hh, ww, 3))
blueness.shape
import math
min = 1000
max = 0
for x in range(0, ww):
for y in range(0, hh):
# Img Color
r = cropped[y, x, 0]#/255
g = cropped[y, x, 1]#/255
b = cropped[y, x, 2]#/255
#color2_rgb = sRGBColor(r, g, b);
# Convert from RGB to Lab Color Space
#color1_lab = convert_color(color1_rgb, LabColor);
#color2_lab = convert_color(color2_rgb, LabColor);
# Find the color difference
#delta_e = delta_e_cie2000(color1_lab, color2_lab);
c1rgb = np.array([r/255,g/255,b/255])
c1 = skimage.color.rgb2hsv(c1rgb)
d = np.linalg.norm(c1 - can)
# d = math.sqrt((tr-r)**2 + (tg-g)**2 + (tb-b)**2)
if d < min:
min = d
if d > max:
max = d
blueness[y, x, 0] = 1 - d
blueness[y, x, 1] = 1 - d
blueness[y, x, 2] = 1 - d
print(min)
print(max)
plt.imshow(blueness)
plt.colorbar()
plt.show()
cansq = np.array([tr/255,tg/255,tb/255]) * np.ones([50,50,3])
plt.subplot(131)
plt.title('can')
plt.imshow(cansq)
cx = 0
cy = 0
wsum = 0
maxblue = blueness.max()
print(maxblue)
minblue = maxblue - 0.02
for x in range(0, ww):
for y in range(0, hh):
if (blueness[y, x, 0] > minblue):
wsum = wsum + blueness[y, x, 0]
cx = cx + blueness[y, x, 0] * x
cy = cy + blueness[y, x, 0] * y
cx = cx/wsum
cy = cy/wsum
icx = int(cx)
icy = int(cy)
blueness[icy-2:icy+2, icx-2:icx+2, 0] = 1
blueness[icy-2:icy+2, icx-2:icx+2, 1] = 0
blueness[icy-2:icy+2, icx-2:icx+2, 2] = 0
plt.imshow(blueness)
plt.colorbar()
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment