《OpenCV图像处理(Python)》网课笔记(从9到12)

9. morphological tranformations (形态变换)


# coding: utf-8

# In[5]:


#lesson9


# In[6]:


import cv2
import numpy as np


# In[7]:


cap = cv2.VideoCapture(0)


# In[8]:


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((5,5),np.uint8)
    erosion = cv2.erode(mask,kernel,iterations = 1)
    dilation = cv2.dilate(mask,kernel,iterations = 1)
    
    opening = cv2.morphologyEx(mask,cv2.MORPH_OPEN,kernel)
    close = cv2.morphologyEx(mask,cv2.MORPH_CLOSE,kernel)    
    
    
    cv2.imshow('frame',frame)
#     cv2.imshow('mask',mask)
    cv2.imshow('res',res)
    cv2.imshow('erosion',erosion)
    cv2.imshow('dilation',dilation)
    cv2.imshow('opening',opening)
    cv2.imshow('close',close)
    
    
    if cv2.waitKey(3) & 0xFF == ord('q'):
        break

cv2.destroyAllWindows()
cap.release()
    
    

10. edge detections and gradients (边缘检测和梯度)


# coding: utf-8

# In[4]:


#lesson10


# In[5]:


import cv2
import numpy as np


# In[6]:


cap = cv2.VideoCapture(0)


# In[7]:


while True:
    _ , frame = cap.read()
    
    laplacian = cv2.Laplacian(frame,cv2.CV_64F)
    sobelx = cv2.Sobel(frame,cv2.CV_64F,1,0,ksize=5)
    sobely = cv2.Sobel(frame,cv2.CV_64F,0,1,ksize=5)
    edges = cv2.Canny(frame,100,200)
    
    cv2.imshow('original',frame)
    cv2.imshow('laplacian',laplacian)
    cv2.imshow('sobelx',sobelx)
    cv2.imshow('sobely',sobely)
    cv2.imshow('edges',edges)

    if cv2.waitKey(3) & 0xFF == ord('q'):
        break

cv2.destroyAllWindows()
cap.release()
    
    

11. template matching(模板匹配)


# coding: utf-8

# In[1]:


#lesson11


# In[2]:


import cv2
import numpy as np

img_rgb = cv2.imread('opencv-template-matching-python-tutorial.jpg')
img_gray = cv2.cvtColor(img_rgb, cv2.COLOR_BGR2GRAY)

template = cv2.imread('opencv-template-for-matching.jpg',0)
w, h = template.shape[::-1]


# In[5]:


res = cv2.matchTemplate(img_gray,template,cv2.TM_CCOEFF_NORMED)
threshold = 0.7
loc = np.where( res >= threshold)


# In[6]:


for pt in zip(*loc[::-1]):
    cv2.rectangle(img_rgb, pt, (pt[0] + w, pt[1] + h), (0,0,255), 2)

cv2.imwrite('leeson11-Detected.png',img_rgb)

这里面,输入的图片

opencv-template-matching-python-tutorial.jpg

《OpenCV图像处理(Python)》网课笔记(从9到12)_第1张图片

opencv-template-for-matching.jpg

输出结果

leeson11-Detected.png

《OpenCV图像处理(Python)》网课笔记(从9到12)_第2张图片

 

12. GrabCut Foreground Extraction


# coding: utf-8

# In[1]:


#lesson12


# In[2]:


import cv2
import numpy as np
import matplotlib.pyplot as plt


# In[4]:


img = cv2.imread('opencv-python-foreground-extraction-tutorial.jpg')
mask = np.zeros(img.shape[:2],np.uint8)


# In[5]:


bgdModel = np.zeros((1,65),np.float64)
fgdModel = np.zeros((1,65),np.float64)


# In[6]:


rect = (161,79,150,150)


# In[8]:


cv2.grabCut(img,mask,rect,bgdModel,fgdModel,5,cv2.GC_INIT_WITH_RECT)
mask2 = np.where((mask==2)|(mask==0),0,1).astype('uint8')
img = img*mask2[:,:,np.newaxis]

cv2.imwrite('lesson12.jpg',img)

plt.imshow(img)
plt.colorbar()
plt.show()

opencv-python-foreground-extraction-tutorial.jpg

《OpenCV图像处理(Python)》网课笔记(从9到12)_第3张图片

lesson12.jpg

《OpenCV图像处理(Python)》网课笔记(从9到12)_第4张图片

 

 

 

 

你可能感兴趣的:(图像处理)