forked from justmarkham/python-reference
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathopencv_reference.py
More file actions
149 lines (109 loc) · 5.21 KB
/
opencv_reference.py
File metadata and controls
149 lines (109 loc) · 5.21 KB
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
'''
OpenCV Quick Reference
https://github.com/justmarkham/python-reference
By Stein Castillo (kevin@dataschool.io)
http://www.dataschool.io
Table of Contents:
Imports
Basic Image Operations
Image Manipulation
'''
### IMPORTS ###
#Generic imports
import cv2
import cv2 as cv # I use this import to make the calls easier.
import numpy as np
#Import matplotlib to display images
from matplotlib import pyplot as plt
#Extended mahotas functionality
import mahotas
import mahotas as mh #typical import
#Import if command line arguments are required
import argparse
### BASIC IMAGE OPERATIONS ###
#Load an image
image = cv2.imread(image)
#Load an image and convert to grayscale
image = cv2.imread(image, 0)
image = cv2.imread(image, cv2.IMREAD_GRAYSCALE) #both commands do the same thing
#Determine image properties
(h, w, c) = image.shape #h: Height, w: Width, c: Channels
size = image.size #Number of pixels
imgtype = image.dtype #Image type
max = image.max() #Maximum value
min = image.min() #Minimum value
#Display an image with openCV
cv2.namedWindow(window_name, property) #creates a window with specific properties:
#cv2.WINDOW_NORMAL :User can resize the window (no constrain)
#cv2.WINDOW_AUTOSIZE: Window size is automatically adjusted
#cv2.WINDOW_OPENGL: Window created with OPENGL support
cv2.imshow(window_name, image) #display image, automatic window properties
cv2.destroyWindow(window_name) #Closes specified window
cv2.destroyAllWindows() #Close all opencv windows
cv2.waitKey() #Display the windows and wait for user keypress
key = cv.waitKey(0) &0xFF #Display the windows and store keypress
#Display an image with MATPLOTLIB
img = cv2.imread(image, 0)
plt.imshow(img, cmap = 'gray', interpolation = 'bicubic')
plt.xticks([]), plt.yticks([]) # to hide tick values on X and Y axis
plt.show()
###IMAGE MANIPULATION###
#convert image to grayscale
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
#Blur image
#Gaussian blur
#Very effective to remove gaussian noise in an image
#It is recommended to convert the image to grayscale before applying the blur filter.
blurred = cv2.GaussianBlur(image, (5, 5), 0) #image: source image
#(5, 5): Kernel size, must be positive and odd
#Average blur
#Used to smooth an image applying normalized box filter
#Average filter. takes the average of all the pixels under the kernel
blurred = cv2.blur(image, (5,5)) #image: Source image
#(5,5): Kernel size, must be positive and odd
#Median blur
#Used to remove "salt and pepper" noise from an image
#Computes the median of all the pixels under the kernel
blurred = cv2.medianBlur(image, 5) #image: Source image
#5: Kernel size, most be positive and odd
#Applyng Gaussian blur filter with Mahotas
blurred = mahotas.gaussian_filter(image, 8) #imageö Source image
#8: standard deviation for Gaussian kernel (in pixel units)
# Thresholding an image
""" Thresholding is the binarization of an image:
any value over an index (C) is set to 255 (White)
any value under the index (C) is set ot 0 (black)
inverse binarizacion is also possible:
any value over the index (C) is the to 0 (black)
any value under the index (C) is set to 255 (white)"""
# Simple thresholding
# requires user intervetion to provide the index (C) value
(T, thresh) = cv2.threshold(src, C, 255, cv2.THRESH_BINARY) #src: source image
#C: index (as explained above)
#255: max value to assign if value greater than C
#Method: cv2.THRESH_BINARY
# cv2.THRESH_BINARY_INV
#Returns: T: max value
# thresh: Thresholded image
# Examples of simple thresholding
(T, thresh) = cv2.threshold(blurred, 155, 255, cv2.THRESH_BINARY)
(T, thresh) = cv2.threshold(blurred, 155, 255, cv2.THRESH_BINARY_INV)
# Resize an image
image = cv.resize(image, None, fx = 0.5, fy = 0.5, interpolation = cv.INTER_AREA)
"""
fx: set the resizing factor on x axis. In this example shrinks the image 50%
fy: set the resizing factor on y axis. In this example shrinks the image 50%
interpolation values:
cv.INTER_AREA : Used for image shrinking
cv.INTER_LINEAR: Used for zooming - Also used as default for all resizing purposes
cv.INTER_CUBIC: Also used for shrinking but is slower
"""
# Change contrast and brightness of an image
result = cv.addWeighted(image, alpha, np.zeros(image.shape, image.dtype), beta, gamma)
"""
result = (image * alpha) + (np.zeros * beta) + gamma
gamma: changes the brightness of the image
alpha: changes the contrast of the image
beta: Does nothing since it is multipied to an array of zeros
alpha = 0.5, beta = 120 : X-ray effect
"""