89 lines
2.9 KiB
Python
89 lines
2.9 KiB
Python
from math import floor
|
|
import cv2
|
|
import numpy as np
|
|
import matplotlib.pyplot as plt
|
|
import tkinter as tk
|
|
|
|
visina_male_slike = 240
|
|
|
|
sirina_male_slike = 320
|
|
|
|
mali_kvadrat = 0.3
|
|
|
|
def doloci_barvo_koze(slika, levo_zgoraj, desno_spodaj):
|
|
roi = slika[levo_zgoraj[1]:desno_spodaj[1], levo_zgoraj[0]:desno_spodaj[0]]
|
|
spodnja_meja_koze = np.array([np.min(roi[:,:,0]), np.min(roi[:,:,1]), np.min(roi[:,:,2])])
|
|
zgornja_meja_koze = np.array([np.max(roi[:,:,0]), np.max(roi[:,:,1]), np.max(roi[:,:,2])])
|
|
return (spodnja_meja_koze,zgornja_meja_koze)
|
|
|
|
def zmanjsaj_sliko(slika):
|
|
return cv2.resize(slika,(sirina_male_slike, visina_male_slike))
|
|
|
|
def obdelaj_sliko(slika, okno_sirina, okno_visina, barva_koze_spodaj, barva_koze_zgoraj):
|
|
height, width = slika.shape[:2]
|
|
heightRep = floor( 1 / okno_visina )
|
|
subImgH = floor( height * okno_visina)
|
|
widthRep = floor( 1 / okno_sirina )
|
|
subImgW = floor (width * okno_sirina)
|
|
|
|
maxPoint1 = 0
|
|
maxPoint2 = 0
|
|
maxRatio = 0.0
|
|
|
|
for x in range(heightRep):
|
|
for y in range(widthRep):
|
|
point1 = y * subImgW
|
|
point2 = x * subImgH
|
|
subImg = slika[point2 : point2 + subImgH-1, point1: point1 + subImgW-1]
|
|
ratio = prestej_piksle_z_barvo_koze(subImg, barva_koze_spodaj, barva_koze_zgoraj)
|
|
ratio = round(ratio, 2)
|
|
if ratio > maxRatio:
|
|
maxRatio = ratio
|
|
maxPoint1 = point1
|
|
maxPoint2 = point2
|
|
|
|
maxRatio = round(maxRatio, 2)
|
|
slika = cv2.rectangle(slika, (maxPoint1, maxPoint2), (maxPoint1 + subImgW, maxPoint2 + subImgH), ( 0, 255, 0 ), 1 )
|
|
slika = cv2.putText(slika, str(maxRatio), (maxPoint1, maxPoint2+subImgH), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 0, 0), 2, cv2.LINE_AA)
|
|
return slika
|
|
|
|
def prestej_piksle_z_barvo_koze(podslika, barva_koze_spodaj, barva_koze_zgoraj):
|
|
mask = cv2.inRange(podslika, barva_koze_spodaj, barva_koze_zgoraj)
|
|
white_pixels = cv2.countNonZero(mask)
|
|
return (white_pixels / mask.size)
|
|
|
|
|
|
cap = cv2.VideoCapture(0)
|
|
if cap.isOpened() == False:
|
|
print("Ne morem odpreti kamere")
|
|
|
|
for x in range(30):
|
|
ret, frame = cap.read()
|
|
if ret == True:
|
|
pass
|
|
else:
|
|
pass
|
|
|
|
ret, frame = cap.read()
|
|
if ret == True:
|
|
r = cv2.selectROI("select the area", frame)
|
|
cv2.destroyAllWindows()
|
|
levo_zgoraj = ( r[0], r[1] )
|
|
desno_spodaj = ( r[0] + r[2], r[1] + r[3] )
|
|
limitLow, limitHigh = doloci_barvo_koze(frame, levo_zgoraj, desno_spodaj)
|
|
|
|
cv2.namedWindow("Kamera")
|
|
while True:
|
|
ret, frame = cap.read()
|
|
if ret == True:
|
|
frame = cv2.flip(frame,1)
|
|
#smallImg = zmanjsaj_sliko(frame)
|
|
obdelanaSlika = obdelaj_sliko(frame, mali_kvadrat, mali_kvadrat, limitLow, limitHigh)
|
|
imgDraw = np.zeros(obdelanaSlika.shape, np.uint8)
|
|
cv2.imshow("Kamera",obdelanaSlika | imgDraw)
|
|
if cv2.waitKey(10) & 0xFF == ord('q'):
|
|
break
|
|
else:
|
|
break
|
|
cap.release()
|
|
cv2.destroyAllWindows() |