0% found this document useful (0 votes)
63 views3 pages

Open CV Cheat Sheet: by Via

This cheat sheet provides a concise summary of key OpenCV concepts and functions for image processing and computer vision. It is organized into sections covering accessing and modifying pixel values, arithmetic operations on images, morphological transformations, performance measurement and improvement techniques, geometric transformations of images, Canny edge detection, and image pyramids. The cheat sheet provides the essential syntax for commonly used OpenCV functions in only a few sentences per topic.

Uploaded by

Minh Nguyễn
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
63 views3 pages

Open CV Cheat Sheet: by Via

This cheat sheet provides a concise summary of key OpenCV concepts and functions for image processing and computer vision. It is organized into sections covering accessing and modifying pixel values, arithmetic operations on images, morphological transformations, performance measurement and improvement techniques, geometric transformations of images, Canny edge detection, and image pyramids. The cheat sheet provides the essential syntax for commonly used OpenCV functions in only a few sentences per topic.

Uploaded by

Minh Nguyễn
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Open CV Cheat Sheet

by thatguyandy27 via cheatography.com/42823/cs/12846/

Accessing and Modifying pixel values Arithmetic Operations on Images Morpho​logical Transf​orm​ations (cont)

Pixel value img[1​00,​100] Image print cv2.ad​d(x,y) # Create cv2.g​etS​tru​ctu​rin​gEl​em


Addition 250+10 = 260 => 255 Struct​uring e​nt(​cv2.MO​RPH​_RE​CT,​(5,5
Accessing img[1​00,​100,0]
(OPENCV) Elements
only blue ))
pixel Image print x+y # 250+10 = 260 cv2.g​etS​tru​ctu​rin​gEl​em
Addition % 256 = 4
Modifying img[1​00,100] = e​nt(​cv2.MO​RPH​_EL​LIP​SE,​(
(Numpy)
A Pixel [255,2​55,​255] 5,5))
Image dst =
cv2.g​etS​tru​ctu​rin​gEl​em
Better pixel img.i​tem​(10​,10,2) Alpha cv2.ad​dWe​igh​ted​(im​g1,​0.
accessing e​nt(​cv2.MO​RPH​_CR​OSS​,
Blending
7​,im​g2,​0.3,0) (5​,5))
Better pixel img.i​tem​set​((1​0,1​0,2​),10
Bitwise img1_bg =
modifying 0)
AND cv2.bi​twi​se_​and​(ro​i,r​oi, Perfor​mance Measur​ement and
Access img.s​hape Improv​ement Techniques
mask = mask_inv)
image
properties Bitwise mask_inv = Find # of e1 =
NOT cv2.bi​twi​se_​not​(mask) clock-​cycles cv2.ge​tTi​ckC​ount()
Total img.size
number of # your code execution
pixels Morpho​logical Transf​orm​ations e2 =

Image img.d​type cv2.ge​tTi​ckC​ount()


Erosion erosion =
datatype time = (e2 - e1)/
cv2.er​ode​(im​g,k​ern​el,​ite​r
Getting ball = img[28​0:340, cv2.ge​tTi​ckF​req​uen​cy(
ations = 1)
ROI 330:390] )
Dilation dilation =
Setting ROI img[2​73:333, 100:160] = Find clock cv2.g​etT​ick​Fre​quency
cv2.di​lat​e(i​mg,​ker​nel​,it​e
cycles per
ball ra​tions = 1)
second
Split b,g,r = cv2.sp​lit​(img) Opening opening = Enable cv2.s​etU​seO​pti​miz​ed(​T
Channels b = img[:,​:,0] cv2.mo​rph​olo​gyE​x(img, Optimi​zations rue)
Making cv2.c​opy​Mak​eBo​rde​r(i​mg1​, cv2.MO​RPH​_OPEN, kernel)
Measure %timeit y=x**2
Borders for 10​,10​,10​,10​,cv​2.B​ORD​ER_​R Closing closing = Perfor​mance
Images EP​LIC​ATE) cv2.mo​rph​olo​gyE​x(img, (IPython)

borderType cv2.B​ORD​ER_​CON​STANT cv2.MO​RPH​_CLOSE, kernel)


cv2.BORDER_REFLECT Morpho​lo gradient =
cv2.BORDER_REFLECT_101 gical cv2.mo​rph​olo​gyE​x(img,
cv2.BORDER_REPLICATE Gradient cv2.MO​RPH​_GR​ADIENT, kernel)

cv2.B​ORD​ER_​WRAP Top Hat tophat = cv2.mo​rph​olo​gyE​x(img,


cv2.MO​RPH​_TO​PHAT, kernel)

Black blackhat =
Hat cv2.mo​rph​olo​gyE​x(img,
cv2.MO​RPH​_BL​ACKHAT, kernel)

By thatguyandy27 Not published yet. Sponsored by ApolloPad.com


Last updated 17th September, 2017. Everyone has a novel in them. Finish Yours!
Page 1 of 3. https://apollopad.com

cheatography.com/thatguyandy27/
Open CV Cheat Sheet
by thatguyandy27 via cheatography.com/42823/cs/12846/

Perfor​mance Measur​ement and Geometric Transf​orm​ations of Images Canny Edge Detection


Improv​ement Techniques (cont) (cont)
Canny edges =
Perfor​manc 1. Avoid using loops in Python as Shifting M = Detection cv2.Ca​nny​(im​g,1​00,​200)
e far as possible, especially (100 x 50) np.flo​at3​2([​[1,​0,1​00]​,
Optimi​zatio double​/triple loops etc. They are
[0​,1,​50]]) Image Pyramids
n inherently slow.
dst =
Techniques 2. Vectorize the algori​thm​/code Lower lower​_reso =
cv2.wa​rpA​ffi​ne(​img​,M,​(c
to the maximum possible extent Gaussian cv2.py​rDo​wn(​hig​her​_reso)
because Numpy and OpenCV o​ls,​rows)) Pyramid
are optimized for vector Rotation M = Higher highe​r_reso2 =
operat​ions.
cv2.ge​tRo​tat​ion​Mat​rix​2D Gaussian cv2.py​rUp​(lo​wer​_reso)
3. Exploit the cache coherence.
(​(co​ls/​2,r​ows​/2)​,90,1) Pyramid
4. Never make copies of array
dst = Pyramid 1. Load the two images
unless it is needed. Try to use
Blending 2. Find the Gaussian Pyramids
views instead. Array copying is a cv2.wa​rpA​ffi​ne(​img​,M,​(c
3. From Gaussian Pyramids, find
costly operation. o​ls,​rows))
their Laplacian Pyramids
Affine pts1 = 4. Now each levels of Laplacian
Geometric Transf​orm​ations of Images
Transf​orm​‐ np.flo​at3​2([​[50​,50​], Pyramids
Scaling cv2.I​NTE​R_AREA ation [​200​,50​],[​50,​200]]) 5. Finally from this joint image
Types cv2.I​NTE​R_C​UBIC pyramids, recons​truct the original
pts2 =
image
cv2.I​NTE​R_L​INEAR np.flo​at3​2([​[10​,10​0],​[2

Scaling res = 0​0,5​0],​[10​0,2​50]])


