In [2]:
from math import sqrt, sin, cos, atan2, pi
from matplotlib import pyplot as plt
import numpy as np
import cv2
from colorsys import rgb_to_hsv

img = cv2.imread("weerstand2.jpg", 1)  # 1=colour image
detect = img.copy()

lines = 200
points = 50

p1 = (205, 410)
p2 = (290, 325)

cv2.line(detect, p1, p2, (0,0,255), 10)

wdx = p1[0] - p2[0]
wdy = p1[1] - p2[1]

klen = sqrt(wdx * wdx + wdy * wdy) / 8
khoek = atan2(wdy, wdx) + pi / 2

kdx = klen * cos(khoek)
kdy = klen * sin(khoek)

wxs = np.linspace(p1[0], p2[0], num=lines)
wys = np.linspace(p1[1], p2[1], num=lines)

hsv = np.zeros((lines, 3), dtype=np.float64)
for i in range(lines):
    p3 = (round(wxs[i] + kdx), round(wys[i] + kdy))
    p4 = (round(wxs[i] - kdx), round(wys[i] - kdy))
    cv2.line(detect, p3, p4, (0,255,255), 1)
    kxs = np.linspace(p3[0], p4[0], num=points)
    kys = np.linspace(p3[1], p4[1], num=points)
    mean = np.zeros(3, dtype=np.float64)
    for j in range(points):
        kx = round(kxs[j])
        ky = round(kys[j])
        mean += img[ky, kx]
    mean /= points
    hsv[i] = rgb_to_hsv(mean[2], mean[1], mean[0])
    if hsv[i, 0] < 0.2:
        hsv[i, 0] += 1  # hue correction
    #print("kleur %s = BGR %s = Hue %s" % (i, mean, round(hsv[i, 0], 3)))

dpi = 96
w = img.shape[1]
h = img.shape[0]
plt.figure(figsize=(2 * w/dpi,h/dpi),dpi=dpi)

plt.subplot(121)
plt.imshow(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))
plt.title("Weerstand 1 origineel")

plt.subplot(122)
plt.imshow(cv2.cvtColor(detect, cv2.COLOR_BGR2RGB))
plt.title("Weerstand 1 detectie")

plt.show()
In [3]:
plt.figure(figsize=(15, 10))
plt.title("Gemiddelde HSV-waardes haaks op de as van de weerstand")
plt.xlabel("Monsterlijn")
#plt.ylabel("Tint, verzadiging, intensiteit")
#plt.hlines(0.587, 0, 200, "r", "--")
#plt.hlines((0.97, 1.03, 0.72, 0.749, 1.015), (0, 50, 87, 115, 145), (50, 87, 115, 145, 180), "r", "--")
plt.plot(np.arange(0, lines), hsv[:, 0])
plt.plot(np.arange(0, lines), hsv[:, 1])
plt.plot(np.arange(0, lines), hsv[:, 2] / 255)
plt.legend(("Tint", "Verzadiging", "Intensiteit"))
plt.show()
In [17]:
plt.figure(figsize=(20, 10))

plt.subplot(131)
_ = plt.hist(hsv[:,0], bins=12, range=(0.0, 1.2))
plt.title("Beige weerstand - Tint")

plt.subplot(132)
_ = plt.hist(hsv[:,1], bins=12, range=(0.0, 1.2))
plt.title("Beige weerstand - Verzadiging")

plt.subplot(133)
_ = plt.hist(hsv[:,2] / 255, bins=12, range=(0.0, 1.2))
plt.title("Beige weerstand - Intensiteit")

plt.show()
In [ ]: