python opencv 将lena图像嵌入空白画布处

import cv2
import matplotlib.pyplot as plt
import numpy as np
path = "./temp(1)/temp/src.jpg"
threshold = 80

读取图像

img = cv2.imread(path)
img = img[:,:,::-1]
img.shape
plt.imshow(img)
plt.axis('off')
plt.show()

python opencv 将lena图像嵌入空白画布处_第1张图片

# 转化灰度
img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
img_gray.shape
plt.imshow(img_gray, cmap='gray')
plt.axis('off')
plt.show()

python opencv 将lena图像嵌入空白画布处_第2张图片

# 高斯模糊
img_gray = cv2.GaussianBlur(img_gray, (3, 3), 1)
plt.imshow(img_gray, cmap='gray')
plt.axis('off')
plt.show()

python opencv 将lena图像嵌入空白画布处_第3张图片

二值转化

_, img_br = cv2.threshold(img_gray, threshold, 255, cv2.THRESH_BINARY)
plt.imshow(img_br, cmap='gray')
plt.axis('off')
plt.show()

python opencv 将lena图像嵌入空白画布处_第4张图片

# 寻找外层轮廓
contours, hierarchy = cv2.findContours(img_br, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
# 确保至少找到一个轮廓
contour = np.zeros((4, 2))
if len(contours) > 0:
    # 按轮廓大小降序排列
    cnts = sorted(contours, key=cv2.contourArea, reverse=True)
    for c in cnts:
        # 近似轮廓
        peri = cv2.arcLength(c, True)
        approx = cv2.approxPolyDP(c, 0.02 * peri, True)
        # 近似轮廓有四个点,则确定
        if len(approx) == 4:
            contour = approx
            break

# 保留轮廓图片
img_contour = img.copy()
cv2.drawContours(img_contour, [contour], 0, (0, 0, 255), 1)
cv2.imwrite("./contour.png",img_contour)
cour = cv2.imread('./contour.png')
plt.imshow(cour)
plt.axis('off')
plt.show()

python opencv 将lena图像嵌入空白画布处_第5张图片

# 读取目标图片
dst = cv2.imread("./temp(1)/temp/dst.jpg")
# dst = cv2.imread("./temp(1)/temp/7.jpg")
# dst = cv2.imread('fish.jpg')
plt.imshow(dst[:,:,::-1])
plt.axis('off')
plt.show()

python opencv 将lena图像嵌入空白画布处_第6张图片

# 初始化原图Mask
mask = np.zeros_like(img)
# 获取子图长宽,赋给mask
h, w, c = dst.shape
mask[:h, :w, :] = 255
# mask 透射变换
# 得到变换矩阵
# 初始mask点
point1 = np.float32([[0, 0], [0, h - 1], [w - 1, h - 1], [w - 1, 0]])
# 目标mask点
point2 = np.float32(contour.reshape(4, 2))
# 得到变换矩阵
perspective_matrix = cv2.getPerspectiveTransform(point1, point2)
# 得到里白外黑mask
mask_center_true = cv2.warpPerspective(mask, perspective_matrix, (img.shape[1], img.shape[0]))
plt.imshow(mask_center_true)
plt.axis('off')
plt.show()

python opencv 将lena图像嵌入空白画布处_第7张图片

# 取图
mask_center_roi = cv2.bitwise_and(img,mask_center_true)
plt.imshow(mask_center_roi)
plt.axis('off')
plt.show()
# 反取mask
temp = np.ones_like(img) * 255
mask_center_false = cv2.bitwise_xor(mask_center_true,temp)
plt.imshow(mask_center_false)
plt.axis('off')
plt.show()
# and
src_center_false = cv2.bitwise_and(img,mask_center_false)
plt.imshow(src_center_false)
plt.axis('off')
plt.show()
# 得到dst透射变换
temp = np.zeros_like(img)
temp[:h,:w,:] = dst
dst_center_true = cv2.warpPerspective(temp, perspective_matrix, (img.shape[1], img.shape[0]))
plt.imshow(dst_center_true[:,:,::-1])
plt.axis('off')
plt.show()

python opencv 将lena图像嵌入空白画布处_第8张图片

# 融合
result = cv2.bitwise_or(src_center_false[:,:,::-1],dst_center_true)
plt.imshow(result[:,:,::-1])
plt.axis('off')
plt.show()

python opencv 将lena图像嵌入空白画布处_第9张图片

你可能感兴趣的:(opencv,计算机视觉)