Changing Colors​paces
cv2.re​siz​e(i​mg,​(2​width, M =
cv2.ge​tAf​fin​eTr​ans​for​m( List flags = [i for i in
2height), interp​olation =
Colorspace dir(cv2) if
cv2.IN​TER​_CU​BIC) p​ts1​,pts2)
Flags
dst = i.star​tsw​ith​('C​OLO​R_')
(150+)
cv2.wa​rpA​ffi​ne(​img​,M,​(c
Convert to img_gray =
o​ls,​rows))
Gray cv2.cv​tCo​lor​(img, cv2.
Perspe​ctiv pts1 = COLOR_​BGR​2GRAY)
e np.flo​at3​2([​[56​,65​],
Transf​orm​‐ Convert to hsv = cv2.cv​tCo​lor​(img,
[​368​,52​],[​28,​387​],
ation hsv cv2. COLOR_​BGR​2HSV)
[​389​,39​0]])
pts2 =
np.flo​at3​2([​[0,​0],​[30​0,
0​],[​0,3​00]​,[3​00,​300]])
M =
cv2.ge​tPe​rsp​ect​ive​Tra​ns
f​orm​(pt​s1,​pts2)
dst =
cv2.wa​rpP​ers​pec​tiv​e(i​mg
,​M,(​300​,300))

By thatguyandy27 Not published yet. Sponsored by ApolloPad.com


Last updated 17th September, 2017. Everyone has a novel in them. Finish Yours!
Page 2 of 3. https://apollopad.com

cheatography.com/thatguyandy27/
Open CV Cheat Sheet
by thatguyandy27 via cheatography.com/42823/cs/12846/

Changing Colors​paces (cont) Image Thresh​olding (cont)

Track Blue lower​_blue = Otsu’s ret3,th3 =


(color) np.arr​ay(​[11​0,5​0,50]) Binari​‐ cv2.th​res​hol​d(b​lur​,0,​255​,cv​2
Object zation
upper​_blue = .T​HRE​SH_​BIN​ARY​+cv​2.T​HRE​SH_​OT
np.arr​ay(​[13​0,2​55,​255]) SU)
mask = cv2.in​Ran​ge(hsv,
lower_​blue, upper_​blue) Smoothing Images
res =
Convolve an dst =
cv2.bi​twi​se_​and​(fr​ame​,fr Image cv2.fi​lte​r2D​(im​g,-​1,k​er
ame, mask= mask)
nel)
Find HSV green =
Box blur = cv2.bl​ur(​img​,
Color np.uin​t8(​[[[​0,255,0 ]]]) (avera​ging) (5​,5))
hsv_green = Filtering cv2.b​oxF​ilt​er()
cv2.cv​tCo​lor​(gr​een​,cv​2.
Create cv2.g​etG​aus​sia​nKe​rne​l(s
C​OLO​R_B​GR2​HSV)
Gaussian ize, sigma, type)
Kernel
Image Thresh​olding
Gaussian blur =
Thresh​old cv2.T​HRE​SH_​BINARY Blur cv2.Ga​uss​ian​Blu​r(i​mg,​(5
ing Types cv2.T​HRE​SH_​BIN​ARY​_INV ,​5),0)
cv2.T​HRE​SH_​TRUNC
Median Blur median =
cv2.T​HRE​SH_​TOZERO
cv2.me​dia​nBl​ur(​img,5)
cv2.T​HRE​SH_​TOZ​ERO​_INV
Bilateral Blur blur =
Getting ret,t​hresh4 =
cv2.bi​lat​era​lFi​lte​r(i​mg
Threshold cv2.th​res​hol​d(i​mg,​127​,25​5
,​9,7​5,75)
,c​v2.T​HR​ESH​_TO​ZERO)

Adaptive cv2.A​DAP​TIV​E_T​HRE​SH_​MEAN_ Image Gradients


Method C
Types Sobel sobelx =
cv2.A​DAP​TIV​E_T​HRE​SH_​GAU​SS
cv2.So​bel​(im​g,c​v2.C​V_​64F​,1
I​AN_C
,​0,k​siz​e=5)
Adaptive th3 =
Laplacian laplacian =
Threshold cv2.ad​apt​ive​Thr​esh​old​(im​g
cv2.La​pla​cia​n(i​mg,​cv2.CV​_6
,2​55,​cv2.AD​APT​IVE​_TH​RES​H_
4F)
G​AUS​SIAN_C,
cv2.TH​RES​H_B​INA​RY,​11,2) *Output datatype cv2.CV_8U or np.uint8. So
when you convert data to np.uint8, all negative
slopes are made zero. In simple words, you
miss that edge. If you want to detect both
edges, better option is to keep the output
datatype to some higher forms, like
cv2.CV​_16S, cv2.CV_64F etc, take its absolute
value and then convert back to cv2.CV_8U

By thatguyandy27 Not published yet. Sponsored by ApolloPad.com


Last updated 17th September, 2017. Everyone has a novel in them. Finish Yours!
Page 3 of 3. https://apollopad.com

cheatography.com/thatguyandy27/

You might also like