SlideShare a Scribd company logo
1 of 4
Reading Images
To read an image as it is
>>>import cv2
>>> a=cv2.imread('D:/1.jpg')
(or)
>>> a=cv2.imread('D:/1.jpg',-1) #the default
(or)
>>> a=cv2.imread('D:/1.jpg',cv2.IMREAD_UNCHANGED)
To show an image
>>> cv2.imshow('window_name',a)
>>>cv2.waitKey(0)
window_name can be any name u like. cv2.waitKey(milliseconds) returns ASCII value of the key pressed
on the Keyboard
To destroy the window created through imshow
To destroy all windows,
>>>cv2.destroyAllWindows()
To destroy a specific window only,
>>>cv2.destroyWindow('window_name')
To check the dimensions of the image read
>>> import cv2
>>> image=cv2.imread('1.png')
>>> image.shape
(450, 300, 3)
#450 rows, 300 columns and 3 planes
>>> (rows,columns,channels)=image.shape
Converting an image into gray scale
gray=cv2.cvtColor(i, cv2.COLOR_BGR2GRAY)
COLOR_RGB2 YCrCb
'COLOR_BGR2RGB'
flags
To find different flags available in opencv
To find the list of color conversion possible
>>> flags= [i for i in dir(cv2) if i.startswith('COLOR_')]
>>> print flags
To find the flags for events in opencv
events=[i for i in dir(cv2) if 'EVENT' in i]
print events
To find the different line flags available
>>> flags= [i for i in dir(cv2) if i.startswith('LINE_')]
>>> print flags
To find the different FONT flags available
fonts=[i for i in dir(cv2) if 'FONT' in i]
print fonts
To draw a Line
To draw a rectangle
To Draw an Ellipse
Starting Co-
ordinate value
Starting Co-
ordinate value
Ending Co-
ordinate value
Colour of the
line in B,G,R
Thickness of line
Line style /flag
cv2.line(image,(0,0),(100,100),(255,0,0),5,cv2.LINE_AA)
image
Top-left Co-
ordinate value
bottom right Co-
ordinate value
Colour of the
line in B,G,R
Thickness of line
cv2.rectangle(image,(0,0),(100,100),(255,0,0),5)
image
center Co-
ordinate value
major axis, minor
axis length
Colour of the
line in B,G,R
Angle of rotation in anti
clockwise direction
cv2.ellipse(image,(0,0),(100,100),(255,0,0),0)
To add text to images
To resize an image
resized = cv2.resize(image,(rows_in_resized, columns_in_resized), cv2.INTER_LINEAR)
To copy an image
image=cv2.imread(‘C:/1.jpg’)
copied=image.copy()
Convert image to binary image, based on threshold
>>> gray=cv2.cvtColor(image,cv2.COLOR_BGR2GRAY)
>>> ret1,thresh1=cv2.threshold(gray,0,255,cv2.THRESH_BINARY+cv2.THRESH_OTSU)
>>> ret1
149.0 # threshold value decided by OTSU and all the pixels intensity above this threshold is made 255
#thresh1 is the binary image.
>>> ret,thresh=cv2.threshold(gray,50,255,cv2.THRESH_BINARY)
>>> ret
50.0# threshold value decided by u i.e, second argument
To find contours
_,contours,hierarchy=cv2.findContours(thresh,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
Text to be written
on image
Position on image
where text to be
inserted
Thickness of line
FONT flag
cv2.putText(img, 'Text to be added ',(100,100),cv2.FONT_ITALIC,4,(255,255,255),2,cv2.LINE_AA)
image
Line Type flag
Font
Size
Font
Colour
cv2.INTER_AREA
cv2.INTER_CUBIC
Interp
olatio
n flag
To flip an image
rimg=cv2.flip(img,1) #vertical flip
fimg=cv2.flip(img,0) #horizontal flip
Image Arithmetic
Adding two images
added=cv2.add(img1,img2)
added=cv2.addWeighted(img1,0.7,img2,0.3,0)
Absolute difference of two images
difference= cv2.absdiff(img1,img2)
Creating the negative of an binary image
binaryInvert=cv2.bitwise_not(binary_imag)
Find the edges of an image using Canny
edges = cv2.Canny(image,100,200)
Gradient Filters
1. sobelx=cv2.Sobel(image,cv2.CU_64F,1,0,5)
sobely=cv2.Sobel(image,cv2.CU_64F,0,1,5)
2. lap=cv2.Laplacian(image,cv2.CU_64F
Visit my blog:
enthusiaststudent.blogspot.in
Keywords:
cv2.imread, cv2.imshow, cv2.waitKey, cv2.destroyAllWindows, cv2.cvtColor, cv2.destroyWindow, cv2.line,
cv2.rectangle, cv2.ellipse, cv2.putText, cv2.resize, cv2.threshold, cv2.THRESH_BINARY,
cv2.THRESH_OTSU, cv2.findContours, cv2.flip, cv2.add, cv2.absdiff, cv2.bitwise_not, cv2.Canny

More Related Content

More from Lakshmi Sarvani Videla (20)

Recursive functions in C
Recursive functions in CRecursive functions in C
Recursive functions in C
 
Function Pointer in C
Function Pointer in CFunction Pointer in C
Function Pointer in C
 
Functions
FunctionsFunctions
Functions
 
Java sessionnotes
Java sessionnotesJava sessionnotes
Java sessionnotes
 
Singlelinked list
Singlelinked listSinglelinked list
Singlelinked list
 
Graphs
GraphsGraphs
Graphs
 
B trees
B treesB trees
B trees
 
Functions in python3
Functions in python3Functions in python3
Functions in python3
 
Dictionary
DictionaryDictionary
Dictionary
 
Sets
SetsSets
Sets
 
Lists
ListsLists
Lists
 
DataStructures notes
DataStructures notesDataStructures notes
DataStructures notes
 
Solutionsfor co2 C Programs for data structures
Solutionsfor co2 C Programs for data structuresSolutionsfor co2 C Programs for data structures
Solutionsfor co2 C Programs for data structures
 
C programs
C programsC programs
C programs
 
Data Mining: Data Preprocessing
Data Mining: Data PreprocessingData Mining: Data Preprocessing
Data Mining: Data Preprocessing
 
Dbms
DbmsDbms
Dbms
 
Computer Organization and 8085 microprocessor notes
Computer Organization and 8085 microprocessor notesComputer Organization and 8085 microprocessor notes
Computer Organization and 8085 microprocessor notes
 
Complete Operating System notes
Complete Operating System notesComplete Operating System notes
Complete Operating System notes
 
Computer ScienceTechnical quiz
Computer ScienceTechnical quizComputer ScienceTechnical quiz
Computer ScienceTechnical quiz
 
Networks notes
Networks notesNetworks notes
Networks notes
 

Recently uploaded

Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationRadu Cotescu
 
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024BookNet Canada
 
Maximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxMaximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxOnBoard
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Drew Madelung
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Miguel Araújo
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking MenDelhi Call girls
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxMalak Abu Hammad
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonetsnaman860154
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘RTylerCroy
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationRidwan Fadjar
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking MenDelhi Call girls
 
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...shyamraj55
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityPrincipled Technologies
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking MenDelhi Call girls
 
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...HostedbyConfluent
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountPuma Security, LLC
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdfhans926745
 
Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Paola De la Torre
 
Google AI Hackathon: LLM based Evaluator for RAG
Google AI Hackathon: LLM based Evaluator for RAGGoogle AI Hackathon: LLM based Evaluator for RAG
Google AI Hackathon: LLM based Evaluator for RAGSujit Pal
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking MenDelhi Call girls
 

Recently uploaded (20)

Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
 
Maximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxMaximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptx
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptx
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 Presentation
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
 
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path Mount
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101
 
Google AI Hackathon: LLM based Evaluator for RAG
Google AI Hackathon: LLM based Evaluator for RAGGoogle AI Hackathon: LLM based Evaluator for RAG
Google AI Hackathon: LLM based Evaluator for RAG
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 

Basic Python2.7 and open cv3 Cheatsheet

  • 1. Reading Images To read an image as it is >>>import cv2 >>> a=cv2.imread('D:/1.jpg') (or) >>> a=cv2.imread('D:/1.jpg',-1) #the default (or) >>> a=cv2.imread('D:/1.jpg',cv2.IMREAD_UNCHANGED) To show an image >>> cv2.imshow('window_name',a) >>>cv2.waitKey(0) window_name can be any name u like. cv2.waitKey(milliseconds) returns ASCII value of the key pressed on the Keyboard To destroy the window created through imshow To destroy all windows, >>>cv2.destroyAllWindows() To destroy a specific window only, >>>cv2.destroyWindow('window_name') To check the dimensions of the image read >>> import cv2 >>> image=cv2.imread('1.png') >>> image.shape (450, 300, 3) #450 rows, 300 columns and 3 planes >>> (rows,columns,channels)=image.shape Converting an image into gray scale gray=cv2.cvtColor(i, cv2.COLOR_BGR2GRAY) COLOR_RGB2 YCrCb 'COLOR_BGR2RGB' flags
  • 2. To find different flags available in opencv To find the list of color conversion possible >>> flags= [i for i in dir(cv2) if i.startswith('COLOR_')] >>> print flags To find the flags for events in opencv events=[i for i in dir(cv2) if 'EVENT' in i] print events To find the different line flags available >>> flags= [i for i in dir(cv2) if i.startswith('LINE_')] >>> print flags To find the different FONT flags available fonts=[i for i in dir(cv2) if 'FONT' in i] print fonts To draw a Line To draw a rectangle To Draw an Ellipse Starting Co- ordinate value Starting Co- ordinate value Ending Co- ordinate value Colour of the line in B,G,R Thickness of line Line style /flag cv2.line(image,(0,0),(100,100),(255,0,0),5,cv2.LINE_AA) image Top-left Co- ordinate value bottom right Co- ordinate value Colour of the line in B,G,R Thickness of line cv2.rectangle(image,(0,0),(100,100),(255,0,0),5) image center Co- ordinate value major axis, minor axis length Colour of the line in B,G,R Angle of rotation in anti clockwise direction cv2.ellipse(image,(0,0),(100,100),(255,0,0),0)
  • 3. To add text to images To resize an image resized = cv2.resize(image,(rows_in_resized, columns_in_resized), cv2.INTER_LINEAR) To copy an image image=cv2.imread(‘C:/1.jpg’) copied=image.copy() Convert image to binary image, based on threshold >>> gray=cv2.cvtColor(image,cv2.COLOR_BGR2GRAY) >>> ret1,thresh1=cv2.threshold(gray,0,255,cv2.THRESH_BINARY+cv2.THRESH_OTSU) >>> ret1 149.0 # threshold value decided by OTSU and all the pixels intensity above this threshold is made 255 #thresh1 is the binary image. >>> ret,thresh=cv2.threshold(gray,50,255,cv2.THRESH_BINARY) >>> ret 50.0# threshold value decided by u i.e, second argument To find contours _,contours,hierarchy=cv2.findContours(thresh,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE) Text to be written on image Position on image where text to be inserted Thickness of line FONT flag cv2.putText(img, 'Text to be added ',(100,100),cv2.FONT_ITALIC,4,(255,255,255),2,cv2.LINE_AA) image Line Type flag Font Size Font Colour cv2.INTER_AREA cv2.INTER_CUBIC Interp olatio n flag
  • 4. To flip an image rimg=cv2.flip(img,1) #vertical flip fimg=cv2.flip(img,0) #horizontal flip Image Arithmetic Adding two images added=cv2.add(img1,img2) added=cv2.addWeighted(img1,0.7,img2,0.3,0) Absolute difference of two images difference= cv2.absdiff(img1,img2) Creating the negative of an binary image binaryInvert=cv2.bitwise_not(binary_imag) Find the edges of an image using Canny edges = cv2.Canny(image,100,200) Gradient Filters 1. sobelx=cv2.Sobel(image,cv2.CU_64F,1,0,5) sobely=cv2.Sobel(image,cv2.CU_64F,0,1,5) 2. lap=cv2.Laplacian(image,cv2.CU_64F Visit my blog: enthusiaststudent.blogspot.in Keywords: cv2.imread, cv2.imshow, cv2.waitKey, cv2.destroyAllWindows, cv2.cvtColor, cv2.destroyWindow, cv2.line, cv2.rectangle, cv2.ellipse, cv2.putText, cv2.resize, cv2.threshold, cv2.THRESH_BINARY, cv2.THRESH_OTSU, cv2.findContours, cv2.flip, cv2.add, cv2.absdiff, cv2.bitwise_not, cv2.Canny