In [1]:
2+2
Out[1]:
4
In [2]:
import cv2
In [3]:
pwd
Out[3]:
'/home/ringland/public_html/448'
In [4]:
imagename = '20190826_142952.jpg'
im = cv2.imread(imagename)
In [5]:
cv2.imwrite('foo.jpg',im)
Out[5]:
True
In [6]:
type(im)
Out[6]:
numpy.ndarray
In [7]:
im.shape
Out[7]:
(2268, 4032, 3)
In [8]:
im[300,271,1]
Out[8]:
208
In [9]:
type(im[300,271,1])
Out[9]:
numpy.uint8
In [10]:
im.dtype
Out[10]:
dtype('uint8')

Slicing

start:stop:step any can be omitted :stop means 0:stop start: means start to the end : means all

In [11]:
cv2.imwrite('foo.jpg',im[:1000,:,:])
Out[11]:
True
In [12]:
# Let's turn one layer off using broadcasting
im[:,:,1] = 0
cv2.imwrite('foo.jpg',im)
Out[12]:
True
In [13]:
# Let's turn one layer off using broadcasting
#im[:,:,1] = 0
#im[:,:,0] = 0
im[:,:,[0,1]] = 0  # leaving only the third color layer
cv2.imwrite('foo.jpg',im)
Out[13]:
True
In [15]:
im = cv2.imread(imagename)
im[:,:,[1,2]] = 0  # leaving only the third color layer
cv2.imwrite('foo.jpg',im)
Out[15]:
True
In [16]:
import numpy as np
In [19]:
a = np.random.randint(0,10,(5,4))
a
Out[19]:
array([[9, 7, 7, 3],
       [1, 7, 5, 3],
       [1, 4, 6, 9],
       [4, 3, 8, 1],
       [8, 2, 6, 9]])
In [20]:
a>5
Out[20]:
array([[ True,  True,  True, False],
       [False,  True, False, False],
       [False, False,  True,  True],
       [False, False,  True, False],
       [ True, False,  True,  True]])
In [21]:
a[ a>5 ] = -1
a
Out[21]:
array([[-1, -1, -1,  3],
       [ 1, -1,  5,  3],
       [ 1,  4, -1, -1],
       [ 4,  3, -1,  1],
       [-1,  2, -1, -1]])
In [ ]:
a[ isgreen ] = b[ isgreen ]
In [27]:
cv2.imwrite('foo.jpg',im.sum(axis=2)/3 )
Out[27]:
True
In [22]:
im.sum(axis=2).shape
Out[22]:
(2268, 4032)
In [24]:
type(im.sum(axis=2))
Out[24]:
numpy.ndarray
In [25]:
b = np.random.randint(0,10,(3,4,2))
b
Out[25]:
array([[[9, 1],
        [2, 8],
        [3, 1],
        [6, 4]],

       [[5, 9],
        [0, 0],
        [7, 3],
        [6, 2]],

       [[0, 7],
        [4, 0],
        [4, 6],
        [2, 0]]])
In [26]:
b.sum(axis=2)
Out[26]:
array([[10, 10,  4, 10],
       [14,  0, 10,  8],
       [ 7,  4, 10,  2]])
In [39]:
im = cv2.imread(imagename)
isgreen = im[:,:,1]/im.sum(axis=2) > .4
isgreen
/home/ringland/anaconda3/lib/python3.7/site-packages/ipykernel_launcher.py:2: RuntimeWarning: divide by zero encountered in true_divide
  
/home/ringland/anaconda3/lib/python3.7/site-packages/ipykernel_launcher.py:2: RuntimeWarning: invalid value encountered in true_divide
  
/home/ringland/anaconda3/lib/python3.7/site-packages/ipykernel_launcher.py:2: RuntimeWarning: invalid value encountered in greater
  
Out[39]:
True
In [35]:
isgreen.sum()
Out[35]:
5101971
In [ ]:
im[ isgreen ] = otherim [ isgreen ]