python网课笔记_《OpenCV图像处理(Python)》网课笔记(从5到8)

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

be34cecfbff81c8906df16514b9f1975.png

mainsvmimage.png

504debd5442816c312088ca9b64030ef.png

mainlogo.png

f0bfabdefd90d1d6b5cb204b3f446727.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

04f1db959fc322be608e0f88de54e337.png

lesson6-threshold.png

e73af14869f8280a2c8b8a8a1dc1e58e.png

lesson6-threshold2.png

faac1c25884a3cfe2a78724b2613c47e.png

lesson6-gaus.png

22dfd5b86978777e3c9320db6c6b3c74.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()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值