Berechnen Sie die ABS-Differenz zwischen zwei Pixeln im HSV-FarbraumPython

Python-Programme
Guest
 Berechnen Sie die ABS-Differenz zwischen zwei Pixeln im HSV-Farbraum

Post by Guest »

Vorwort/Erklärung:
Ich versuche, zwei Pixel im HSV-Farbraum zu vergleichen und ihren absoluten Unterschied zu berechnen. Mein Code scheint es für den S & V-Kanal korrekt zu machen. Meistens liefern die H-Kanäle jedoch den korrekten Wert, aber manchmal erhalte ich ein völlig unvernünftiges Ergebnis.
Was ich in meinem Code mache:
  • Aufnehmen eines Videos
  • Suchen einer bestimmten Position im ersten Bild und Speichern dieses Pixelwerts in HSV
  • Analysieren Sie das Video Bild für Bild und erfassen Sie die HSV-Werte an der Pixelposition aus Schritt 2
  • Berechnung der Differenz von 3 und 2
Hypothese:
Ich verstehe anscheinend etwas über den HSV-Farbraum falsch.
Nach einigen Recherchen zum Stackoverflow habe ich die Min-Funktion gegen den 360-Limit-Vergleich für den H-Kanal und den 255-Vergleich für den V-Kanal implementiert. Hin und wieder erhalte ich jedoch immer noch ein falsches Ergebnis für den H-Kanal.
Mein Code:

Code: Select all

import cv2 as cv
import numpy as np
import pyautogui
import time

tilling_target = cv.imread("tilling_target7.png")
tilling_target = np.array(tilling_target)
cap = cv.VideoCapture("video.mp4")

'Move to starting Lure Position'
ret, frame = cap.read()
lure_location = cv.matchTemplate(
frame,
tilling_target,
cv.TM_CCOEFF_NORMED)
loc_arr = np.array(location)

min_val, max_val, min_loc, max_loc = cv.minMaxLoc(loc_arr)
print(max_loc)
frameHSV = cv.cvtColor(frame, cv.COLOR_BGR2HSV)
urpixel = frameHSV[max_loc[1]+20, max_loc[0]-40]
start_time = time.time()

'Watch the lure and changes'
while True:
ret, frame = cap.read()
frameHSV = cv.cvtColor(frame, cv.COLOR_BGR2HSV)
pixel = frameHSV[max_loc[1]+20, max_loc[0]-40]
print("urpixel:",urpixel)
print("pixel:",pixel)
print("time:",time.time()-start_time)
dif_to_urpixel = [min(abs(pixel[0]-urpixel[0]), 360-abs((pixel[0]-urpixel[0]))), abs(pixel[1]-urpixel[1]), min(abs(pixel[2]-urpixel[2]), 255-abs(pixel[2]-urpixel[2]))]]
print("differenz:",dif_to_urpixel)
print("sum diff:", sum(dif_to_urpixel))
Ausgabe-Snippet:

Code: Select all

urpixel: [100 114  65]
pixel: [101 138  63]
time: 3.048245668411255
differenz: [1, 24, 1] **--> working as intended**
sum diff: 26

urpixel: [100 114  65]
pixel: [ 99 124  74]
time: 3.2529683113098145
differenz: [105, 10, 9] **--> 100-99 should result in 1 and not 105??**
sum diff: 124

urpixel: [100 114  65]
pixel: [101 143  57]
time: 6.2827301025390625
differenz: [1, 29, 7] **--> working as intended**

urpixel: [100 114  65]
pixel: [ 99 134  57]
time: 5.828529596328735
differenz: [105, 20, 7] **--> Again resulting in 105 instead of 1**

Quick Reply

Change Text Case: 
   
  • Similar Topics
    Replies
    Views
    Last post