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

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()