python 对论文插图制作局部放大图并在原图中相同位置加边框:
全部代码如下:
import cv2
import numpy as np
image_path = "E:\\仿真资料\\论文草稿\\监督和自监督训练定性对比图\\"
save_path = "E:\\仿真资料\\论文草稿\\监督和自监督训练定性对比图_2\\"
#image_path = "F:\\原始图片\yueye_2\\"
#save_path = "F:\\原始图片\yueye_2\\"
k = '0000000065.png'
img = cv2.imdecode(np.fromfile(image_path+k, dtype = np.uint8),-1) # 读取带有中文路径或中文文件名的图片
#img = cv2.imread(image_path+i, -1) #在cv2.imread参数中加入-1,表示不改变读取图像的类型直接读取
print(img.shape)
print(img.dtype)
i = 30 #要放大区域的左上角的 x 坐标(竖轴坐标)
j = 1280 #要放大区域的左上角的 y 坐标(横轴坐标)
h = 130 #要放大区域的高
w = 140 #要放大区域的宽
#h_2 = 300
#w_2 = 300
patch1 = img[i:i+h, j:j+w] # numpy 里先x,后y,x轴沿垂直方向向下,y轴沿水平方向向右
#patch1 = cv2.resize(patch1, (w_2, h_2)) #(w, h)
cv2.imencode('.png', patch1)[1].tofile(save_path+k) #也可以将 .png 改为 .jpg。
#cv2.imwrite(save_path+i, patch1)
print(patch1.shape)
print(patch1.dtype)
pt1 = (j, i) # 长方形框左上角坐标
pt2 = (j+w, i+h) # 长方形框右下角坐标
cv2.rectangle(img, pt1, pt2, (0, 0, 255), 2) # cv2 里也是先x,后y,x轴沿水平方向向右,y轴沿垂直方向向下
cv2.imencode('.png', img)[1].tofile(save_path+k[:-4]+'_带边框'+'(左上角坐标:({}, {}),高:{},宽:{})'.format(i,j,h,w)+'.png') # 保存带有中文路径或中文文件名的图片
#cv2.imwrite(save_path+i[:-4]+'_caijian'+'.png', img)