OpenCV基本操作

OpenCV基本操作

import cv2
from matplotlib import pyplot as plt
import numpy as np
import random

def cv_show(name, img):
    cv2.imshow(name, img)
    cv2.waitKey(0)
    cv2.destroyAllWindows()

图片读取

img = cv2.imread('./images/cat.10.jpg')
img = cv2.imread('jb.png')
img = cv2.resize(img, (300, 600))
print(img)
cv2.imshow('image', img)
cv2.waitKey(0)
cv2.destroyAllWindows()

视频读取

vc = cv2.VideoCapture('./test.mp4')
if vc.isOpened():
    open, frame = vc.read()
else:
    open = False
while open:
    rel, frame = vc.read()
    if frame is None:
        break
    if rel == True:
        gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
        cv2.imshow('result', gray)
        if cv2.waitKey(100) & 0xFF == 27:
            break
    else:
        break
vc.release()
cv2.destroyAllWindows()

截取部分图像

img = cv2.imread('./images/cat.1.jpg')
cat = img[0:200, 0:200]
cv2.imshow('cat', cat)
cv2.waitKey(0)
cv2.destroyAllWindows()

颜色通道提取

b, g, r = cv2.split(img)
# print(b, b.shape)
img = cv2.merge((b, g, r))
cur_img = img.copy()
cur_img[:, :, 0] = 0
cur_img[:, :, 1] = 0
cv2.imshow('cur_img', cur_img)
cv2.waitKey(0)
cv2.destroyAllWindows()

边界填充

top_size, bottom_size, left_size, right_size = (50, 50, 50, 50)
replicate = cv2.copyMakeBorder(img, top_size, bottom_size, left_size, right_size, borderType=cv2.BORDER_REPLICATE)
reflect = cv2.copyMakeBorder(img, top_size, bottom_size, left_size, right_size, cv2.BORDER_REFLECT)
reflect101 = cv2.copyMakeBorder(img, top_size, bottom_size, left_size, right_size, cv2.BORDER_REFLECT_101)
wrap = cv2.copyMakeBorder(img, top_size, bottom_size, left_size, right_size, cv2.BORDER_WRAP)
constant = cv2.copyMakeBorder(img, top_size, bottom_size, left_size, right_size, cv2.BORDER_CONSTANT, value=0)
plt.subplot(231), plt.imshow(img, 'gray'), plt.title('ORIGINAL')
plt.subplot(232), plt.imshow(replicate, 'gray'), plt.title('REPLICATE')
plt.subplot(233), plt.imshow(reflect, 'gray'), plt.title('REFLECT')
plt.subplot(234), plt.imshow(reflect101, 'gray'), plt.title('REFLECT_101')
plt.subplot(235), plt.imshow(wrap, 'gray'), plt.title('WRAP')
plt.subplot(236), plt.imshow(constant, 'gray'), plt.title('CONSTANT')
plt.show()

数值计算

img_cat1 = cv2.imread('./images/cat.5.jpg')
img_cat11 = img_cat1 + 10
for i in range(img.shape[0]):
    for j in range(img.shape[1]):
        img_cat2 = random.randint(0, 256)
img_cat22 = img_cat1 + img_cat2
cv2.imshow('0', img_cat1)
cv2.imshow('1', img_cat11)
cv2.imshow('2', img_cat22)
cv2.waitKey(0)
cv2.destroyAllWindows()

图像融合

img_cat1 = cv2.imread('./images/cat.5.jpg')
img_cat2 = cv2.imread('./images/cat.11.jpg')
print(img_cat1.shape, img_cat2.shape)
img_cat22 = cv2.resize(img_cat2, (175, 144))
print(img_cat22.shape)
img_cat222 = cv2.resize(img_cat2, (0, 0), fx=3, fy=1)
img_merge = img_cat1 + img_cat22
img_merge1 = cv2.addWeighted(img_cat1, 0.4, img_cat22, 0.6, 0)
# cv2.imshow('0', img_cat1)
# cv2.imshow('1', img_cat2)
# cv2.imshow('2', img_cat22)
# cv2.imshow('3', img_merge)
# cv2.imshow('4', img_cat222)
cv2.imshow('5', img_merge1)
cv2.waitKey(0)
cv2.destroyAllWindows()
cv2.imwrite('my_cat.png', img)

图像处理

