import cv2 as cv
import sys
import numpy as np
import matplotlib.pyplot as plt
if __name__ == '__main__':
# 读取图像并判断是否读取成功390*568
img = cv.imread('tu.jpg')
# 需要放大的部分
part = img[200:300, 250:350]
if img is None is None:
print('Failed to read picture')
sys.exit()
# # 1局部图和原图拼接
# big = np.zeros((390, 668, 3))
# big[0:390, :568, :] = img
# big[0:100, 568:668, :] = part
# # 先转为int型,可以在plt显示为图片
# big = big.astype(int)
# # 转为uint8,否则显示为一片黑
# big = big.astype(np.uint8)
# # cv.rectangle(img, 左下角=(x1, y1), 右下角=(x2, y2), 框BGR=(0, 255, 0), 框粗=1)
# cv.rectangle(big, (568, 0), (668, 100), (255, 0, 0), 2)
# cv.rectangle(big, (250, 200), (350, 300), (255, 0, 0), 2)
# # cv.line(img, 点=(x1, y1), 点=(x2, y2), (0, 255, 0), 2)
# big = cv.line(big, (350, 200), (568, 0), (0, 0, 255), 2)
# big = cv.line(big, (350, 300), (568, 100), (0, 255, 0), 2)
# cv.imshow('big', big)
# # 按0关闭页面
# cv.waitKey(0)
# cv.destroyAllWindows()
# 2原图像局部按比例缩放
mask = cv.resize(part, (300, 300), fx=0, fy=0, interpolation=cv.INTER_LINEAR)
big = np.zeros((390, 868, 3))
big[0:390, :568, :] = img
big[0:300, 568:868, :] = mask
# 先转为int型,可以在plt显示为图片
big = big.astype(int)
# 转为uint8,否则显示为一片黑
big = big.astype(np.uint8)
# cv.rectangle(img, 左下角=(x1, y1), 右下角=(x2, y2), 框BGR=(0, 255, 0), 框粗=1)
cv.rectangle(big, (568, 0), (868, 300), (255, 0, 0), 2)
cv.rectangle(big, (250, 200), (350, 300), (255, 0, 0), 2)
# cv.line(img, 点=(x1, y1), 点=(x2, y2), (0, 255, 0), 2)
big = cv.line(big, (350, 200), (568, 0), (0, 0, 255), 2)
big = cv.line(big, (350, 300), (568, 300), (0, 0, 255), 2)
cv.imshow('big', big)
# 按0关闭页面
cv.waitKey(0)
cv.destroyAllWindows()

原图见图中ID

局部放大的图
该代码示例展示了如何使用OpenCV库在Python中读取图像,选取图像的一部分进行放大,然后将放大后的局部图像与原图像拼接在一起。使用了cv.resize()函数进行缩放操作,并用cv.rectangle()和cv.line()画出边界和线条以标识放大区域。
687

被折叠的 条评论
为什么被折叠?



