コード例 #1
0
import pyopencv as cv
import numpy as np
import matplotlib.pyplot as plt

# 读入图片并缩小为1/2
img0 = cv.imread("lena.jpg")
size = img0.size()
w, h = size.width, size.height
img1 = cv.Mat()
cv.resize(img0, img1, cv.Size(w // 2, h // 2))

# 各种卷积核
kernels = [("低通滤波器", np.array([[1, 1, 1], [1, 2, 1], [1, 1, 1]]) * 0.1),
           ("高通滤波器", np.array([[0, -1, 0], [-1, 5, -1], [0, -1, 0]])),
           ("边缘检测", np.array([[-1, -1, -1], [-1, 8, -1], [-1, -1, -1]]))]

index = 0
for name, kernel in kernels:
    plt.subplot(131 + index)
    # 将卷积核转换为Mat对象
    kmat = cv.asMat(kernel.astype(np.float), force_single_channel=True)
    img2 = cv.Mat()
    cv.filter2D(img1, img2, -1, kmat)
    # 由于matplotlib的颜色顺序和OpenCV的顺序相反
    plt.imshow(img2[:, :, ::-1])
    plt.title(name)
    index += 1
    plt.gca().set_axis_off()
plt.subplots_adjust(0.02, 0, 0.98, 1, 0.02, 0)
plt.show()
コード例 #2
0
 def redraw(self):
     img2 = cv.Mat()
     kernel = cv.asMat(self.kernel * self.scale, force_single_channel=True)
     cv.filter2D(self.img, img2, -1, kernel)
     cv.imshow("Filter Demo", img2)
コード例 #3
0
# -*- coding: utf-8 -*-
import pyopencv as cv
import numpy as np
import time

img = cv.asMat(np.random.rand(1000, 1000))

row = cv.getGaussianKernel(7, -1)
col = cv.getGaussianKernel(5, -1)

kernel = cv.asMat(np.dot(col[:], row[:].T), force_single_channel=True)

img2 = cv.Mat()
img3 = cv.Mat()

start = time.clock()
cv.filter2D(img, img2, -1, kernel)
print(("filter2D:", time.clock() - start))

start = time.clock()
cv.sepFilter2D(img, img3, -1, row, col)
print(("sepFilter3D:", time.clock() - start))

print(("error=", np.max(np.abs(img2[:] - img3[:]))))
コード例 #4
0
 def redraw(self):
     img2 = cv.Mat()
     kernel = cv.asMat(self.kernel*self.scale, force_single_channel=True)
     cv.filter2D(self.img, img2, -1, kernel)
     cv.imshow("Filter Demo", img2)
コード例 #5
0
# -*- coding: utf-8 -*-
import pyopencv as cv
import numpy as np
import time 

img = cv.asMat(np.random.rand(1000,1000)) 

row = cv.getGaussianKernel(7, -1) 
col = cv.getGaussianKernel(5, -1)

kernel = cv.asMat(np.dot(col[:], row[:].T), force_single_channel=True) 

img2 = cv.Mat()
img3 = cv.Mat()

start = time.clock()
cv.filter2D(img, img2, -1, kernel) 
print "filter2D:", time.clock() - start

start = time.clock()
cv.sepFilter2D(img, img3, -1, row, col) 
print "sepFilter3D:", time.clock() - start

print "error=", np.max(np.abs(img2[:] - img3[:])) 
コード例 #6
0
# 读入图片并缩小为1/2
img0 = cv.imread("lena.jpg")
size = img0.size()
w, h = size.width, size.height
img1 = cv.Mat()
cv.resize(img0, img1, cv.Size(w//2, h//2)) 

# 各种卷积核
kernels = [ 
    (u"低通滤波器",np.array([[1,1,1],[1,2,1],[1,1,1]])*0.1),
    (u"高通滤波器",np.array([[0,-1,0],[-1,5,-1],[0,-1,0]])),
    (u"边缘检测",np.array([[-1,-1,-1],[-1,8,-1],[-1,-1,-1]]))
]

index = 0
for name, kernel in kernels:
    plt.subplot(131+index)
    # 将卷积核转换为Mat对象
    kmat = cv.asMat(kernel.astype(np.float), force_single_channel=True) 
    img2 = cv.Mat()
    cv.filter2D(img1, img2, -1, kmat) 
    # 由于matplotlib的颜色顺序和OpenCV的顺序相反
    plt.imshow(img2[:,:,::-1]) 
    plt.title(name)
    index += 1
    plt.gca().set_axis_off()
plt.subplots_adjust(0.02, 0, 0.98, 1, 0.02, 0)
plt.show()