SlideShare a Scribd company logo
1 of 6
Download to read offline
1 | P a g e
##########Install opencv module########
pip install opencv-python
#######To check opencv version####
import cv2
print(cv2.__version__)
#######Read & display gray image#####
import cv2
img=cv2.imread('india.jpeg',0) #'0' loads grayscale image
print(img) #Display pixel values
cv2.imshow('image', img)
cv2.waitKey()
cv2.destroyAllWindows()
#######Read & display color image#####
import cv2
img=cv2.imread('india.jpeg',1)
print(img)
cv2.imshow('image', img)
cv2.waitKey()
cv2.destroyAllWindows()
#######Read & display original image#####
import cv2
img=cv2.imread('india.jpeg',-1)
print(img)
cv2.imshow('image', img)
cv2.waitKey()
cv2.destroyAllWindows()
#########Write image#####
import cv2
img=cv2.imread('india.jpeg',-1)
print(img)
cv2.imshow('image', img)
cv2.waitKey()
cv2.destroyAllWindows()
cv2.imwrite('india.png', img)
2 | P a g e
#########Write image with conditional statements########
import cv2
img=cv2.imread('india.jpeg',-1)
print(img)
cv2.imshow('image', img)
k=cv2.waitKey()
if k==27: #Press Escape key
cv2.destroyAllWindows()
elif k==ord('s'): #press 's' character
cv2.imwrite('india.png', img)
cv2.destroyAllWindows()
#####Drawing functions on OpenCV####
import numpy as np
import cv2
img=cv2.imread('india.jpeg',1)
img=cv2.line(img, (0,0), (255,255), (147, 96, 44), 10) #(147, 96, 44) is RGB color picker, #10 is line
#width
cv2.imshow('image', img)
cv2.waitKey()
#####Drawing functions on OpenCV######
import numpy as np
import cv2
img=cv2.imread('india.jpeg',1)
img=cv2.arrowedLine(img, (0,0), (255,255), (255, 0, 0), 10) # Blue arrow displayed
cv2.imshow('image', img)
cv2.waitKey()
############Drawing functions on OpenCV#######
import numpy as np
import cv2
img=cv2.imread('india.jpeg',1)
img=cv2.arrowedLine(img, (0,0), (255,255), (0, 255, 0), 10) # Green arrow displayed
cv2.imshow('image', img)
cv2.waitKey()
3 | P a g e
#######Drawing functions on OpenCV########
x1,y1-----------------
| |
| |
|-----------------------x2,y2
import numpy as np
import cv2
img=cv2.imread('india.jpeg',1)
img=cv2.rectangle(img, (0,100), (150,100), (0, 255, 0), 10) #(x1=0,y1=100), (x2=150,y2=100)
cv2.imshow('image', img)
cv2.waitKey()
#####Setting camera parameters#######
import cv2
cap = cv2.VideoCapture(0)
print(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
print(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
while(cap.isOpened()):
ret, frame = cap.read()
if ret == True:
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
cv2.imshow('frame', gray)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
else:
break
cap.release()
cv2.destroyAllWindows()
4 | P a g e
##########
import cv2
cap = cv2.VideoCapture(0)
print(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
print(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
cap.set(3, 1280) #3 is width parameter
cap.set(4, 720) #4 is height parameter
print(cap.get(3))
print(cap.get(4))
while(cap.isOpened()):
ret, frame = cap.read()
if ret == True:
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
cv2.imshow('frame', gray)
if cv2.waitKey(1) & 0xFF == ord('q'): #press 'q' to close the window
break
else:
break
cap.release()
cv2.destroyAllWindows()
5 | P a g e
#########add_text_to_videos######
import cv2
import datetime
cap = cv2.VideoCapture(0)
print(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
print(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
#cap.set(3, 3000)
#cap.set(4, 3000)
#print(cap.get(3))
#print(cap.get(4))
while(cap.isOpened()):
ret, frame = cap.read()
if ret == True:
font = cv2.FONT_HERSHEY_SIMPLEX
text = 'Width: '+ str(cap.get(3)) + ' Height:' + str(cap.get(4))
datet = str(datetime.datetime.now())
frame = cv2.putText(frame, text, (10, 50), font, 1,
(0, 255, 255), 2, cv2.LINE_AA) #(10,50) is co-ordinates
frame = cv2.putText(frame, datet, (10, 100), font, 1,
(0, 255, 255), 2, cv2.LINE_AA)
cv2.imshow('frame', frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
else:
break
cap.release()
cv2.destroyAllWindows()
6 | P a g e
#########mouse_event_opencv_python##########
import numpy as np
import cv2
#events = [i for i in dir(cv2) if 'EVENT' in i]
#print(events)
def click_event(event, x, y, flags, param):
if event == cv2.EVENT_LBUTTONDOWN:
print(x,', ' ,y)
font = cv2.FONT_HERSHEY_SIMPLEX
strXY = str(x) + ', '+ str(y)
cv2.putText(img, strXY, (x, y), font, .5, (255, 255, 0), 2)
cv2.imshow('image', img)
if event == cv2.EVENT_RBUTTONDOWN:
blue = img[y, x, 0]
green = img[y, x, 1]
red = img[y, x, 2]
font = cv2.FONT_HERSHEY_SIMPLEX
strBGR = str(blue) + ', '+ str(green)+ ', '+ str(red)
cv2.putText(img, strBGR, (x, y), font, .5, (0, 255, 255), 2)
cv2.imshow('image', img)
#img = np.zeros((512, 512, 3), np.uint8)
img = cv2.imread('lena.jpg')
cv2.imshow('image', img)
cv2.setMouseCallback('image', click_event)
cv2.waitKey(0)
cv2.destroyAllWindows()
############

More Related Content

What's hot

20130530-PEGjs
20130530-PEGjs20130530-PEGjs
20130530-PEGjs
zuqqhi 2
 
Programa donde suma las filass de las dos columna y ordena el resultado de l...
Programa donde suma  las filass de las dos columna y ordena el resultado de l...Programa donde suma  las filass de las dos columna y ordena el resultado de l...
Programa donde suma las filass de las dos columna y ordena el resultado de l...
Yo no soy perfecta pero soy mejor que tu
 

What's hot (8)

Qsam simulator in IBM Quantum Lab cloud
Qsam simulator in IBM Quantum Lab cloudQsam simulator in IBM Quantum Lab cloud
Qsam simulator in IBM Quantum Lab cloud
 
Quantum circuit example in Qiskit
Quantum circuit example in QiskitQuantum circuit example in Qiskit
Quantum circuit example in Qiskit
 
Arrays
ArraysArrays
Arrays
 
PVS-Studio in 2019
PVS-Studio in 2019PVS-Studio in 2019
PVS-Studio in 2019
 
Implementing string
Implementing stringImplementing string
Implementing string
 
20130530-PEGjs
20130530-PEGjs20130530-PEGjs
20130530-PEGjs
 
Programa donde suma las filass de las dos columna y ordena el resultado de l...
Programa donde suma  las filass de las dos columna y ordena el resultado de l...Programa donde suma  las filass de las dos columna y ordena el resultado de l...
Programa donde suma las filass de las dos columna y ordena el resultado de l...
 
Hybrid quantum classical neural networks with pytorch and qiskit
Hybrid quantum classical neural networks with pytorch and qiskitHybrid quantum classical neural networks with pytorch and qiskit
Hybrid quantum classical neural networks with pytorch and qiskit
 

Similar to Python openCV codes

Python program to build deep learning algorithm using a CNNs model to.docx
Python program to build deep learning algorithm using a CNNs model to.docxPython program to build deep learning algorithm using a CNNs model to.docx
Python program to build deep learning algorithm using a CNNs model to.docx
LukeQVdGrantg
 
Need helping adding to the code below to plot the images from the firs.pdf
Need helping adding to the code below to plot the images from the firs.pdfNeed helping adding to the code below to plot the images from the firs.pdf
Need helping adding to the code below to plot the images from the firs.pdf
actexerode
 
Using the code below- I need help with creating code for the following.pdf
Using the code below- I need help with creating code for the following.pdfUsing the code below- I need help with creating code for the following.pdf
Using the code below- I need help with creating code for the following.pdf
acteleshoppe
 
20145-5SumII_CSC407_assign1.htmlCSC 407 Computer Systems II.docx
20145-5SumII_CSC407_assign1.htmlCSC 407 Computer Systems II.docx20145-5SumII_CSC407_assign1.htmlCSC 407 Computer Systems II.docx
20145-5SumII_CSC407_assign1.htmlCSC 407 Computer Systems II.docx
eugeniadean34240
 
Hello- I hope you are doing well- I am doing my project- which is Rans (1).pdf
Hello- I hope you are doing well- I am doing my project- which is Rans (1).pdfHello- I hope you are doing well- I am doing my project- which is Rans (1).pdf
Hello- I hope you are doing well- I am doing my project- which is Rans (1).pdf
Ian0J2Bondo
 

Similar to Python openCV codes (20)

Python OpenCV Real Time projects
Python OpenCV Real Time projectsPython OpenCV Real Time projects
Python OpenCV Real Time projects
 
29-kashyap-mask-detaction.pptx
29-kashyap-mask-detaction.pptx29-kashyap-mask-detaction.pptx
29-kashyap-mask-detaction.pptx
 
Python program to build deep learning algorithm using a CNNs model to.docx
Python program to build deep learning algorithm using a CNNs model to.docxPython program to build deep learning algorithm using a CNNs model to.docx
Python program to build deep learning algorithm using a CNNs model to.docx
 
Open Cv 2005 Q4 Tutorial
Open Cv 2005 Q4 TutorialOpen Cv 2005 Q4 Tutorial
Open Cv 2005 Q4 Tutorial
 
Need helping adding to the code below to plot the images from the firs.pdf
Need helping adding to the code below to plot the images from the firs.pdfNeed helping adding to the code below to plot the images from the firs.pdf
Need helping adding to the code below to plot the images from the firs.pdf
 
Using the code below- I need help with creating code for the following.pdf
Using the code below- I need help with creating code for the following.pdfUsing the code below- I need help with creating code for the following.pdf
Using the code below- I need help with creating code for the following.pdf
 
20145-5SumII_CSC407_assign1.htmlCSC 407 Computer Systems II.docx
20145-5SumII_CSC407_assign1.htmlCSC 407 Computer Systems II.docx20145-5SumII_CSC407_assign1.htmlCSC 407 Computer Systems II.docx
20145-5SumII_CSC407_assign1.htmlCSC 407 Computer Systems II.docx
 
The First C# Project Analyzed
The First C# Project AnalyzedThe First C# Project Analyzed
The First C# Project Analyzed
 
Guide to Node.js: Basic to Advanced
Guide to Node.js: Basic to AdvancedGuide to Node.js: Basic to Advanced
Guide to Node.js: Basic to Advanced
 
RDataMining slides-r-programming
RDataMining slides-r-programmingRDataMining slides-r-programming
RDataMining slides-r-programming
 
HTML5: where flash isn't needed anymore
HTML5: where flash isn't needed anymoreHTML5: where flash isn't needed anymore
HTML5: where flash isn't needed anymore
 
2016 - Continuously Delivering Microservices in Kubernetes using Jenkins
2016 - Continuously Delivering Microservices in Kubernetes using Jenkins2016 - Continuously Delivering Microservices in Kubernetes using Jenkins
2016 - Continuously Delivering Microservices in Kubernetes using Jenkins
 
Composer, putting dependencies on the score
Composer, putting dependencies on the scoreComposer, putting dependencies on the score
Composer, putting dependencies on the score
 
Heroku pop-behind-the-sense
Heroku pop-behind-the-senseHeroku pop-behind-the-sense
Heroku pop-behind-the-sense
 
Easy Path to Machine Learning (2019)
Easy Path to Machine Learning (2019)Easy Path to Machine Learning (2019)
Easy Path to Machine Learning (2019)
 
Game dev 101 part 3
Game dev 101 part 3Game dev 101 part 3
Game dev 101 part 3
 
Hello- I hope you are doing well- I am doing my project- which is Rans (1).pdf
Hello- I hope you are doing well- I am doing my project- which is Rans (1).pdfHello- I hope you are doing well- I am doing my project- which is Rans (1).pdf
Hello- I hope you are doing well- I am doing my project- which is Rans (1).pdf
 
Computer vision
Computer vision Computer vision
Computer vision
 
Bcsl 033 data and file structures lab s5-2
Bcsl 033 data and file structures lab s5-2Bcsl 033 data and file structures lab s5-2
Bcsl 033 data and file structures lab s5-2
 
Use of django at jolt online v3
Use of django at jolt online v3Use of django at jolt online v3
Use of django at jolt online v3
 

More from Amarjeetsingh Thakur

More from Amarjeetsingh Thakur (20)

“Introduction to MATLAB & SIMULINK”
“Introduction to MATLAB  & SIMULINK”“Introduction to MATLAB  & SIMULINK”
“Introduction to MATLAB & SIMULINK”
 
Python code for servo control using Raspberry Pi
Python code for servo control using Raspberry PiPython code for servo control using Raspberry Pi
Python code for servo control using Raspberry Pi
 
Python code for Push button using Raspberry Pi
Python code for Push button using Raspberry PiPython code for Push button using Raspberry Pi
Python code for Push button using Raspberry Pi
 
Python code for Buzzer Control using Raspberry Pi
Python code for Buzzer Control using Raspberry PiPython code for Buzzer Control using Raspberry Pi
Python code for Buzzer Control using Raspberry Pi
 
Arduino programming part 2
Arduino programming part 2Arduino programming part 2
Arduino programming part 2
 
Arduino programming part1
Arduino programming part1Arduino programming part1
Arduino programming part1
 
Python Numpy Source Codes
Python Numpy Source CodesPython Numpy Source Codes
Python Numpy Source Codes
 
Steemit html blog
Steemit html blogSteemit html blog
Steemit html blog
 
Adafruit_IoT_Platform
Adafruit_IoT_PlatformAdafruit_IoT_Platform
Adafruit_IoT_Platform
 
Core python programming tutorial
Core python programming tutorialCore python programming tutorial
Core python programming tutorial
 
Python openpyxl
Python openpyxlPython openpyxl
Python openpyxl
 
Introduction to Internet of Things (IoT)
Introduction to Internet of Things (IoT)Introduction to Internet of Things (IoT)
Introduction to Internet of Things (IoT)
 
Introduction to Node MCU
Introduction to Node MCUIntroduction to Node MCU
Introduction to Node MCU
 
Introduction to Things board (An Open Source IoT Cloud Platform)
Introduction to Things board (An Open Source IoT Cloud Platform)Introduction to Things board (An Open Source IoT Cloud Platform)
Introduction to Things board (An Open Source IoT Cloud Platform)
 
Introduction to MQ Telemetry Transport (MQTT)
Introduction to MQ Telemetry Transport (MQTT)Introduction to MQ Telemetry Transport (MQTT)
Introduction to MQ Telemetry Transport (MQTT)
 
Arduino Interfacing with different sensors and motor
Arduino Interfacing with different sensors and motorArduino Interfacing with different sensors and motor
Arduino Interfacing with different sensors and motor
 
Image processing in MATLAB
Image processing in MATLABImage processing in MATLAB
Image processing in MATLAB
 
Introduction to Arduino
Introduction to ArduinoIntroduction to Arduino
Introduction to Arduino
 
Introduction to Arduino
Introduction to ArduinoIntroduction to Arduino
Introduction to Arduino
 
Image Processing Using MATLAB
Image Processing Using MATLABImage Processing Using MATLAB
Image Processing Using MATLAB
 

Recently uploaded

VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
dharasingh5698
 

Recently uploaded (20)

Water Industry Process Automation & Control Monthly - April 2024
Water Industry Process Automation & Control Monthly - April 2024Water Industry Process Automation & Control Monthly - April 2024
Water Industry Process Automation & Control Monthly - April 2024
 
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete RecordCCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
 
UNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its PerformanceUNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its Performance
 
Thermal Engineering-R & A / C - unit - V
Thermal Engineering-R & A / C - unit - VThermal Engineering-R & A / C - unit - V
Thermal Engineering-R & A / C - unit - V
 
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
 
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...
 
Thermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.pptThermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.ppt
 
Vivazz, Mieres Social Housing Design Spain
Vivazz, Mieres Social Housing Design SpainVivazz, Mieres Social Housing Design Spain
Vivazz, Mieres Social Housing Design Spain
 
Generative AI or GenAI technology based PPT
Generative AI or GenAI technology based PPTGenerative AI or GenAI technology based PPT
Generative AI or GenAI technology based PPT
 
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
 
Intze Overhead Water Tank Design by Working Stress - IS Method.pdf
Intze Overhead Water Tank  Design by Working Stress - IS Method.pdfIntze Overhead Water Tank  Design by Working Stress - IS Method.pdf
Intze Overhead Water Tank Design by Working Stress - IS Method.pdf
 
NFPA 5000 2024 standard .
NFPA 5000 2024 standard                                  .NFPA 5000 2024 standard                                  .
NFPA 5000 2024 standard .
 
KubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghlyKubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghly
 
UNIT-IFLUID PROPERTIES & FLOW CHARACTERISTICS
UNIT-IFLUID PROPERTIES & FLOW CHARACTERISTICSUNIT-IFLUID PROPERTIES & FLOW CHARACTERISTICS
UNIT-IFLUID PROPERTIES & FLOW CHARACTERISTICS
 
PVC VS. FIBERGLASS (FRP) GRAVITY SEWER - UNI BELL
PVC VS. FIBERGLASS (FRP) GRAVITY SEWER - UNI BELLPVC VS. FIBERGLASS (FRP) GRAVITY SEWER - UNI BELL
PVC VS. FIBERGLASS (FRP) GRAVITY SEWER - UNI BELL
 
VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...
VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...
VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...
 
Roadmap to Membership of RICS - Pathways and Routes
Roadmap to Membership of RICS - Pathways and RoutesRoadmap to Membership of RICS - Pathways and Routes
Roadmap to Membership of RICS - Pathways and Routes
 
UNIT-II FMM-Flow Through Circular Conduits
UNIT-II FMM-Flow Through Circular ConduitsUNIT-II FMM-Flow Through Circular Conduits
UNIT-II FMM-Flow Through Circular Conduits
 
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
 
Thermal Engineering Unit - I & II . ppt
Thermal Engineering  Unit - I & II . pptThermal Engineering  Unit - I & II . ppt
Thermal Engineering Unit - I & II . ppt
 

Python openCV codes

  • 1. 1 | P a g e ##########Install opencv module######## pip install opencv-python #######To check opencv version#### import cv2 print(cv2.__version__) #######Read & display gray image##### import cv2 img=cv2.imread('india.jpeg',0) #'0' loads grayscale image print(img) #Display pixel values cv2.imshow('image', img) cv2.waitKey() cv2.destroyAllWindows() #######Read & display color image##### import cv2 img=cv2.imread('india.jpeg',1) print(img) cv2.imshow('image', img) cv2.waitKey() cv2.destroyAllWindows() #######Read & display original image##### import cv2 img=cv2.imread('india.jpeg',-1) print(img) cv2.imshow('image', img) cv2.waitKey() cv2.destroyAllWindows() #########Write image##### import cv2 img=cv2.imread('india.jpeg',-1) print(img) cv2.imshow('image', img) cv2.waitKey() cv2.destroyAllWindows() cv2.imwrite('india.png', img)
  • 2. 2 | P a g e #########Write image with conditional statements######## import cv2 img=cv2.imread('india.jpeg',-1) print(img) cv2.imshow('image', img) k=cv2.waitKey() if k==27: #Press Escape key cv2.destroyAllWindows() elif k==ord('s'): #press 's' character cv2.imwrite('india.png', img) cv2.destroyAllWindows() #####Drawing functions on OpenCV#### import numpy as np import cv2 img=cv2.imread('india.jpeg',1) img=cv2.line(img, (0,0), (255,255), (147, 96, 44), 10) #(147, 96, 44) is RGB color picker, #10 is line #width cv2.imshow('image', img) cv2.waitKey() #####Drawing functions on OpenCV###### import numpy as np import cv2 img=cv2.imread('india.jpeg',1) img=cv2.arrowedLine(img, (0,0), (255,255), (255, 0, 0), 10) # Blue arrow displayed cv2.imshow('image', img) cv2.waitKey() ############Drawing functions on OpenCV####### import numpy as np import cv2 img=cv2.imread('india.jpeg',1) img=cv2.arrowedLine(img, (0,0), (255,255), (0, 255, 0), 10) # Green arrow displayed cv2.imshow('image', img) cv2.waitKey()
  • 3. 3 | P a g e #######Drawing functions on OpenCV######## x1,y1----------------- | | | | |-----------------------x2,y2 import numpy as np import cv2 img=cv2.imread('india.jpeg',1) img=cv2.rectangle(img, (0,100), (150,100), (0, 255, 0), 10) #(x1=0,y1=100), (x2=150,y2=100) cv2.imshow('image', img) cv2.waitKey() #####Setting camera parameters####### import cv2 cap = cv2.VideoCapture(0) print(cap.get(cv2.CAP_PROP_FRAME_WIDTH)) print(cap.get(cv2.CAP_PROP_FRAME_HEIGHT)) while(cap.isOpened()): ret, frame = cap.read() if ret == True: gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) cv2.imshow('frame', gray) if cv2.waitKey(1) & 0xFF == ord('q'): break else: break cap.release() cv2.destroyAllWindows()
  • 4. 4 | P a g e ########## import cv2 cap = cv2.VideoCapture(0) print(cap.get(cv2.CAP_PROP_FRAME_WIDTH)) print(cap.get(cv2.CAP_PROP_FRAME_HEIGHT)) cap.set(3, 1280) #3 is width parameter cap.set(4, 720) #4 is height parameter print(cap.get(3)) print(cap.get(4)) while(cap.isOpened()): ret, frame = cap.read() if ret == True: gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) cv2.imshow('frame', gray) if cv2.waitKey(1) & 0xFF == ord('q'): #press 'q' to close the window break else: break cap.release() cv2.destroyAllWindows()
  • 5. 5 | P a g e #########add_text_to_videos###### import cv2 import datetime cap = cv2.VideoCapture(0) print(cap.get(cv2.CAP_PROP_FRAME_WIDTH)) print(cap.get(cv2.CAP_PROP_FRAME_HEIGHT)) #cap.set(3, 3000) #cap.set(4, 3000) #print(cap.get(3)) #print(cap.get(4)) while(cap.isOpened()): ret, frame = cap.read() if ret == True: font = cv2.FONT_HERSHEY_SIMPLEX text = 'Width: '+ str(cap.get(3)) + ' Height:' + str(cap.get(4)) datet = str(datetime.datetime.now()) frame = cv2.putText(frame, text, (10, 50), font, 1, (0, 255, 255), 2, cv2.LINE_AA) #(10,50) is co-ordinates frame = cv2.putText(frame, datet, (10, 100), font, 1, (0, 255, 255), 2, cv2.LINE_AA) cv2.imshow('frame', frame) if cv2.waitKey(1) & 0xFF == ord('q'): break else: break cap.release() cv2.destroyAllWindows()
  • 6. 6 | P a g e #########mouse_event_opencv_python########## import numpy as np import cv2 #events = [i for i in dir(cv2) if 'EVENT' in i] #print(events) def click_event(event, x, y, flags, param): if event == cv2.EVENT_LBUTTONDOWN: print(x,', ' ,y) font = cv2.FONT_HERSHEY_SIMPLEX strXY = str(x) + ', '+ str(y) cv2.putText(img, strXY, (x, y), font, .5, (255, 255, 0), 2) cv2.imshow('image', img) if event == cv2.EVENT_RBUTTONDOWN: blue = img[y, x, 0] green = img[y, x, 1] red = img[y, x, 2] font = cv2.FONT_HERSHEY_SIMPLEX strBGR = str(blue) + ', '+ str(green)+ ', '+ str(red) cv2.putText(img, strBGR, (x, y), font, .5, (0, 255, 255), 2) cv2.imshow('image', img) #img = np.zeros((512, 512, 3), np.uint8) img = cv2.imread('lena.jpg') cv2.imshow('image', img) cv2.setMouseCallback('image', click_event) cv2.waitKey(0) cv2.destroyAllWindows() ############