ret, thresh1 = cv2.threshold(img, 127, 255, cv2.THRESH_BINARY)
ret, thresh2 = cv2.threshold(img, 127, 255, cv2.THRESH_BINARY_INV)
ret, thresh3 = cv2.threshold(img, 127, 255, cv2.THRESH_TRUNC)
ret, thresh4 = cv2.threshold(img, 127, 255, cv2.THRESH_TOZERO)
ret, thresh5 = cv2.threshold(img, 127, 255, cv2.THRESH_TOZERO_INV)
title = ['Origin', 'Binary', 'Binary_inv', 'Trunc', 'Tozero', 'Tozero_inv']
images = [img, thresh1, thresh2, thresh3, thresh4, thresh5]
for i in range(6):
    plt.subplot(2, 3, i+1), plt.imshow(images[i], 'gray')
    plt.title(title[i])
    plt.xticks([]), plt.yticks([])
plt.show()

图像平滑

加噪声

for i in range(2000): # 添加点噪声
    temp_x = np.random.randint(0, img.shape[0])
    temp_y = np.random.randint(0, img.shape[1])
    img[temp_x][temp_y] = 255
cv2.imshow('img', img)
cv2.waitKey(0)
cv2.destroyAllWindows()

均值滤波

blur = cv2.blur(img, (3, 3))
cv2.imshow('blur', blur)
cv2.waitKey(0)
cv2.destroyAllWindows()

方框滤波1

box1 = cv2.boxFilter(img, -1, (3, 3), normalize=True)
cv2.imshow('box', box1)
cv2.waitKey(0)
cv2.destroyAllWindows()

方框滤波2

box2 = cv2.boxFilter(img, -1, (3, 3), normalize=False)
cv2.imshow('box', box2)
cv2.waitKey(0)
cv2.destroyAllWindows()

高斯滤波

gaussian = cv2.GaussianBlur(img, (5, 5), 1)
cv2.imshow('gaussian', gaussian)
cv2.waitKey(0)
cv2.destroyAllWindows()

中值滤波

median = cv2.medianBlur(img, 5)
cv2.imshow('median', median)
cv2.waitKey(0)
cv2.destroyAllWindows()

展示所有

res = np.hstack((blur, box1, box2, gaussian, median))
print(res)
cv2.imshow('all', res)
cv2.waitKey(0)
cv2.destroyAllWindows()

形态学处理

腐蚀操作

kernel = np.ones((15, 15), np.uint8)
ret, thresh1 = cv2.threshold(img, 127, 255, cv2.THRESH_BINARY)
erosion = cv2.erode(thresh1, kernel, iterations=1)
cv2.imwrite('jb_1.png', erosion)
res = np.hstack((thresh1, erosion))
cv2.imshow('erosion', res)
cv2.waitKey(0)
cv2.destroyAllWindows()

膨胀操作

dilate = cv2.dilate(erosion, kernel, iterations=1)
res = np.hstack((img, dilate))
cv2.imshow('dilate', res)
cv2.waitKey(0)
cv2.destroyAllWindows()

开运算闭运算

opening = cv2.morphologyEx(img, cv2.MORPH_OPEN, kernel)
closing = cv2.morphologyEx(img, cv2.MORPH_CLOSE, kernel)
res = np.hstack((img, opening, closing))
cv2.imshow('open-close', res)
cv2.waitKey(0)
cv2.destroyAllWindows()

梯度运算

kernel = np.ones((5, 5), np.uint8)
dilate = cv2.dilate(img, kernel, iterations=5)
erosion = cv2.erode(img, kernel, iterations=5)
res = np.hstack((dilate, erosion))
cv2.imshow('res', res)
cv2.waitKey(0)
cv2.destroyAllWindows()
gradient = cv2.morphologyEx(img, cv2.MORPH_GRADIENT, kernel)
cv2.imshow('gradient', gradient)
cv2.waitKey(0)
cv2.destroyAllWindows()
cv2.imwrite('jb_1.png', gradient)

礼帽黑帽

tophat = cv2.morphologyEx(img, cv2.MORPH_TOPHAT, kernel)
blackhat = cv2.morphologyEx(img, cv2.MORPH_BLACKHAT, kernel)
res = np.hstack((tophat, blackhat))
cv2.imshow('tophat-blackhat', res)
cv2.waitKey(0)
cv2.destroyAllWindows()

边缘检测

sobel

sobelx = cv2.Sobel(img, cv2.CV_64F, 1, 0, ksize=3)
sobelx = cv2.convertScaleAbs(sobelx)
cv_show(sobelx, 'sobelx')
sobely = cv2.Sobel(img, cv2.CV_64F, 0, 1, ksize=3)
sobely = cv2.convertScaleAbs(sobely)
cv_show(sobelx, 'sobely')
sobel = cv2.addWeighted(sobelx, 0.5, sobely, 0.5, 0)
cv_show(sobel, 'sobel')

scharr、laplacian

