科研作图-局部图像放大效果的python实现

局部图像放大镜效果的python实现

前言

当图中想传达的区域太小,而在不裁剪的情况下又想要放大图中的某一部分区域,实现放大镜的效果。

比如我想放大这只大冤种狗狗的眼睛,并将放大的结果放置在图中的角落里。达到如下图所示的结果。该如何实现呢?

代码

为了能方便友友们直接上手使用,我编写了可交互式的局部放大功能的代码。大家只需要输入自己图像的路径就可以体验啦。

import cv2
import os
import numpy as np
 
img_path= r'' # 请输入自己需要放大图像的路径
    
img_name = os.path.basename(img_path)
img = cv2.imread(img_path)

def on_EVENT_LBUTTONDOWN(event, x, y, flags, param):
    global img
    if event == cv2.EVENT_LBUTTONDOWN: # 按下鼠标左键则放大所点的区域
        xy = "%d,%d" % (x, y)
        print (xy)
        length = 20 # 局部区域的边长的一半
        big_length = 200 # 放大后图像的边长
        part_left = x - length
        part_right = x + length
        part_top = y - length
        part_bottom = y + length
        height, width, _ = np.shape(img)
        if (x < width / 2) & (y < height / 2): 
            loc_left = 10
            loc_top = 10
            loc_right = loc_left + big_length
            loc_bottom = loc_top + big_length
            cv2.line(img,(part_right,part_top),(loc_right,loc_top),(0,0,0),2)
            cv2.line(img,(part_left,part_bottom),(loc_left,loc_bottom),(0,0,0),2)
        elif (x >= width / 2) & (y < height / 2):
            loc_right = width - 10
            loc_left = loc_right - big_length
            loc_top = 10
            loc_bottom = loc_top + big_length
            cv2.line(img,(part_left,part_top),(loc_left,loc_top),(0,0,0),2)
            cv2.line(img,(part_right,part_bottom),(loc_right,loc_bottom),(0,0,0),2)
        elif (x < width / 2) & (y >= height / 2):
            loc_left = 10
            loc_right = loc_left + big_length
            loc_bottom = height - 10
            loc_top = loc_bottom - big_length
            cv2.line(img,(part_left,part_top),(loc_left,loc_top),(0,0,0),2)
            cv2.line(img,(part_right,part_bottom),(loc_right,loc_bottom),(0,0,0),2)
        elif (x >= width / 2) & (y >= height / 2):
            loc_bottom = height - 10
            loc_top = loc_bottom - big_length
            loc_right = width - 10
            loc_left = loc_right - big_length
            cv2.line(img,(part_right,part_top),(loc_right,loc_top),(0,0,0),2)
            cv2.line(img,(part_left,part_bottom),(loc_left,loc_bottom),(0,0,0),2)
        
        part = img[part_top:part_bottom,part_left:part_right]
        mask = cv2.resize(part, (big_length, big_length), fx=0, fy=0, interpolation=cv2.INTER_LINEAR)
        img[loc_top:loc_bottom,loc_left:loc_right]=mask
        cv2.rectangle(img,(part_left,part_top),(part_right,part_bottom),(0,0,0),2)
        cv2.rectangle(img,(loc_left,loc_top),(loc_right,loc_bottom),(0,0,0),2)
        cv2.imshow("image", img)
        
    if event == cv2.EVENT_RBUTTONDOWN: # 按下鼠标右键恢复原图
        img = cv2.imread(img_path)
        cv2.imshow("image", img)

cv2.namedWindow("image")
cv2.setMouseCallback("image", on_EVENT_LBUTTONDOWN)
cv2.imshow("image", img)
         
cv2.waitKey(0)   
cv2.imwrite("image1.jpg", img)

以下是简介的版本,大家只需要修改某些参数就能实现上述功能啦。

import cv2
import os
import numpy as np
 
