consolidate all repos to one for archive

This commit is contained in:
2025-01-28 13:46:42 +01:00
commit a6610fbc7a
5350 changed files with 2705721 additions and 0 deletions

Binary file not shown.

After

Width:  |  Height:  |  Size: 463 KiB

View File

@@ -0,0 +1,89 @@
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()

Binary file not shown.

After

Width:  |  Height:  |  Size: 463 KiB

View File

@@ -0,0 +1,118 @@
import numpy as np
import cv2
def my_roberts(slika):
kernalX = np.array([[1,0],[0,-1]])
kernalY = np.array([[0,1],[-1,0]])
slika_robov = np.zeros(slika.shape)
height, width = slika.shape
for y in range(0, height-2):
for x in range(0, width-2):
subImg = slika[y:y+2, x:x+2]
kernalResultX = np.sum(kernalX * subImg)
kernalResultY = np.sum(kernalY * subImg)
kernalResult = np.absolute( kernalResultX ) + np.absolute( kernalResultY )
slika_robov[y, x] = kernalResult
slika_robov = np.uint8(slika_robov)
return slika_robov
def my_prewitt(slika):
kernalX = np.array([[-1, -1, -1], [0, 0, 0], [1, 1, 1]])
kernalY = np.array([[-1, 0, 1], [-1, 0, 1], [-1, 0, 1]])
slika_robov = np.zeros(slika.shape)
height, width = slika.shape
for y in range(1, height-2):
for x in range(1, width-2):
subImg = slika[y-1:y+2, x-1:x+2]
kernalResultX = np.sum(kernalX * subImg)
kernalResultY = np.sum(kernalY * subImg)
kernalResult = np.absolute( kernalResultX ) + np.absolute( kernalResultY )
slika_robov[y, x] = np.absolute(kernalResult)
slika_robov = np.uint8(slika_robov)
return slika_robov
def my_sobel(slika):
kernalX = np.array([[-1, -3, -1], [0, 0, 0], [1, 3, 1]])
kernalY = np.array([[-1, 0, 1], [-3, 0, 3], [-1, 0, 1]])
slika_robov = np.zeros(slika.shape)
height, width = slika.shape
for y in range(1, height-2):
for x in range(1, width-2):
subImg = slika[y-1:y+2, x-1:x+2]
kernalResultX = np.sum(kernalX * subImg)
kernalResultY = np.sum(kernalY * subImg)
kernalResult = np.absolute( kernalResultX ) + np.absolute( kernalResultY )
slika_robov[y, x] = np.absolute(kernalResult)
slika_robov = np.uint8(slika_robov)
return slika_robov
def canny(slika):
slika_robov = cv2.Canny(slika, 0, 0)
return slika_robov
def spremeni_kontrast(slika, alfa, beta):
return cv2.convertScaleAbs(slika, alpha=alfa, beta=beta)
img = cv2.imread('lenna.png', 0)
cv2.imshow('lenna', img)
#
#imgRoberts = my_roberts(img)
#cv2.imshow('lenna_roberts', imgRoberts)
#
#imgPrewitt = my_prewitt(img)
#cv2.imshow('lenna_prewitt', imgPrewitt)
#
#imgSobel = my_sobel(img)
#cv2.imshow('lenna_sobel', imgSobel)
img2 = spremeni_kontrast(img, 1.5, 0)
cv2.imshow('lenna2i', img2)
cv2.imshow('lenna2', my_sobel(img2))
img3 = spremeni_kontrast(img, 0.5, 1)
cv2.imshow('lenna3i', img3)
cv2.imshow('lenna3', my_sobel(img3))
windowName = 'lenna_canny'
val1 = 0
val2 = 0
def on_change1(val):
global val1
val1 = val
imgCanny = cv2.Canny(img, val1, val2)
cv2.imshow(windowName, imgCanny)
def on_change2(val):
global val2
val2 = val
imgCanny = cv2.Canny(img, val1, val2)
cv2.imshow(windowName, imgCanny)
imgCanny = canny(img)
cv2.imshow(windowName, imgCanny)
cv2.createTrackbar('threshold1', windowName, 0, 255, on_change1)
cv2.createTrackbar('threshold2', windowName, 0, 255, on_change2)
cv2.waitKey(0)
cv2.destroyAllWindows()

