5. image arithmetics and logic (图像算术和逻辑)
# coding: utf-8
# In[10]:
#lesson5
# In[11]:
import cv2
import numpy as np
# In[12]:
img1 = cv2.imread('3D-Matplotlib.png')
img2 = cv2.imread('mainsvmimage.png')
# In[14]:
# add = img1 + img2
# add = cv2.add(img1//2,img2//2)
weighted = cv2. addWeighted(img1,0.6,img2,0.4,0)
cv2.imwrite('lesson5-weighted.png',weighted)
cv2.imshow('weighted',weighted)
cv2.waitKey(0)
cv2.destroyAllWindows()
# In[15]:
img3 = cv2.imread('mainlogo.png')
# In[16]:
rows,cols,channels = img3.shape
roi = img1[0:rows,0:cols]
img3gray = cv2.cvtColor(img3,cv2.COLOR_BGR2GRAY)
ret, mask = cv2.threshold(img3gray,220,255,cv2.THRESH_BINARY_INV)
# In[17]:
mask_inv = cv2.bitwise_not(mask)
img1_bg = cv2.bitwise_and(roi,roi,mask = mask_inv)
img3_fg = cv2.bitwise_and(img3,img3,mask = mask)
# In[18]:
dst = cv2.add(img1_bg,img3_fg)
img1[0:rows,0:cols] = dst
# In[19]:
cv2.imshow('res',img1)
cv2.imshow('mask_inv',mask_inv)
cv2.imshow('img1_bg',img1_bg)
cv2.imshow('img3_fg',img3_fg)
cv2.imshow('dst',dst)
# In[20]:
# cv2.imshow('mask',mask)
cv2.imwrite('lesson5-dst.png',dst)
cv2.waitKey(0)
cv2.destroyAllWindows()
其中输入里面,
3D-Matplotlib.png
mainsvmimage.png
mainlogo.png
输出结果里面
6. thresholding(阈值)
代码部分
# coding: utf-8
# In[5]:
#leeson6
# In[13]:
import cv2
import numpy as np
# In[14]:
img = cv2.imread('bookpage.jpg')
retval, threshold = cv2.threshold(img,12,255,cv2.THRESH_BINARY)
# In[15]:
grayscaled = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
retval2, threshold2 = cv2.threshold(grayscaled,12,255,cv2.THRESH_BINARY)
# In[16]:
gaus = cv2.adaptiveThreshold(grayscaled,255,cv2.ADAPTIVE_THRESH_GAUSSIAN_C,cv2.THRESH_BINARY,115,1)
# In[18]:
cv2.imwrite('lesson6-original.png',img)
cv2.imwrite('lesson6-threshold.png',threshold)
cv2.imwrite('lesson6-threshold2.png',threshold2)
cv2.imwrite('lesson6-gaus.png',gaus)
# In[19]:
cv2.imshow('original',img)
cv2.imshow('threshold',threshold)
cv2.imshow('threshold2',threshold2)
cv2.imshow('gaus',gaus)
cv2.waitKey(0)
cv2.destroyAllWindows()
lesson6-original.png
lesson6-threshold.png
lesson6-threshold2.png
lesson6-gaus.png
7. Color Filtering(颜色过滤器)
代码
# coding: utf-8
# In[1]:
#lesson7
# In[2]:
import cv2
import numpy as np
# In[3]:
cap = cv2.VideoCapture(0)
# In[4]:
while True:
_, frame = cap.read()
hsv = cv2.cvtColor(frame,cv2.COLOR_BGR2HSV)
lower_red = np.array([50,0,0])
upper_red = np.array([255,255,255])
#Evidently, here mask is the same as hsv
mask = cv2.inRange(hsv,lower_red,upper_red)
res = cv2.bitwise_and(frame,frame,mask = mask)
cv2.imshow('frame',frame)
cv2.imshow('mask',mask)
cv2.imshow('res',res)
if cv2.waitKey(2) & 0xFF == ord('q'):
break
cv2.destroyAllWindows()
cap.release()
8. Blurring and Smoothing (模糊和光滑)
代码
# coding: utf-8
# In[1]:
#lesson8
# In[19]:
import cv2
import numpy as np
# In[22]:
cap = cv2.VideoCapture(0)
# In[23]:
while True:
_, frame = cap.read()
hsv = cv2.cvtColor(frame,cv2.COLOR_BGR2HSV)
lower_red = np.array([50,0,0])
upper_red = np.array([255,255,255])
#Evidently, here mask is the same as hsv
mask = cv2.inRange(hsv,lower_red,upper_red)
res = cv2.bitwise_and(frame,frame,mask = mask)
kernel = np.ones((15,15),np.float32)/225
smoothed = cv2.filter2D(res,-1,kernel)
blur = cv2.GaussianBlur(res,(15,15),0)
median = cv2.medianBlur(res,15)
bilateral = cv2.bilateralFilter(res,15,75,75)
cv2.imshow('frame',frame)
# cv2.imshow('mask',mask)
cv2.imshow('res',res)
cv2.imshow('blur',blur)
cv2.imshow('median',median)
cv2.imshow('bilateral',bilateral)
if cv2.waitKey(3) & 0xFF == ord('q'):
break
cv2.destroyAllWindows()
cap.release()