Änderungen

Aus Hackerspace Ffm
Wechseln zu: Navigation, Suche

OpenCV mit Python

2.165 Byte entfernt, 18:52, 31. Mai 2019
/* OpenCV Cheats */
* Bildbereich kopieren: <code>block = bild[y0:y1, x0:x1]</code>
* Bildbereich woanders einfügen (Größe muss genau passen!): <code>bild[y0:y1, x0:x1] = block</code>
* Alle Farbkomponenten eines Pixels ändern: <code>bild[x,y,x] = [b,g,r]</code>
* Nur eine Farbkomponente ändern (hier g = Index 1 da Reihenfolge BGR): <code>bild[x,y,1] = 255</code>
* 8-Bit Farbkomponenten holen: <code>bild.astype(np.uint8)</code> (falls man mal mit float oder so gerechnet hat)
Code funktioniert unter OpenCV Version 3.4.4
<pre>from picamera[[Datei:OpencvOpticalFlowPICam.array import PiRGBArrayfrom picamera import PiCameraimport timeimport numpy as npimport cv2zip]]
# initialize the camera and grab a reference to the raw camera capturecamera = PiCamera()camera.resolution = (640, 480)camera.framerate = 32camera.rotation = 0rawCapture = PiRGBArray(camera, size=camera.resolution) # allow the camera to warmuptime.sleep(1)  print(cv2.__version__) # params for ShiTomasi corner detectionfeature_params = dict( maxCorners = 100, qualityLevel = 0.3, minDistance = 7, blockSize = 7 ) # Parameters for lucas kanade optical flowlk_params = dict( winSize = (15,15), maxLevel = 2, criteria = (cv2.TERM_CRITERIA_EPS | cv2.TERM_CRITERIA_COUNT, 10, 0.03)) # Create some random colorscolor = np.random.randint(0,255,(100,3)) first_frame = True for frame in camera.capture_continuous(rawCapture, format="bgr", use_video_port=True): image = frame.array #ret, frame = frame frame_gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) if first_frame: # Take first frame and find corners in it old_gray = frame_gray.copy() p0 = cv2.goodFeaturesToTrack(old_gray, mask = None, **feature_params) # Create a mask image for drawing purposes mask = np.zeros_like(old_gray) # print(old_gray) first_frame = False    # calculate optical flow p1, st, err = cv2.calcOpticalFlowPyrLK(old_gray, frame_gray, p0, None, **lk_params)  # Select good points good_new = p1[st==1] good_old = p0[st==1]  # draw the tracks for i,(new,old) in enumerate(zip(good_new,good_old))Datei: a,b = newOpencvOpticalFlowUSBCam.ravel() c,d = old.ravel() mask = cv2.line(mask, (a,b),(c,d), color[izip].tolist(), 2) frame = cv2.circle(frame_gray,(a,b),5,color[i].tolist(),-1) img = cv2.add(frame_gray,mask) # print(img) cv2.imshow('frame',img) k = cv2.waitKey(30) & 0xff if k == 27: break rawCapture.truncate(0)  # Now update the previous frame and previous points old_gray = frame_gray.copy() p0 = good_new.reshape(-1,1,2) cv2.destroyAllWindows()camera.close()</pre>
[[Datei:OpencvWorkshopOpticalFlow.jpg|400px]]
1.932
Bearbeitungen