Binary file not shown.

After

Width:  |  Height:  |  Size: 146 KiB

View File

@@ -0,0 +1,201 @@
import numpy as np
import cv2
import random
def findClosestCentroids3(img, centroids):
closestCentroi = np.zeros((img.shape[0],img.shape[1]), np.uint8)
klusters = centroids.shape[0]
for i in range(img.shape[0]):
for j in range(img.shape[1]):
dist = np.zeros(klusters, np.float32)
for l in range(klusters):
# calculate the distance between the pixel and the centroid
dist[l] = np.sqrt(
((img[i][j][0] - centroids[l][0]) ** 2)+
((img[i][j][1] - centroids[l][1]) ** 2)+
((img[i][j][2] - centroids[l][2]) ** 2))
#assign the pixel to the cluster with the nearest centroid human readable
#closestCentroi[i, j] = np.argmin(dist) * 255 / (klusters - 1)
closestCentroi[i, j] = np.argmin(dist)
return closestCentroi
def calculateNewCenter3(img, centroids, closestCentroi):
newCentroids = np.zeros(centroids.shape, np.float32)
klusters = centroids.shape[0]
centroidsCount = np.zeros(klusters, np.float32)
for i in range(img.shape[0]):
for j in range(img.shape[1]):
newCentroids[closestCentroi[i][j]] += img[i][j]
centroidsCount[closestCentroi[i][j]] += 1
for i in range(klusters):
if centroidsCount[i] == 0:
centroidsCount[i] = 1
newCentroids[i] /= centroidsCount[i]
return newCentroids
def kmeans3(img, centroids, iterations):
for i in range(iterations):
print("Iteration: ", i)
closestCentroi = findClosestCentroids3(img, centroids)
# cv2.imshow('closestCentroi', closestCentroi)
# cv2.waitKey(0)
# cv2.destroyAllWindows()
centroids = calculateNewCenter3(img, centroids, closestCentroi)
commpressedImg = np.zeros(img.shape, np.uint8)
for i in range(img.shape[0]):
for j in range(img.shape[1]):
commpressedImg[i][j] = centroids[closestCentroi[i][j]]
return commpressedImg
def findClosestCentroids5(img, centroids, locations):
closestCentroi = np.zeros((img.shape[0],img.shape[1]), np.uint8)
klusters = centroids.shape[0]
for i in range(img.shape[0]):
for j in range(img.shape[1]):
dist = np.zeros(klusters, np.float32)
for l in range(klusters):
# calculate the distance between the pixel and the centroid
dist[l] = np.sqrt(
((img[i][j][0] - centroids[l][0]) ** 2)+
((img[i][j][1] - centroids[l][1]) ** 2)+
((img[i][j][2] - centroids[l][2]) ** 2)+
((i - locations[l][0]) ** 2)+
((j - locations[l][1]) ** 2))
# assign the pixel to the cluster with the nearest centroid human readable
#closestCentroi[i, j] = np.argmin(dist) * 255 / (klusters - 1)
closestCentroi[i, j] = np.argmin(dist)
return closestCentroi
def calculateNewCenter5(img, centroids, locations, closestCentroi):
newCentroids = np.zeros(centroids.shape, np.float32)
newLocations = np.zeros(locations.shape, np.float32)
klusters = centroids.shape[0]
centroidsCount = np.zeros(klusters, np.float32)
for i in range(img.shape[0]):
for j in range(img.shape[1]):
newCentroids[closestCentroi[i][j]] += img[i][j]
newLocations[closestCentroi[i][j]] += [i,j]
centroidsCount[closestCentroi[i][j]] += 1
for i in range(klusters):
if centroidsCount[i] == 0:
centroidsCount[i] = 1
newCentroids[i] /= centroidsCount[i]
newLocations[i] /= centroidsCount[i]
return newCentroids, newLocations
def kmeans5(img, centroids, locations, iterations):
for i in range(iterations):
print("Iteration: ", i)
closestCentroi = findClosestCentroids5(img, centroids, locations)
centroids, locations = calculateNewCenter5(img, centroids, locations, closestCentroi)
commpressedImg = np.zeros(img.shape, np.uint8)
for i in range(img.shape[0]):
for j in range(img.shape[1]):
commpressedImg[i][j] = centroids[closestCentroi[i][j]]
return commpressedImg
def kmeans(img, centroids, locations, iterations, dim):
if dim == 5:
commpressedImg = kmeans5(img, centroids, locations, iterations)
else:
commpressedImg = kmeans3(img, centroids, iterations)
return commpressedImg
def click_event(event, x, y, flags, params):
global points
global locations
if event == cv2.EVENT_LBUTTONDOWN:
print(f'({y},{x})')
points = np.append(points, [img[y,x]] , axis=0)
locations = np.append(locations, [[y,x]] , axis=0)
def selectPointsM(img):
cv2.namedWindow('Select color')
cv2.setMouseCallback('Select color', click_event)
while True:
cv2.imshow('Select color', img)
if cv2.waitKey(1) & 0xFF == 27:
break
cv2.destroyAllWindows()
T = 50
def selectPointsR(img, stDimenzij):
global points
global locations
global T
numOfPoints = input('Vnesi stevilo tock:')
i = 0
while i < int(numOfPoints):
x = random.randint(0, img.shape[0]-1)
y = random.randint(0, img.shape[1]-1)
point = img[x,y]
location = [x,y]
add = True
if stDimenzij == 3:
for j in range(points.shape[0]):
dist = np.sqrt(
((points[j][0] - point[0]) ** 2)+
((points[j][1] - point[1]) ** 2)+
((points[j][2] - point[2]) ** 2))
if dist < T:
add = False
break
else:
for j in range(points.shape[0]):
dist = np.sqrt(
((points[j][0] - point[0]) ** 2)+
((points[j][1] - point[1]) ** 2)+
((points[j][2] - point[2]) ** 2)+
((locations[j][0] - location[0]) ** 2)+
((locations[j][1] - location[1]) ** 2))
if dist < T:
add = False
break
if add:
points = np.append(points, [point] , axis=0)
locations = np.append(locations, [location] , axis=0)
i += 1
# read the input image
img = cv2.imread('veg.jpg' , 1)
points = np.empty((0, 3), dtype=np.float32)
locations = np.empty((0, 2), dtype=np.float32)
stDimenzij = input('Vnesi stevilo dimenzij 3 ali 5:')
stIteracij = input('Vnesi stevilo iteracij:')
selectType = input('Vnesi 1 za rocno izbiro, 2 za random izbiro:')
if selectType == '1':
selectPointsM(img)
else:
selectPointsR(img, int(stDimenzij))
commpressedImg = kmeans(img, points, locations, int(stIteracij), int(stDimenzij))
cv2.imshow('Original image',img)
cv2.imshow('Compressed image',commpressedImg)
cv2.waitKey(0)
cv2.destroyAllWindows()