scharrx = cv2.Scharr(img, cv2.CV_64F, 1, 0)
scharry = cv2.Scharr(img, cv2.CV_64F, 0, 1)
scharrx = cv2.convertScaleAbs(scharrx)
scharry = cv2.convertScaleAbs(scharry)
scharr = cv2.addWeighted(scharrx, 0.5, scharry, 0.5, 0)
laplacian = cv2.Laplacian(img, cv2.CV_64F)
laplacian = cv2.convertScaleAbs(laplacian)
res = np.hstack((sobel, scharr, laplacian))
cv_show(res, 'res')

canny

v1 = cv2.Canny(img, 80, 150)
v2 = cv2.Canny(img, 50, 100)
res = np.hstack((v1, v2))
cv_show(res, 'res')

高斯金字塔

up = cv2.pyrUp(img)
print(up.shape)
cv_show(up, 'up')
down = cv2.pyrDown(img)
cv_show(down, 'down')

拉普拉斯金字塔

up = cv2.pyrUp(img)
print(up.shape)
cv_show(up, 'up')
down = cv2.pyrDown(img)
cv_show(down, 'down')

图像轮廓

img = cv2.imread('lunkuo.jpg')
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
ret, thresh = cv2.threshold(gray, 127, 255, cv2.THRESH_BINARY)
cv_show(thresh, 'thresh')
contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE)
draw_img = img.copy()
res = cv2.drawContours(draw_img, contours, -1, (0, 0, 255), 5)
cv_show(res, 'res')
cnt = contours[0]
print(cv2.contourArea(cnt))
print(cv2.arcLength(cnt, True))

轮廓近似

gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
ret, thresh = cv2.threshold(gray, 127, 255, cv2.THRESH_BINARY)
contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE)
cnt = contours[1]
draw_img = img.copy()
res = cv2.drawContours(draw_img, [cnt], -1, (0, 0, 255), 10)
cv_show(res, 'res')
epsilon = 0.1 * cv2.arcLength(cnt, True)
approx = cv2.approxPolyDP(cnt, epsilon, True)
draw_img = img.copy()
res = cv2.drawContours(draw_img, [approx], -1, (0, 0, 255), 10)
cv_show(res, 'res')

边界矩形

cv_show(img, 'img')
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
ret, thresh = cv2.threshold(gray, 127, 255, cv2.THRESH_BINARY)
contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE)
print(len(contours), len(hierarchy))
cnt = contours[1]
x, y, w, h = cv2.boundingRect(cnt)
img = cv2.rectangle(img, (x, y), (x+w, y+h), (0, 255, 0), 2)
cv_show(img, 'img')
area = cv2.contourArea(cnt)
x, y, w, h = cv2.boundingRect(cnt)
rect_area = w * h
extent = float(area) / rect_area
print('轮廓面积和边界矩形比', extent)

外接圆

(x, y), radius = cv2.minEnclosingCircle(cnt)
center = (int(x), int(y))
radius = int(radius)
res = cv2.circle(img, center, radius, (0, 255, 0), 2)
cv_show(res, 'img')

模板匹配

img = cv2.imread('my_cat.png')
template = cv2.imread('my_cat1.png')
h, w = template.shape[:2]
print(img.shape, template.shape)
methods = {'cv2.TM_CCOEFF', 'cv2.TM_CCOEFF_NORMED', 'cv2.TM_CCORR', 'cv2.TM_CCORR_NORMED', 'cv2.TM_SQDIFF', 'cv2.TM_SQDIFF_NORMED'}
res = cv2.matchTemplate(img, template, cv2.TM_CCOEFF)
print(res.shape)
min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(res)

for meth in methods:
    img2 = img.copy()
    method = eval(meth)
    print(method)
    res = cv2.matchTemplate(img, template, method)
    min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(res)
    if method in [cv2.TM_SQDIFF, cv2.TM_SQDIFF_NORMED]:
        top_left = min_loc
    else:
        top_left = max_loc
    bottom_right = (top_left[0] + w, top_left[1] + h)

    # 画矩形
    cv2.rectangle(img2, top_left, bottom_right, 255, 2)
    plt.subplot(121), plt.imshow(res, cmap='gray')
    plt.xticks([]), plt.yticks([]) # 隐藏坐标轴
    plt.subplot(122), plt.imshow(img2, cmap='gray')
    plt.xticks([]), plt.yticks([])  # 隐藏坐标轴
    plt.suptitle(meth)
    plt.show()

匹配多个对象

