In [1]:
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("weerstand1.jpg", 1)  # 1=colour image
detect = img.copy()

lines = 200
points = 50

p1 = (278, 395)
p2 = (290, 210)

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)

hue = np.zeros(lines)
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
    hue[i] = rgb_to_hsv(mean[2], mean[1], mean[0])[0]
    if hue[i] < 0.1:
        hue[i] += 1
    #print("kleur %s = BGR %s = Hue %s" % (i, mean, round(hue[i], 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 [2]:
plt.figure(figsize=(15, 10))
plt.title("Gemiddelde tint haaks op de as van de weerstand")
plt.xlabel("Monsterlijn")
plt.ylabel("Tint")
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), "g", "--")
plt.plot(np.arange(0, lines), hue)
plt.show()