img_path= r'' # 请输入自己需要放大图像的路径
img = cv2.imread(img_path)
# 输入局部点的中心点的坐标以及截取的边长的一半
x = 364
y = 261 
length = 20 
part_left = x - length
part_right = x + length
part_top = y - length
part_bottom = y + length
# 输入放大后图像左上点位于原图中的位置和边长
loc_left = 10
loc_top = 10
big_length = 200
loc_right = loc_left + big_length
loc_bottom = loc_top + big_length
# 截取图像中的局部区域并放大
part = img[part_top:part_bottom,part_left:part_right]
mask = cv2.resize(part, (big_length, big_length), fx=0, fy=0, interpolation=cv2.INTER_LINEAR)
# 将放大后的图像放置位于指定区域
img[loc_top:loc_bottom,loc_left:loc_right]=mask
# 画一些图形引导人的视线(可选择)
cv2.rectangle(img,(part_left,part_top),(part_right,part_bottom),(0,0,0),2)
cv2.rectangle(img,(loc_left,loc_top),(loc_right,loc_bottom),(0,0,0),2)
cv2.line(img,(part_right,part_top),(loc_right,loc_top),(0,0,0),2)
cv2.line(img,(part_left,part_bottom),(loc_left,loc_bottom),(0,0,0),2)
# 展示结果并保存
cv2.namedWindow("image2")
cv2.imshow("image2", img)
cv2.imwrite("image3.jpg", img)
cv2.waitKey(0)

下面具体介绍一下这个代码的核心部分,方便大家后续进行魔改。
1.获取局部区域并插值放大

part = img[part_top:part_bottom,part_left:part_right]
mask = cv2.resize(part, (big_length, big_length), fx=0, fy=0, interpolation=cv2.INTER_LINEAR)

2.将放大的图像放在原图中的某个位置

img[loc_top:loc_bottom,loc_left:loc_right]=mask

3.可以添加边框和线指引人的视线

cv2.rectangle(img,(part_left,part_top),(part_right,part_bottom),(0,0,0),2)
cv2.rectangle(img,(loc_left,loc_top),(loc_right,loc_bottom),(0,0,0),2)
cv2.line(img,(part_right,part_top),(loc_right,loc_top),(0,0,0),2)
cv2.line(img,(part_left,part_bottom),(loc_left,loc_bottom),(0,0,0),2)

应用

如果大家也是一名研究生,可以利用这些代码制作以下类型的图片。

Green Fluorescent Protein and Phase Contrast Image Fusion Via Detail Preserving Cross NetworkCT图

Image fusion in the loop of high-level vision tasks: A semantic-aware real-time infrared and visible image fusion network
在这里插入图片描述

  • 12
    点赞
  • 32
    收藏
    觉得还不错? 一键收藏
  • 11
    评论
实现Python图像局部细节放大,可以使用OpenCV库。以下是一种实现方法: 1. 导入必要的库和模块,例如cv2和sys。 2. 读取图像并判断是否成功读取。 3. 指定需要放大的部分区域,可以通过裁剪图像来选择感兴趣的区域。 4. 使用双线性插值法将选定的部分区域进行放大,可以使用cv2.resize函数实现。设置放大后的大小和插值方式。 5. 将放大后的结果放置在图像的指定位置,可以通过赋值操作来实现。 6. 可选:为放大的部分区域绘制边框和连线,以突出显示。 7. 展示最终的结果图像。 下面是实现局部细节放大Python代码示例: import cv2 as cv import sys if __name__ == '__main__': # 读取图像并判断是否读取成功 img = cv.imread('tu.jpg') if img is None: print('Failed to read picture') sys.exit() # 需要放大的部分区域 part = img[300:400, 250:350] # 双线性插值法放大部分区域 mask = cv.resize(part, (300, 300), fx=0, fy=0, interpolation=cv.INTER_LINEAR) # 放大局部图的位置 img[110:410, 570:870] = mask # 画框并连线 cv.rectangle(img, (250, 300), (350, 400), (0, 255, 0), 1) img = cv.line(img, (350, 300), (570, 110), (0, 255, 0)) img = cv.line(img, (350, 400), (570, 410), (0, 255, 0)) # 展示结果 cv.imshow('img', img) cv.waitKey(0) cv.destroyAllWindows() 注意:在代码中,你需要将'tu.jpg'替换为你实际使用的图像文件路径。此代码将选定的部分区域放大,并将结果放置在图像的指定位置。你还可以根据需要调整参数来获得更好的效果。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *3* [科研作图-局部图像放大效果python实现](https://blog.csdn.net/zzu_zhong/article/details/131312317)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* [python放大局部图像(画中画形式展示)](https://blog.csdn.net/qq_33687272/article/details/121363358)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论 11
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值