img_rgb = cv2.imread('my_cat.png')
img_gray = cv2.cvtColor(img_rgb, cv2.COLOR_BGR2GRAY)
template = cv2.imread('my_cat1.png', 0)
# print(template.shape)
h, w = template.shape[:2]
# print(h, w)
res = cv2.matchTemplate(img_gray, template, cv2.TM_CCOEFF_NORMED)
# print(res)
threshold = 0.6
loc = np.where(res >= threshold)
# print(loc[::1])
for pt in zip(*loc[::1]):
    bottom_right = (pt[0] + w, pt[1] + h)
    cv2.rectangle(img_rgb, pt, bottom_right, (0, 0, 255), 2)
cv2.imshow('img_rgb', img_rgb)
cv2.waitKey(0)

直方图

img = cv2.imread('my_cat.png', 0)
hist = cv2.calcHist([img], [0], None, [256], [0, 256])
print(hist.shape)
plt.hist(img.ravel(), 256)
plt.show()
img = cv2.imread('my_cat.png')
color = ('b', 'g', 'r')
for i, col in enumerate(color):
    histr = cv2.calcHist([img], [i], None, [256], [0, 256])
    plt.plot(histr, color=col)
    plt.xlim([0, 256])
plt.show()

mask操作

cv_show(img, 'img')
mask = np.zeros(img.shape[:2], np.uint8)
mask[100:300, 100:400] = 255
cv_show(mask, 'mask')
masked_img = cv2.bitwise_and(img, img, mask=mask) #与操作
cv_show(masked_img, 'masked_img')
hist_full = cv2.calcHist([img], [0], None, [256], [0, 256])
hist_mask = cv2.calcHist([img], [0], mask, [256], [0, 256])
plt.subplot(221), plt.imshow(img, 'gray')
plt.subplot(222), plt.imshow(mask, 'gray')
plt.subplot(223), plt.imshow(masked_img, 'gray')
plt.subplot(224), plt.plot(hist_full), plt.plot(hist_mask)
plt.xlim([0, 256])
plt.show()

均衡化

equ = cv2.equalizeHist(img)
plt.hist(equ.ravel(), 256)
plt.show()

自适应直方图均衡化

clahe = cv2.createCLAHE(clipLimit=2.0, tileGridSize=(8, 8))
res_clahe = clahe.apply(img)
res = np.hstack((img, equ, res_clahe))
cv_show(res, 'res')

傅里叶变换

img_float32 = np.float32(img)
dft = cv2.dft(img_float32, flags=cv2.DFT_COMPLEX_OUTPUT)
dft_shift = np.fft.fftshift(dft)
magnitude_spectrum = 20*np.log(cv2.magnitude(dft_shift[:, :, 0], dft_shift[:, :, 1]))
plt.subplot(121), plt.imshow(img, cmap='gray')
plt.title('Input Image'), plt.xticks([]), plt.yticks([])
plt.subplot(122), plt.imshow(magnitude_spectrum, cmap='gray')
plt.title('Magnitude Spectrum'), plt.xticks([]), plt.yticks([])
plt.show()

img_float32 = np.float32(img)
dft = cv2.dft(img_float32, flags=cv2.DFT_COMPLEX_OUTPUT)
dft_shift = np.fft.fftshift(dft)
rows, cols = img.shape
crow, ccol = int(rows/2), int(cols/2) # 中心位置
# 低通滤波
mask_low = np.zeros((rows, cols, 2), np.uint8)
mask_low[crow-30:crow+30, ccol-30:ccol+30] = 1

# IDFT
fshift = dft_shift*mask_low
f_ishift = np.fft.ifftshift(fshift)
img_back = cv2.idft(f_ishift)
img_back = cv2.magnitude(img_back[:, :, 0], img_back[:, :, 1])
plt.subplot(121), plt.imshow(img, cmap='gray')
plt.title('Input Image'), plt.xticks([]), plt.yticks([])
plt.subplot(122), plt.imshow(img_back, cmap='gray')
plt.title('Result'), plt.xticks([]), plt.yticks([])
plt.show()
# 高通滤波
mask_high = np.ones((rows, cols, 2), np.uint8)
mask_high[crow-30:crow+30, ccol-30:ccol+30] = 0

# IDFT
fshift = dft_shift*mask_high
f_ishift = np.fft.ifftshift(fshift)
img_back = cv2.idft(f_ishift)
img_back = cv2.magnitude(img_back[:, :, 0], img_back[:, :, 1])
plt.subplot(121), plt.imshow(img, cmap='gray')
plt.title('Input Image'), plt.xticks([]), plt.yticks([])
plt.subplot(122), plt.imshow(img_back, cmap='gray')
plt.title('Result'), plt.xticks([]), plt.yticks([])
plt.show()
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值