imread
under Python returns a NumPy arrayshape
atribute keeps the array's dimensionslenna = cv2.imread('data/lenna.tiff', cv2.IMREAD_GRAYSCALE)
lenna.shape
(512, 512)
uint8
arrayslenna.dtype
dtype('uint8')
lenna
array([[162, 162, 162, ..., 170, 155, 128], [162, 162, 162, ..., 170, 155, 128], [162, 162, 162, ..., 170, 155, 128], ..., [ 43, 43, 50, ..., 104, 100, 98], [ 44, 44, 55, ..., 104, 105, 108], [ 44, 44, 55, ..., 104, 105, 108]], dtype=uint8)
lenna[0,0]
162
plt.imshow(lenna, cmap=plt.cm.gray)
plt.colorbar()
<matplotlib.colorbar.Colorbar at 0x7f1fd469b510>
uint8
arraymandrill = cv2.imread('data/mandrill.tiff')
mandrill.shape
(512, 512, 3)
mandrill.dtype
dtype('uint8')
mandrill[2,3]
array([29, 46, 54], dtype=uint8)
imread
returns color images in BGR ordermandrill[2,3,1]
46
plt.imshow(mandrill)
<matplotlib.image.AxesImage at 0x7f1fd45c4590>
mandrill_rgb = cv2.cvtColor(mandrill, cv2.COLOR_BGR2RGB)
plt.imshow(mandrill_rgb)
<matplotlib.image.AxesImage at 0x7f1fd4478190>
sobel = cv2.Sobel(lenna, cv2.CV_16SC1, 1, 1)
plt.imshow(sobel, cmap=plt.cm.viridis)
plt.colorbar()
<matplotlib.colorbar.Colorbar at 0x7f1fd429b590>
sobel[100,100]
-8
import numpy as np
thermo = np.loadtxt('data/thermo-maize.csv', delimiter=';')
plt.title(r'Thermography image (float32)')
plt.imshow(thermo, cmap=plt.cm.hot)
plt.colorbar()
<matplotlib.colorbar.Colorbar at 0x7f1fd4162290>
A = np.arange(100).reshape(10,10)
A
array([[ 0, 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]])
A[3:10:2,5:9]
array([[35, 36, 37, 38], [55, 56, 57, 58], [75, 76, 77, 78], [95, 96, 97, 98]])
A NumPy array can also be indexed by masks, defined as boolean or integer arrays. This approach is frequently called fancy indexing. In this example, a boolean mask is produced applying a logical operation on an array.
lenna > 128
array([[ True, True, True, ..., True, True, False], [ True, True, True, ..., True, True, False], [ True, True, True, ..., True, True, False], ..., [False, False, False, ..., False, False, False], [False, False, False, ..., False, False, False], [False, False, False, ..., False, False, False]], dtype=bool)
res = np.zeros_like(lenna)
res[lenna > 128] = 255
plt.subplot(1,2,1)
plt.imshow(lenna, cmap=plt.cm.gray)
plt.subplot(1,2,2)
plt.imshow(res, cmap=plt.cm.binary_r)
<matplotlib.image.AxesImage at 0x7f1fcc6ee790>
I = cv2.imread('data/girl.jpg', cv2.IMREAD_GRAYSCALE)
plt.imshow(I, cmap=plt.cm.gray, vmin=0, vmax=255)
plt.colorbar()
<matplotlib.colorbar.Colorbar at 0x7f1fcc3fc9d0>
mu_I = np.mean(I)
sigma_I = np.std(I)
W_I = (I - mu_I)/sigma_I
delta_W = W_I.max() - W_I.min()
W_uint8 = np.array(255./delta_W * (W_I - W_I.min()), dtype=np.uint8)
plt.imshow(W_uint8, cmap=plt.cm.gray, vmin=0, vmax=255)
plt.colorbar()
<matplotlib.colorbar.Colorbar at 0x7f1fcc1f6650>
A = np.arange(25).reshape(5,-1)
A
array([[ 0, 1, 2, 3, 4], [ 5, 6, 7, 8, 9], [10, 11, 12, 13, 14], [15, 16, 17, 18, 19], [20, 21, 22, 23, 24]])
B = A[0:3,0:3]
B
array([[ 0, 1, 2], [ 5, 6, 7], [10, 11, 12]])
B[0,0] = 255
B
array([[255, 1, 2], [ 5, 6, 7], [ 10, 11, 12]])
A
array([[255, 1, 2, 3, 4], [ 5, 6, 7, 8, 9], [ 10, 11, 12, 13, 14], [ 15, 16, 17, 18, 19], [ 20, 21, 22, 23, 24]])
B = A[0:3,0:3].copy()
B[0,0] = 128
B
array([[128, 1, 2], [ 5, 6, 7], [ 10, 11, 12]])
A
array([[255, 1, 2, 3, 4], [ 5, 6, 7, 8, 9], [ 10, 11, 12, 13, 14], [ 15, 16, 17, 18, 19], [ 20, 21, 22, 23, 24]])
Consider the UCI ML hand-written digits datasets
from sklearn import datasets
digits = datasets.load_digits()
digits['data'].shape
(1797, 64)
x = digits['data'][0]
x
array([ 0., 0., 5., 13., 9., 1., 0., 0., 0., 0., 13., 15., 10., 15., 5., 0., 0., 3., 15., 2., 0., 11., 8., 0., 0., 4., 12., 0., 0., 8., 8., 0., 0., 5., 8., 0., 0., 9., 8., 0., 0., 4., 11., 0., 1., 12., 7., 0., 0., 2., 14., 5., 10., 12., 0., 0., 0., 0., 6., 13., 10., 0., 0., 0.])
plt.imshow(digits['data'][0:30,:], interpolation='nearest', cmap=plt.cm.gray)
plt.xlabel('Dimensions')
plt.ylabel('Digit sample')
<matplotlib.text.Text at 0x7f1fa4116dd0>
X = x.reshape(8,8)
X
array([[ 0., 0., 5., 13., 9., 1., 0., 0.], [ 0., 0., 13., 15., 10., 15., 5., 0.], [ 0., 3., 15., 2., 0., 11., 8., 0.], [ 0., 4., 12., 0., 0., 8., 8., 0.], [ 0., 5., 8., 0., 0., 9., 8., 0.], [ 0., 4., 11., 0., 1., 12., 7., 0.], [ 0., 2., 14., 5., 10., 12., 0., 0.], [ 0., 0., 6., 13., 10., 0., 0., 0.]])
plt.imshow(X, interpolation='nearest', cmap=plt.cm.gray)
<matplotlib.image.AxesImage at 0x7f1f9f6d39d0>
x = X.reshape(-1)
x
array([ 0., 0., 5., 13., 9., 1., 0., 0., 0., 0., 13., 15., 10., 15., 5., 0., 0., 3., 15., 2., 0., 11., 8., 0., 0., 4., 12., 0., 0., 8., 8., 0., 0., 5., 8., 0., 0., 9., 8., 0., 0., 4., 11., 0., 1., 12., 7., 0., 0., 2., 14., 5., 10., 12., 0., 0., 0., 0., 6., 13., 10., 0., 0., 0.])