1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
import numpy as np import cv2 print(cv2.__version__) def onTrack1(val): global hueLow hueLow=val print('Hue Low',hueLow) def onTrack2(val): global hueHigh hueHigh=val print('Hue High',hueHigh) def onTrack3(val): global satLow satLow=val print('Sat Low',satLow) def onTrack4(val): global satHigh satHigh=val print('Sat High',satHigh) def onTrack5(val): global valLow valLow=val print('Val Low',valLow) def onTrack6(val): global valHigh valHigh=val print('Val High',valHigh) width=640 height=360 cam=cv2.VideoCapture(0,cv2.CAP_DSHOW) cam.set(cv2.CAP_PROP_FRAME_WIDTH, width) cam.set(cv2.CAP_PROP_FRAME_HEIGHT,height) cam.set(cv2.CAP_PROP_FPS, 30) cam.set(cv2.CAP_PROP_FOURCC,cv2.VideoWriter_fourcc(*'MJPG')) cv2.namedWindow('myTracker') cv2.moveWindow('myTracker',width,0) hueLow=10 hueHigh=20 satLow=10 satHigh=250 valLow=10 valHigh=250 cv2.createTrackbar('Hue Low','myTracker',10,179,onTrack1) cv2.createTrackbar('Hue High','myTracker',20,179,onTrack2) cv2.createTrackbar('Sat Low','myTracker',10,255,onTrack3) cv2.createTrackbar('Sat High','myTracker',250,255,onTrack4) cv2.createTrackbar('Val Low','myTracker',10,255,onTrack5) cv2.createTrackbar('Val High','myTracker',250,255,onTrack6) while True: ignore, frame = cam.read() frameHSV=cv2.cvtColor(frame,cv2.COLOR_BGR2HSV) lowerBound=np.array([hueLow,satLow,valLow]) upperBound=np.array([hueHigh,satHigh,valHigh]) myMask=cv2.inRange(frameHSV,lowerBound,upperBound) #myMask=cv2.bitwise_not(myMask) myObject=cv2.bitwise_and(frame,frame,mask=myMask) myObjectSmall=cv2.resize(myObject,(int(width/2),int(height/2))) cv2.imshow('My Object',myObjectSmall) cv2.moveWindow('My Object',int(width/2),int(height)) myMaskSmall=cv2.resize(myMask,(int(width/2),int(height/2))) cv2.imshow('My Mask',myMaskSmall) cv2.moveWindow('My Mask',0,height) cv2.imshow('my WEBcam', frame) cv2.moveWindow('my WEBcam',0,0) if cv2.waitKey(1) & 0xff ==ord('q'): break cam.release() |
Tag Archives: Python
Putting Text, Rectangles and Circles on Images in OpenCV for Windows
In the video lesson above we show how to add text, rectangles and circles to video frames using OpenCV. Below we include the code with we developed in this lesson. Enjoy!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
import cv2 print(cv2.__version__) width=640 height=360 myRadius=30 myColor=(0,0,0) myThick=2 fontH=2 fontT=2 myText='Paul is Boss' myFont=cv2.FONT_HERSHEY_DUPLEX upperLeft=(250,140) lowerRight=(390,220) lineW=4 cam=cv2.VideoCapture(1,cv2.CAP_DSHOW) cam.set(cv2.CAP_PROP_FRAME_WIDTH, width) cam.set(cv2.CAP_PROP_FRAME_HEIGHT,height) cam.set(cv2.CAP_PROP_FPS, 30) cam.set(cv2.CAP_PROP_FOURCC,cv2.VideoWriter_fourcc(*'MJPG')) while True: ignore, frame = cam.read() frame[140:220,250:390]=(255,0,0) cv2.rectangle(frame,upperLeft,lowerRight,(0,255,0),lineW) cv2.circle(frame,(int(width/2),int(height/2)),myRadius,myColor,myThick) cv2.putText(frame,myText,(120,60),myFont,fontH,(0,0,255),fontT) cv2.imshow('my WEBcam', frame) cv2.moveWindow('my WEBcam',0,0) if cv2.waitKey(1) & 0xff ==ord('q'): break cam.release() |
Python 3D Graphics Tutorial 5: Bouncing Ball Simulation in Visual Python
The program below is the code we developed in Lesson 5. It is a Vpython program that creates a 3D model of a marble bouncing in a box.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
from vpython import * from time import * mRadius=.5 wallThickness=.1 roomWidth=12 roomDepth=20 roomHeight=2 floor=box(pos=vector(0,-roomHeight/2,0),size=vector(roomWidth,wallThickness,roomDepth), color=color.white) ceiling=box(pos=vector(0,roomHeight/2,0),size=vector(roomWidth,wallThickness,roomDepth), color=color.white) backWall=box(pos=vector(0,0,-roomDepth/2),size=vector(roomWidth,roomHeight,wallThickness), color=color.white) leftWall=box(pos=vector(-roomWidth/2,0,0),size=vector(wallThickness,roomHeight,roomDepth), color=color.white) rightWall=box(pos=vector(roomWidth/2,0,0),size=vector(wallThickness,roomHeight,roomDepth), color=color.white) marble=sphere(radius=mRadius,color=color.red) deltaX=.1 deltaY=.1 deltaZ=.1 xPos=0 yPos=0 zPos=0 while True: rate(25) xPos=xPos+deltaX yPos=yPos+deltaY zPos=zPos+deltaZ Xrme=xPos+mRadius Xlme=xPos-mRadius Ytme=yPos+mRadius Ybme=yPos-mRadius Zbme=zPos-mRadius Zfme=zPos+mRadius Rwe=roomWidth/2-wallThickness/2 Lwe=-roomWidth/2+wallThickness/2 Cwe=roomHeight/2-wallThickness/2 FLOORwe=-roomHeight/2+wallThickness/2 Bwe=-roomDepth/2+wallThickness/2 Fwe=roomDepth/2-wallThickness/2 if (Xrme>=Rwe or Xlme<=Lwe): deltaX=deltaX*(-1) if (Ytme>=Cwe or Ybme<=FLOORwe): deltaY=deltaY*(-1) if (Zfme>=Fwe or Zbme<=Bwe): deltaZ=deltaZ*(-1) marble.pos=vector(xPos,yPos,zPos) |
Python 3D Graphics LESSON 4: Understanding 3D Motion in Visual Python Model
In this lesson we show how to make an animated ball properly bounce off a wall using Visual Python (Vpython).
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
from vpython import * from time import * mRadius=2 wallThickness=1 roomWidth=15 roomDepth=12 roomHeight=8 floor=box(pos=vector(0,-roomHeight/2,0),size=vector(roomWidth,wallThickness,roomDepth), color=color.white) ceiling=box(pos=vector(0,roomHeight/2,0),size=vector(roomWidth,wallThickness,roomDepth), color=color.white) backWall=box(pos=vector(0,0,-roomDepth/2),size=vector(roomWidth,roomHeight,wallThickness), color=color.white) leftWall=box(pos=vector(-roomWidth/2,0,0),size=vector(wallThickness,roomHeight,roomDepth), color=color.white) rightWall=box(pos=vector(roomWidth/2,0,0),size=vector(wallThickness,roomHeight,roomDepth), color=color.white) marble=sphere(radius=mRadius,color=color.red) deltaX=.1 xPos=0 while True: rate(25) xPos=xPos+deltaX Xrme=xPos+mRadius Xlme=xPos-mRadius Rwe=roomWidth/2-wallThickness/2 Lwe=-roomWidth/2+wallThickness/2 if (Xrme>=Rwe or Xlme<Lwe): deltaX=deltaX*(-1) marble.pos=vector(xPos,0,0) |
Python 3D Graphics LESSON 3: Parameterizing your 3D Model
In this lesson we show you the importance of building a parameterized model for your 3D graphics in Python using Visual Python. You can manage large scenes unless you are doing it with parameterized dimensions. The video above takes you through it step by step. Here is the code for the parameterized box and marble we developed in Lesson 3:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
from vpython import * from time import * mRadius=.75 wallThickness=.1 roomWidth=10 roomDepth=5 roomHeight=10 floor=box(pos=vector(0,-roomHeight/2,0),size=vector(roomWidth,wallThickness,roomDepth), color=color.white) ceiling=box(pos=vector(0,roomHeight/2,0),size=vector(roomWidth,wallThickness,roomDepth), color=color.white) backWall=box(pos=vector(0,0,-roomDepth/2),size=vector(roomWidth,roomHeight,wallThickness), color=color.white) leftWall=box(pos=vector(-roomWidth/2,0,0),size=vector(wallThickness,roomHeight,roomDepth), color=color.white) rightWall=box(pos=vector(roomWidth/2,0,0),size=vector(wallThickness,roomHeight,roomDepth), color=color.white) marble=sphere(radius=mRadius,color=color.red) deltaX=.1 xPos=0 while True: rate(10) xPos=xPos+deltaX if (xPos>roomWidth/2 or xPos<-roomWidth/2): deltaX=deltaX*(-1) marble.pos=vector(xPos,0,0) |