Numpy Numpy NP NP: Mylist (, ,)
Numpy Numpy NP NP: Mylist (, ,)
import numpy as np
In [7]:
mylist = [1,2,3]
In [8]:
type(mylist)
Out[8]:
list
In [9]:
type(mylist)
Out[9]:
list
In [11]:
myarray = np.array(mylist)
In [12]:
myarray
Out[12]:
array([1, 2, 3])
In [13]:
type(myarray)
Out[13]:
numpy.ndarray
In [15]:
np.arange(0,10,2)
Out[15]:
array([0, 2, 4, 6, 8])
In [17]:
np.ones(shape=(5,5))
Out[17]:
array([[1., 1., 1., 1., 1.],
[1., 1., 1., 1., 1.],
[1., 1., 1., 1., 1.],
[1., 1., 1., 1., 1.],
[1., 1., 1., 1., 1.]])
In [18]:
np.zeros(shape=(5,5))
Out[18]:
array([[0., 0., 0., 0., 0.],
array([[0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0.]])
In [ ]:
In [ ]:
In [27]:
np.random.seed(101)
arr = np.random.randint(0,100,10)
In [28]:
arr
Out[28]:
In [29]:
arr2 = np.random.randint(0,100,10)
In [30]:
arr2
Out[30]:
array([ 4, 63, 40, 60, 92, 64, 5, 12, 93, 40])
In [31]:
arr.max()
Out[31]:
95
In [32]:
arr.argmax()
Out[32]:
0
In [33]:
arr2.argmax()
Out[33]:
In [34]:
arr.argmin()
Out[34]:
7
In [35]:
arr.min()
arr.min()
Out[35]:
9
In [36]:
arr.mean()
Out[36]:
60.8
In [37]:
arr.shape
Out[37]:
(10,)
In [38]:
arr.reshape(2,5)
Out[38]:
array([[95, 11, 81, 70, 63],
[87, 75, 9, 77, 40]])
In [39]:
mat = np.arange(0,100).reshape(10,10)
In [40]:
mat
Out[40]:
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]])
In [42]:
mat.shape
Out[42]:
(10, 10)
In [44]:
mat[4,6]
Out[44]:
46
In [45]:
mat[:,3]
Out[45]:
array([ 3, 13, 23, 33, 43, 53, 63, 73, 83, 93])
In [46]:
mat[:,5].shape
Out[46]:
(10,)
In [49]:
mat[:,5].reshape(1,10)
Out[49]:
array([[ 5, 15, 25, 35, 45, 55, 65, 75, 85, 95]])
In [50]:
mat[:,5].reshape(10,1)
Out[50]:
array([[ 5],
[15],
[25],
[35],
[45],
[55],
[65],
[75],
[85],
[95]])
In [51]:
mat[0:5,0:5]
Out[51]:
array([[ 0, 1, 2, 3, 4],
[10, 11, 12, 13, 14],
[20, 21, 22, 23, 24],
[30, 31, 32, 33, 34],
[40, 41, 42, 43, 44]])
In [52]:
mat[4:7,5:9]
Out[52]:
array([[45, 46, 47, 48],
[55, 56, 57, 58],
[65, 66, 67, 68]])
In [76]:
mat[4:7,5:9]=0
In [77]:
mat
Out[77]:
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, 0, 0, 0, 0, 49],
[50, 51, 52, 53, 54, 0, 0, 0, 0, 59],
[60, 61, 62, 63, 64, 0, 0, 0, 0, 69],
[70, 71, 72, 73, 74, 75, 76, 77, 78, 79],
[80, 81, 82, 83, 84, 85, 86, 87, 88, 89],
[80, 81, 82, 83, 84, 85, 86, 87, 88, 89],
[90, 91, 92, 93, 94, 95, 96, 97, 98, 99]])
In [79]:
mat2 = mat.copy()
In [80]:
mat2
Out[80]:
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, 0, 0, 0, 0, 49],
[50, 51, 52, 53, 54, 0, 0, 0, 0, 59],
[60, 61, 62, 63, 64, 0, 0, 0, 0, 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]])
In [81]:
mat2[0:3,:]=99
In [82]:
mat2
Out[82]:
array([[99, 99, 99, 99, 99, 99, 99, 99, 99, 99],
[99, 99, 99, 99, 99, 99, 99, 99, 99, 99],
[99, 99, 99, 99, 99, 99, 99, 99, 99, 99],
[30, 31, 32, 33, 34, 35, 36, 37, 38, 39],
[40, 41, 42, 43, 44, 0, 0, 0, 0, 49],
[50, 51, 52, 53, 54, 0, 0, 0, 0, 59],
[60, 61, 62, 63, 64, 0, 0, 0, 0, 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]])
In [ ]:
In [ ]:
In [1]:
import numpy as np
In [3]:
In [4]:
In [6]:
pic = Image.open('Computer-Vision-with-Python/DATA/00-puppy.jpg')
In [9]:
type(pic)
Out[9]:
PIL.JpegImagePlugin.JpegImageFile
In [10]:
picarry = np.asarray(pic)
In [11]:
type(picarry)
Out[11]:
numpy.ndarray
In [12]:
picarry.shape
Out[12]:
(1300, 1950, 3)
In [13]:
plt.imshow(picarry)
Out[13]:
<matplotlib.image.AxesImage at 0x207f3062e88>
In [28]:
In [28]:
pic_red = picarry.copy()
pic_green = picarry.copy()
pic_blue = picarry.copy()
In [16]:
plt.imshow(pic_red)
Out[16]:
<matplotlib.image.AxesImage at 0x207f3207588>
In [ ]:
In [32]:
plt.imshow(pic_red[:,:,0],cmap = 'gray')
Out[32]:
<matplotlib.image.AxesImage at 0x207f9574f48>
In [34]:
# RED COLOR
pic_red[:,:,1]=0
pic_red[:,:,2]=0
#=================
# GREEN COLOR
pic_green[:,:,0]=0
pic_green[:,:,2]=0
#=================
# BLUE COLOR
pic_blue[:,:,0]=0
pic_blue[:,:,1]=0
In [27]:
In [27]:
plt.imshow(pic_red)
Out[27]:
<matplotlib.image.AxesImage at 0x207f6f9d508>
In [30]:
plt.imshow(pic_green)
Out[30]:
<matplotlib.image.AxesImage at 0x207f775a288>
In [31]:
plt.imshow(pic_blue)
Out[31]:
<matplotlib.image.AxesImage at 0x207f8db2948>
In [ ]:
In [2]:
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
In [3]:
import cv2
In [4]:
img = cv2.imread('D:/1234.jpg')
In [5]:
type(img)
Out[5]:
numpy.ndarray
In [6]:
plt.imshow(img)
Out[6]:
<matplotlib.image.AxesImage at 0x2343d4186d8>
In [7]:
img.shape
Out[7]:
(1148, 999, 3)
In [8]:
orig_img = cv2.cvtColor(img,cv2.COLOR_BGR2RGB)
In [9]:
plt.imshow(orig_img)
Out[9]:
<matplotlib.image.AxesImage at 0x2343d4ab978>
In [10]:
gray_img = cv2.imread('D:/1234.jpg',cv2.IMREAD_GRAYSCALE)
In [11]:
plt.imshow(gray_img)
Out[11]:
<matplotlib.image.AxesImage at 0x2343d506dd8>
In [12]:
plt.imshow(gray_img,cmap='gray')
Out[12]:
<matplotlib.image.AxesImage at 0x2343d6fcbe0>
In [13]:
new_img = cv2.resize(orig_img,(600,400))
In [14]:
plt.imshow(new_img)
Out[14]:
<matplotlib.image.AxesImage at 0x2343e7377b8>
In [15]:
w = 0.5
h = 0.2
In [16]:
new_img1 = cv2.resize(orig_img,(0,0),orig_img,w,h)
In [17]:
plt.imshow(new_img1)
Out[17]:
<matplotlib.image.AxesImage at 0x2343e79a4a8>
In [18]:
new_img2 = cv2.flip(orig_img,0)
plt.imshow(new_img2)
Out[18]:
<matplotlib.image.AxesImage at 0x2343e7eacf8>
In [19]:
new_img2 = cv2.flip(orig_img,1)
plt.imshow(new_img2)
Out[19]:
<matplotlib.image.AxesImage at 0x2343e840908>
In [20]:
new_img2 = cv2.flip(orig_img,-1)
plt.imshow(new_img2)
Out[20]:
<matplotlib.image.AxesImage at 0x2343e8985c0>
In [21]:
cv2.imwrite('D:/123.jpg',new_img2)
Out[21]:
True
In [22]:
fig = plt.figure(figsize=(10,10))
ax = fig.add_subplot(111)
ax.imshow(img)
Out[22]:
<matplotlib.image.AxesImage at 0x2343e8f0438>
In [ ]:
In [1]:
import cv2
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
In [2]:
blank_img = np.zeros(shape=(512,512,3),dtype=np.int16)
In [3]:
#plt.imshow(blank_img)
blank_img = cv2.imread('D:/1234.jpg')
In [4]:
cv2.rectangle(blank_img,pt1=(200,300),pt2=(700,800),color=(0,0,255),thickness=10)
plt.imshow(blank_img)
Out[4]:
<matplotlib.image.AxesImage at 0x20fbc8c4f60>
In [5]:
circ_img = cv2.imread('D:/1234.jpg')
In [6]:
cv2.circle(circ_img,center=(450,550),radius=300,color=(0,0,255),thickness=10)
plt.imshow(circ_img)
Out[6]:
<matplotlib.image.AxesImage at 0x20fbccbbb38>
In [7]:
fil_circ_img = cv2.imread('D:/1234.jpg')
In [24]:
cv2.circle(fil_circ_img,center=(450,550),radius=300,color=(0,0,255),thickness=-1)
plt.imshow(fil_circ_img)
Out[24]:
<matplotlib.image.AxesImage at 0x20fbdf6ab38>
In [9]:
lin_img = cv2.imread('D:/1234.jpg')
In [10]:
cv2.line(lin_img,pt1=(0,0),pt2=(1000,1148),color=(255, 0, 0),thickness=50)
cv2.line(lin_img,pt1=(999,0),pt2=(0,1148),color=(255, 0, 0),thickness=50)
plt.imshow(lin_img)
Out[10]:
<matplotlib.image.AxesImage at 0x20fbde67390>
In [11]:
lin_img.shape
Out[11]:
(1148, 999, 3)
In [12]:
txt_img = cv2.imread('D:/1234.jpg')
In [25]:
font = cv2.FONT_HERSHEY_SIMPLEX
cv2.putText(txt_img,text='Hello',org=(150,800), fontFace=font,fontScale= 10,color=(255,0
,0),thickness=20,lineType=cv2.LINE_AA)
plt.imshow(txt_img)
Out[25]:
<matplotlib.image.AxesImage at 0x20fbdfc1a20>
In [145]:
poly_img = cv2.imread('D:/1234.jpg')
In [146]:
vertices = np.array([[200,550],[400,250],[700,550],[400,850]],np.int32)
pts = vertices.reshape((-1,1,2))
In [147]:
cv2.polylines(poly_img,[pts],isClosed=True,color=(255,0,0),thickness=5)
plt.imshow(poly_img)
Out[147]:
<matplotlib.image.AxesImage at 0x20fc5bfc208>
In [ ]:
In [ ]:
In [4]:
import cv2
import numpy as np
def draw_circle(event,x,y,flags,param):
if event == cv2.EVENT_LBUTTONDOWN:
cv2.circle(img,(x,y),100,(222,111,0),-1)
elif event == cv2.EVENT_RBUTTONDOWN:
cv2.circle(img,(x,y),100,(0,111,222),-1)
img = cv2.imread('D:/1234.jpg')
cv2.namedWindow(winname='my_drawing')
cv2.setMouseCallback('my_drawing',draw_circle)
while True:
cv2.imshow('my_drawing',img)
if cv2.waitKey(20) & 0xFF == 27:
break
cv2.destroyAllWindows()
In [2]:
In [ ]:
In [11]:
import cv2
import numpy as np
drawing = False
ix,iy = -1,-1
def draw_rectangle(event,x,y,flags,param):
global ix,iy,drawing,mode
if event == cv2.EVENT_LBUTTONDOWN:
drawing = True
ix,iy = x,y
img = cv2.imread('D:/1234.jpg')
cv2.namedWindow(winname='my_drawing')
cv2.setMouseCallback('my_drawing',draw_rectangle)
while True:
cv2.imshow('my_drawing',img)
if cv2.waitKey(1) & 0xFF == 27:
break
cv2.destroyAllWindows()
In [ ]:
In [ ]:
In [1]:
import cv2
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
In [15]:
img = cv2.imread('D:/1234.jpg')
plt.imshow(img)
Out[15]:
<matplotlib.image.AxesImage at 0x29446dc1b70>
In [16]:
img = cv2.cvtColor(img,cv2.COLOR_BGR2RGB)
plt.imshow(img)
Out[16]:
<matplotlib.image.AxesImage at 0x29446e19898>
In [17]:
img = cv2.cvtColor(img,cv2.COLOR_RGB2HSV)
plt.imshow(img)
Out[17]:
<matplotlib.image.AxesImage at 0x29446e70470>
In [ ]:
In [1]:
import cv2
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
In [2]:
img1 = cv2.imread('D:/1234.jpg')
img1 = cv2.cvtColor(img1,cv2.COLOR_BGR2RGB)
plt.imshow(img1)
Out[2]:
<matplotlib.image.AxesImage at 0x298ff4147b8>
In [3]:
img2 = cv2.imread('D:/tt.png')
img2 = cv2.cvtColor(img2,cv2.COLOR_BGR2RGB)
plt.imshow(img2)
Out[3]:
<matplotlib.image.AxesImage at 0x298ff7f99b0>
In [4]:
img1 = cv2.resize(img1,(1200,1600))
img2 = cv2.resize(img2,(1200,1600))
In [5]:
blended = cv2.addWeighted(src1=img1,alpha=0.6,src2=img2,beta=0.2,gamma=0.3)
plt.imshow(blended)
Out[5]:
<matplotlib.image.AxesImage at 0x298ff85c470>
In [ ]:
In [ ]:
Blending and Pasting Images
For some computer vision systems, we'll want to be able to post our own image on top of an already existing
image or video. We may also want to blend images, maybe we want to have a "highlight" effect instead of just a
solid box or empty rectangle.
Let's explore what is commonly known as Arithmetic Image Operations with OpenCV. These are referred to as
Arithmetic Operations because OpenCV is simply performing some common math with the pixels for the final
effect. We'll see a bit of this in our code.
Blending Images
Blending Images is actually quite simple, let's look at a simple example.
In [179]:
import cv2
In [180]:
# Two images
img1 = cv2.imread('D:/1234.jpg')
img2 = cv2.imread('D:/tt.png')
In [181]:
img1.shape
Out[181]:
(1148, 999, 3)
In [182]:
img2.shape
Out[182]:
(512, 512, 3)
In [183]:
import matplotlib.pyplot as plt
%matplotlib inline
In [184]:
plt.imshow(img1)
Out[184]:
<matplotlib.image.AxesImage at 0x1cc1c5e0cf8>
Whoops! Let's remember to fix the RGB!
In [185]:
img1 = cv2.cvtColor(img1, cv2.COLOR_BGR2RGB)
img2 = cv2.cvtColor(img2, cv2.COLOR_BGR2RGB)
In [186]:
plt.imshow(img1)
Out[186]:
<matplotlib.image.AxesImage at 0x1cc1c63c390>
In [187]:
plt.imshow(img2)
Out[187]:
<matplotlib.image.AxesImage at 0x1cc1c695160>
In [188]:
img1 =cv2.resize(img1,(1200,1200))
img2 =cv2.resize(img2,(1200,1200))
Let's practice resizing the image, since the DO NOT COPY image is actually quite large 1200 by 1200, and our
puppy in backpack image is 1400 by 1000
In [189]:
In [189]:
plt.imshow(img1)
Out[189]:
<matplotlib.image.AxesImage at 0x1cc1c6ec780>
In [190]:
plt.imshow(img2)
Out[190]:
<matplotlib.image.AxesImage at 0x1cc1c752320>
In [191]:
img1.shape
Out[191]:
(1200, 1200, 3)
In [192]:
img2.shape
Out[192]:
(1200, 1200, 3)
In [193]:
blended = cv2.addWeighted(src1=img1,alpha=0.7,src2=img2,beta=0.3,gamma=0)
In [194]:
plt.imshow(blended)
Out[194]:
<matplotlib.image.AxesImage at 0x1cc1e0690b8>
In [195]:
large_img = img1
small_img = img2
In [196]:
x_offset=0
y_offset=0
In [197]:
large_img[y_offset:y_offset+small_img.shape[0], x_offset:x_offset+small_img.shape[1]] = s
mall_img
In [198]:
plt.imshow(large_img)
Out[198]:
<matplotlib.image.AxesImage at 0x1cc1e781978>
Blending Images of Different Sizes
Imports
In [199]:
import numpy as np
import cv2
In [200]:
# Load two images
img1 = cv2.imread('D:/1234.jpg')
img2 = cv2.imread('D:/tt.png')
img2 =cv2.resize(img2,(600,600))
In [201]:
plt.imshow(img1)
Out[201]:
<matplotlib.image.AxesImage at 0x1cc1e7d2da0>
In [202]:
plt.imshow(img2)
Out[202]:
<matplotlib.image.AxesImage at 0x1cc1e82ae10>
Create a Region of Interest (ROI)
In [203]:
img1.shape
Out[203]:
(1148, 999, 3)
In [204]:
x_offset=150
y_offset=300
In [205]:
# Creating an ROI of the same size of the foreground image (smaller image that will go on
top)
rows,cols,channels = img2.shape
# roi = img1[0:rows, 0:cols ] # TOP LEFT CORNER
roi = img1[y_offset:900,x_offset:750] # BOTTOM RIGHT CORNER
In [206]:
plt.imshow(roi)
Out[206]:
<matplotlib.image.AxesImage at 0x1cc1e0c4908>
In [207]:
roi.shape
Out[207]:
(600, 600, 3)
Creating a Mask
In [208]:
# Now create a mask of logo and create its inverse mask also
img2gray = cv2.cvtColor(img2,cv2.COLOR_BGR2GRAY)
In [209]:
img2gray.shape
Out[209]:
(600, 600)
In [210]:
plt.imshow(img2gray,cmap='gray')
Out[210]:
<matplotlib.image.AxesImage at 0x1cc1e2456d8>
In [211]:
mask_inv = cv2.bitwise_not(img2gray)
In [212]:
mask_inv.shape
Out[212]:
(600, 600)
In [213]:
plt.imshow(mask_inv,cmap='gray')
Out[213]:
<matplotlib.image.AxesImage at 0x1cc1e2a30b8>
Convert Mask to have 3 channels
In [214]:
white_background = np.full(img2.shape, 255, dtype=np.uint64)
In [215]:
bk = cv2.bitwise_or(white_background, white_background, mask=mask_inv)
In [216]:
bk.shape
Out[216]:
(600, 600, 3)
In [217]:
plt.imshow(bk)
Out[217]:
<matplotlib.image.AxesImage at 0x1cc1e2fac88>
In [218]:
plt.imshow(mask_inv,cmap='gray')
Out[218]:
<matplotlib.image.AxesImage at 0x1cc1e8bb390>
In [219]:
In [219]:
fg = cv2.bitwise_or(img2, img2, mask=mask_inv)
In [220]:
plt.imshow(fg)
Out[220]:
<matplotlib.image.AxesImage at 0x1cc1e912898>
In [221]:
fg.shape
Out[221]:
(600, 600, 3)
In [222]:
final_roi = cv2.bitwise_or(roi,fg)
In [223]:
plt.imshow(final_roi)
Out[223]:
<matplotlib.image.AxesImage at 0x1cc1dab3240>
In [224]:
large_img = img1
small_img = final_roi
large_img[y_offset:y_offset+small_img.shape[0], x_offset:x_offset+small_img.shape[1]] = s
mall_img
plt.imshow(large_img)
Out[224]:
<matplotlib.image.AxesImage at 0x1cc1db08e10>
Great Work!
Check out these documentation examples and links for more help for these kinds of tasks (which can be really
tricky!)
1. https://stackoverflow.com/questions/10469235/opencv-apply-mask-to-a-color-image/38493075
2. https://stackoverflow.com/questions/14063070/overlay-a-smaller-image-on-a-larger-image-python-opencv
3. https://docs.opencv.org/3.4/d0/d86/tutorial_py_image_arithmetics.html