博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
python网课笔记_《OpenCV图像处理(Python)》网课笔记(从5到8)
阅读量:7182 次
发布时间:2019-06-29

本文共 3522 字,大约阅读时间需要 11 分钟。

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()

转载地址:http://ejazm.baihongyu.com/

你可能感兴趣的文章
Linux上安装Bugzilla4.4小记
查看>>
SSIM(结构相似度算法)不同实现版本的差异
查看>>
excel字符处理函数
查看>>
方法method
查看>>
python 回溯法 子集树模板 系列 —— 12、选排问题
查看>>
ArcGIS Engine中空间参照(地理坐标)相关方法总结
查看>>
关于CMS后台展示/操作方式的个人拙见
查看>>
git常用命令
查看>>
ubuntu安装字体
查看>>
为什么要在定义抽象类时使用abstract关键字
查看>>
任意进制转换算法
查看>>
01.Web大前端时代之:HTML5+CSS3入门系列~初识HTML5
查看>>
libv4l 库【转】
查看>>
非常重要:2010 Autodesk 开发者日北京会场地址变更!
查看>>
Elasticsearch集群状态健康值处于red状态问题分析与解决(图文详解)
查看>>
GNU make manual 翻译(十六)
查看>>
详谈如何定制自己的博客园皮肤【转】
查看>>
Maven 手动添加第三方依赖包及编译打包和java命令行编译JAVA文件并使用jar命令打包...
查看>>
关于递归方法的实现
查看>>
10月份推荐android开发职位#安卓巴士公益推荐工作#
查看>>