View File

@@ -0,0 +1,240 @@
import cv2
import numpy as np
locations = np.empty((0,2), dtype=np.int32)
def click_event(event, x, y, flags, params):
global locations
if event == cv2.EVENT_LBUTTONDOWN:
print(img[y][x])
locations = np.append(locations, [[y,x]] , axis=0)
def selectPointsM(img):
global locations
cv2.namedWindow('Select color')
cv2.setMouseCallback('Select color', click_event)
while True:
cv2.imshow('Select color', img)
if cv2.waitKey(1) & 0xFF == 27:
break
cv2.destroyAllWindows()
return locations
def MeanShift(src, window, criteria):
for _ in range(criteria[1]):
ret, window = Iteration(src, window, criteria[2])
if ret:
return (True, window)
return (False, window)
def Iteration(src, window, criteria):
if window[0] < 0:
window = (0, window[1], window[2], window[3])
if window[1] < 0:
window = (window[0], 0, window[2], window[3])
roi = src[window[1]:window[1]+window[3], window[0]:window[0]+window[2]]
#iteracija 1
# x = 0
# y = 0
# num = 0
# for i in range(roi.shape[0]):
# for j in range(roi.shape[1]):
# if roi[i][j] > 0:
# x += j
# y += i
# num += 1
#iteracija 2
# nonzero_indices = np.nonzero(roi)
# nonzero_values = roi[nonzero_indices]
# x = np.sum(nonzero_indices[1])
# y = np.sum(nonzero_indices[0])
# num = np.count_nonzero(nonzero_values)
#iteracija 3
mom = cv2.moments(roi)
x = 0
y = 0
if mom['m00'] != 0:
x = int(mom['m10'] / mom['m00'])
y = int(mom['m01'] / mom['m00'])
x = int(x - window[2] / 2)
y = int(y - window[3] / 2)
window = (window[0] + x, window[1] + y, window[2], window[3])
if x < criteria and y < criteria:
return (True, window)
return (False, window)
def CamShift(src, window, criteria):
ret = False
for _ in range(criteria[1]):
ret, window = Iteration(src, window, criteria[2])
window = changeSize(src, window)
return (ret, window)
def changeSize(src, window):
roi = src[window[1]:window[1]+window[3], window[0]:window[0]+window[2]]
mom = cv2.moments(roi)
ratio = window[2] / window[3]
if mom['m00'] != 0:
w = 2 * np.sqrt(mom['m00'] / 256)
h = w / ratio
window = (window[0], window[1], int(w), int(h))
if window[0] < 0:
window = (0, window[1], window[2], window[3])
if window[1] < 0:
window = (window[0], 0, window[2], window[3])
if window[0] + window[2] > src.shape[1]:
window = (src.shape[1] - window[2], window[1], window[2], window[3])
if window[1] + window[3] > src.shape[0]:
window = (window[0], src.shape[0] - window[3], window[2], window[3])
if window[2] < 10:
window = (window[0], window[1], 10, window[3])
if window[3] < 10:
window = (window[0], window[1], window[2], 10)
return window
def selectObjectH(frame, target_hist):
target_hist = target_hist.astype(np.float32)
cv2.normalize(target_hist, target_hist, 0, 1, cv2.NORM_MINMAX)
hsvImg = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
h_channel = hsvImg[:, :, 0]
#s_channel = hsvImg[:, :, 1]
#probabilityImg = np.zeros(shape=(hsvImg.shape[0], hsvImg.shape[1], 1) , dtype=np.float32)
# Stack the H and S channels
# hs_img = np.stack((h_channel, s_channel), axis=-1)
# for i in range(hs_img.shape[0]):
# for j in range(hs_img.shape[1]):
# probabilityImg[i][j] = target_hist[int(h_channel[i][j])]
# satProp = target_hist[int(hs_img[i][j][1])]
# probabilityImg[i][j] = hueProp * satProp
probabilityImg = target_hist[h_channel.astype(np.uint8)]
cv2.normalize(probabilityImg, probabilityImg, 0, 255, cv2.NORM_MINMAX)
probabilityImg = probabilityImg.astype(np.uint8)
mask = cv2.inRange(probabilityImg, 100, 255)
contour, _ = cv2.findContours(mask, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
maxContour = max(contour, key=cv2.contourArea)
x, y, w, h = cv2.boundingRect(maxContour)
return (x, y, w, h)
def selectObjectHS(frame, target_hist):
target_hist = target_hist.astype(np.float32)
cv2.normalize(target_hist, target_hist, 0, 1, cv2.NORM_MINMAX)
hsvImg = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
# probabilityImg = np.zeros(shape=(hsvImg.shape[0], hsvImg.shape[1], 1) , dtype=np.float32)
# for i in range(hsvImg.shape[0]):
# for j in range(hsvImg.shape[1]):
# hueProp = target_hist[int(hsvImg[i][j][0])][0]
# satProp = target_hist[int(hsvImg[i][j][1])][1]
# probabilityImg[i][j] = hueProp * satProp
hue_values = hsvImg[..., 0].astype(np.uint8)
sat_values = hsvImg[..., 1].astype(np.uint8)
hue_props = target_hist[hue_values.flatten(), 0].reshape(hsvImg.shape[:2])
sat_props = target_hist[sat_values.flatten(), 1].reshape(hsvImg.shape[:2])
probabilityImg = (hue_props * sat_props).astype(np.float32)
cv2.normalize(probabilityImg, probabilityImg, 0, 255, cv2.NORM_MINMAX)
probabilityImg = probabilityImg.astype(np.uint8)
mask = cv2.inRange(probabilityImg, 100, 255)
contour, _ = cv2.findContours(mask, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
maxContour = max(contour, key=cv2.contourArea)
x, y, w, h = cv2.boundingRect(maxContour)
return (x, y, w, h)
def selectObject(frame, target_hist):
if target_hist.shape[0] == 180:
return selectObjectH(frame, target_hist)
else:
return selectObjectHS(frame, target_hist)
def calcHistH(roi):
histogram = np.zeros(180, dtype=int)
for i in range(roi.shape[0]):
for j in range(roi.shape[1]):
histogram[roi[i][j][0]] += 1
return histogram
def calcHistHS(roi):
histogramH = np.zeros(256, dtype=int)
histogramS = np.zeros(256, dtype=int)
for i in range(roi.shape[0]):
for j in range(roi.shape[1]):
histogramH[roi[i][j][0]] += 1
histogramS[roi[i][j][1]] += 1
histogram = np.stack(arrays=(histogramH, histogramS), axis=-1)
return histogram
def calcHist(roi, numOfchannels=1):
if numOfchannels == 1:
return calcHistH(roi)
else:
return calcHistHS(roi)
def calcBackProjectH(frame, target_hist):
target_hist = target_hist.astype(np.float32)
cv2.normalize(target_hist, target_hist, 0, 1, cv2.NORM_MINMAX)
hsvImg = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
h_channel = hsvImg[:, :, 0]
probabilityImg = target_hist[h_channel.astype(np.uint8)]
cv2.normalize(probabilityImg, probabilityImg, 0, 255, cv2.NORM_MINMAX)
return probabilityImg.astype(np.uint8)
def calcBackProjectHS(frame, target_hist):
target_hist = target_hist.astype(np.float32)
cv2.normalize(target_hist, target_hist, 0, 1, cv2.NORM_MINMAX)
hsvImg = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
hue_values = hsvImg[..., 0].astype(np.uint8)
sat_values = hsvImg[..., 1].astype(np.uint8)
hue_props = target_hist[hue_values.flatten(), 0].reshape(hsvImg.shape[:2])
sat_props = target_hist[sat_values.flatten(), 1].reshape(hsvImg.shape[:2])
probabilityImg = (hue_props * sat_props).astype(np.float32)
cv2.normalize(probabilityImg, probabilityImg, 0, 255, cv2.NORM_MINMAX)
return probabilityImg.astype(np.uint8)
def calcBackProject(frame, target_hist):
if target_hist.shape[0] == 180:
return calcBackProjectH(frame, target_hist)
else:
return calcBackProjectHS(frame, target_hist)

View File

@@ -0,0 +1,122 @@
import cv2
import numpy as np
import matplotlib.pyplot as plt
from implement import MeanShift, CamShift, selectObject, calcHist
if True:
capture = cv2.VideoCapture(0)
flip = True
else:
capture = cv2.VideoCapture("colors_Trim.mp4")
flip = False
track_window = None
global xLZ
global yLZ
global xLast
global yLast
global frame
xLZ = 0
yLZ = 0
def clickOnImage(event, x, y, flags, param):
global frame, xLZ, yLZ, xLast, yLast
if event == cv2.EVENT_LBUTTONUP:
#print("EVENT_LBUTTONUP ({},{})".format(x,y))
xLZ = 0
yLZ = 0
if event == cv2.EVENT_LBUTTONDOWN:
#print("EVENT_LBUTTONDOWN ({},{})".format(x,y))
xLZ = x
yLZ = y
if event == cv2.EVENT_MOUSEMOVE:
#print("EVENT_MOUSEMOVE ({},{})".format(x,y))
xLast = x
yLast = y
cv2.namedWindow("Slika")
cv2.setMouseCallback("Slika", clickOnImage)
target_hist = None
while(1):
ret, frame = capture.read()
if ret == True:
if flip:
frame = cv2.flip(frame, 1)
frameCopy = frame.copy()
if xLZ != 0 or yLZ != 0:
cv2.rectangle(frameCopy,(xLZ,yLZ),(xLast,yLast),(0,255,0),2)
cv2.imshow('Slika',frameCopy)
if cv2.waitKey(100) & 0xFF == ord("q"):
miniFrame = frame[yLZ:yLast,xLZ:xLast]
miniFrame = cv2.cvtColor(miniFrame, cv2.COLOR_BGR2HSV)
target_hist = calcHist(miniFrame,2)
cv2.destroyAllWindows()
break
if target_hist is None:
print("No target selected")
exit(1)
track_window = selectObject(frame, target_hist)
cv2.rectangle(frame, (track_window[0], track_window[1]), (track_window[0]+track_window[2], track_window[1]+track_window[3]), (0, 255, 0), 2)
cv2.imshow("Frame", frame)
cv2.waitKey(0)
cv2.destroyAllWindows()
firstFrame = None
while True:
ret, frame = capture.read()
if not ret:
break
if flip:
frame = cv2.flip(frame, 1)
if firstFrame is None:
firstFrame = frame.copy()
frameCopy = frame.copy()
# prevFrame is not none
if firstFrame is not None:
diff = cv2.absdiff(firstFrame, frame)
gray = cv2.cvtColor(diff, cv2.COLOR_BGR2GRAY)
blur = cv2.GaussianBlur(gray, (5, 5), 0)
tres = cv2.threshold(blur, 25, 255, cv2.THRESH_BINARY)[1]
ret, track_window = CamShift(tres, track_window, (3, 10, 1))
cv2.rectangle(frameCopy, (track_window[0], track_window[1]), (track_window[0]+track_window[2], track_window[1]+track_window[3]), (0, 255, 0), 2)
cv2.imshow("Frame", frameCopy)
cv2.imshow("ALG", tres)
if cv2.waitKey(10) & 0xFF == ord("q"):
capture.release()
cv2.destroyAllWindows()
break
cv2.destroyAllWindows()

View File

@@ -0,0 +1,12 @@
import cv2
import numpy as np
img = np.zeros((3,3))
img[1,1] = 2
img[0,2] = 2
print(img)
mom = cv2.moments(img)
print (mom['m00'])
print (mom['m01'])
print (mom['m10'])

View File

@@ -0,0 +1,91 @@
import cv2
import numpy as np
events = [i for i in dir(cv2) if 'EVENT' in i]
from implement import MeanShift, CamShift
global xLZ
global yLZ
global xLast
global yLast
global frame
xLZ = 0
yLZ = 0
frame = None
def clickOnImage(event, x, y, flags, param):
global frame, xLZ, yLZ, xLast, yLast
if event == cv2.EVENT_LBUTTONUP:
#print("EVENT_LBUTTONUP ({},{})".format(x,y))
xLZ = 0
yLZ = 0
if event == cv2.EVENT_LBUTTONDOWN:
#print("EVENT_LBUTTONDOWN ({},{})".format(x,y))
xLZ = x
yLZ = y
if event == cv2.EVENT_MOUSEMOVE:
#print("EVENT_MOUSEMOVE ({},{})".format(x,y))
xLast = x
yLast = y
capture = cv2.VideoCapture(0)
cv2.namedWindow("Slika")
cv2.setMouseCallback("Slika", clickOnImage)
miniFrame = None
track_window = None
while(1):
ret, frame = capture.read()
if ret == True:
if xLZ != 0 or yLZ != 0:
cv2.rectangle(frame,(xLZ,yLZ),(xLast,yLast),(0,255,0),2)
cv2.imshow('Slika',frame)
if cv2.waitKey(100) & 0xFF == ord("q"):
cv2.destroyAllWindows()
break
if cv2.waitKey(100) & 0xFF == ord("r"):
miniFrame = frame[yLZ:yLast,xLZ:xLast]
track_window = (xLZ, yLZ, xLast-xLZ, yLast-yLZ)
cv2.imshow("Mini",miniFrame)
if miniFrame is not None:
roi = miniFrame
hsv_roi = cv2.cvtColor(roi, cv2.COLOR_BGR2HSV)
mask = cv2.inRange(hsv_roi, np.array((0., 60.,32.)), np.array((180.,255.,255.)))
roi_hist = cv2.calcHist([hsv_roi],[0],mask,[180],[0,180])
cv2.normalize(roi_hist,roi_hist,0,255,cv2.NORM_MINMAX)
term_crit = ( cv2.TERM_CRITERIA_EPS | cv2.TERM_CRITERIA_COUNT, 10, 1 )
while(1):
ret, frame = capture.read()
frame = cv2.flip(frame,1)
if ret == True:
hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
dst = cv2.calcBackProject([hsv],[0],roi_hist,[0,180],1)
dst = cv2.threshold(dst, 2, 255, cv2.THRESH_BINARY)[1]
cv2.imshow("dst",dst)
# apply meanshift to get the new location
#ret, track_window = MeanShift(dst, track_window, term_crit)
ret, track_window = CamShift(dst, track_window, term_crit)
# Draw it on image
x,y,w,h = track_window
img2 = cv2.rectangle(frame, (x,y), (x+w,y+h), 255,2)
cv2.imshow('img2',img2)
k = cv2.waitKey(30) & 0xff
if k == 27:
break
else:
break
cv2.destroyAllWindows()
capture.release()

View File

@@ -0,0 +1,129 @@
import cv2
import numpy as np
import matplotlib.pyplot as plt
from skimage.feature import hog
from sklearn.neighbors import KNeighborsClassifier
from sklearn.metrics import accuracy_score
from sklearn.model_selection import train_test_split
from sklearn.metrics import classification_report
from sklearn import svm
from sklearn import tree
import pickle
import os
def lbp(image):
height, width = image.shape[:2]
lbp_image = np.zeros_like(image, dtype=np.uint8)
for y in range(1, height-1):
for x in range(1, width-1):
center = image[y, x]
code = 0
# Compare the pixel values of the neighborhood with the center pixel
code |= (image[y-1, x-1] > center) << 7
code |= (image[y-1, x] > center) << 6
code |= (image[y-1, x+1] > center) << 5
code |= (image[y, x+1] > center) << 4
code |= (image[y+1, x+1] > center) << 3
code |= (image[y+1, x] > center) << 2
code |= (image[y+1, x-1] > center) << 1
code |= (image[y, x-1] > center) << 0
lbp_image[y, x] = code
return lbp_image
def load_data(pictures, marks):
pictures = np.load(pictures)
marks = np.load(marks)
return pictures, marks
def get_data(folder_path):
img_data = []
marks = []
i = 0
for root, dirs, files in os.walk(folder_path):
for file in files:
file_path = os.path.join(root, file)
if 'c' in file_path:
marks.append(1)
else:
marks.append(0)
img = cv2.imread(file_path)
img = cv2.resize(img, (100, 100))
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
lbpHist = cv2.calcHist([lbp(gray)], [0], None, [256], [0, 256])
lbpHist = lbpHist.astype(np.float64)
lbpHist = lbpHist.flatten()
hogFeatures = hog(gray, orientations=9, pixels_per_cell=(10, 10), cells_per_block=(2, 2), block_norm='L2-Hys')
features = np.concatenate((lbpHist, hogFeatures), axis=0)
img_data.append(features)
print(i)
i += 1
img_data = np.array(img_data)
marks = np.array(marks)
return img_data, marks
def save_data(img_data, marks, img_data_path, marks_data_path):
np.save(img_data_path, img_data)
np.save(marks_data_path, marks)
def train_and_save_model(img_data, marks):
#---------------------KNN-------------------
knn = KNeighborsClassifier(n_neighbors=5)
knn.fit(img_data, marks)
knnPickle = open('Models\\knn.bin', 'wb')
pickle.dump(knn, knnPickle)
knnPickle.close()
#------------------SVM----------------------
svmM = svm.SVC()
svmM.fit(img_data, marks)
svmPickle = open('Models\\svm.bin', 'wb')
pickle.dump(svmM, svmPickle)
svmPickle.close()
#------------------DT----------------------
dt = tree.DecisionTreeClassifier()
dt.fit(img_data, marks)
dtPickle = open('Models\\dt.bin', 'wb')
pickle.dump(dt, dtPickle)
dtPickle.close()
def load_model():
#---------------------KNN-------------------
knnPickle = open('Models\\knn.bin', 'rb')
knn = pickle.load(knnPickle)
knnPickle.close()
#------------------SVM----------------------
svmPickle = open('Models\\svm.bin', 'rb')
svm = pickle.load(svmPickle)
svmPickle.close()
#------------------DT----------------------
dtPickle = open('Models\\dt.bin', 'rb')
dt = pickle.load(dtPickle)
dtPickle.close()
return knn, svm, dt

View File

@@ -0,0 +1,54 @@
import cv2
import numpy as np
import matplotlib.pyplot as plt
from skimage.feature import hog
from sklearn.neighbors import KNeighborsClassifier
from sklearn.metrics import accuracy_score
from sklearn.model_selection import train_test_split
from sklearn.metrics import classification_report
from sklearn import svm
from sklearn import tree
import pickle
from implement import train_and_save_model, save_data, get_data, load_data, load_model
print("Test")
img_data_path = "Data\\test_img_data.npy"
marks_data_path = "Data\\test_marks.npy"
train_baza_path = "Baza\\Pet100"
print("Loading data")
if False:
img_data, marks = get_data(train_baza_path)
save_data(img_data, marks, img_data_path, marks_data_path)
else:
img_data, marks = load_data(img_data_path, marks_data_path)
print("Loding model")
knn, svm, dt = load_model()
print("Calculating predictions")
knn_pred = knn.predict(img_data)
svm_pred = svm.predict(img_data)
dt_pred = dt.predict(img_data)
predictions = []
for i in range(len(knn_pred)):
if knn_pred[i] == svm_pred[i] == dt_pred[i]:
predictions.append(knn_pred[i])
elif knn_pred[i] == svm_pred[i]:
predictions.append(knn_pred[i])
elif knn_pred[i] == dt_pred[i]:
predictions.append(knn_pred[i])
elif svm_pred[i] == dt_pred[i]:
predictions.append(svm_pred[i])
predictions = np.array(predictions)
print("KNN: ", accuracy_score(marks, knn_pred))
print("SVM: ", accuracy_score(marks, svm_pred))
print("DT: ", accuracy_score(marks, dt_pred))
print("Accuracy: ", accuracy_score(marks, predictions))
print("End")

View File

@@ -0,0 +1,31 @@
import cv2
import numpy as np
import matplotlib.pyplot as plt
from skimage.feature import hog
from sklearn.neighbors import KNeighborsClassifier
from sklearn.metrics import accuracy_score
from sklearn.model_selection import train_test_split
from sklearn.metrics import classification_report
from sklearn import svm
from sklearn import tree
import pickle
from implement import train_and_save_model, save_data, get_data, load_data
print("Train")
img_data_path = "Data\\train_img_data.npy"
marks_data_path = "Data\\train_marks.npy"
train_baza_path = "Baza\\Pet1000"
print("loading data")
if False:
img_data, marks = get_data(train_baza_path)
save_data(img_data, marks, img_data_path, marks_data_path)
else:
img_data, marks = load_data(img_data_path, marks_data_path)
print("Training")
train_and_save_model(img_data, marks)
print("End")