【学习记录】图片局部放大
写在前面:本博客仅作记录学习之用,部分图片来自网络,如需使用请注明出处,同时如有侵犯您的权益,请联系删除!
文章目录
- 前言
- 局部放大
- 函数实现
- 单一局部放大置于图像内部
- 多局部放大拼接于图形侧面
- 主函数
- 效果
- 目标区域选取的方法
- 目标区域选取程序
- 目标区域选取效果
- 使用说明
- 总结
- 修改记录
- 致谢
前言
本博客仅为学习记录之用,目的在于后续若需要相关的有资可查。在言语上恐有诸多纰漏,如有错误,欢迎指出交流学习!
本博客所包含的大致内容: 图片某一局部放大并置于图形内部;图片多个局部放大并拼接在原图侧面。
局部放大
为什么要局部放大?无论的图像生成、修复、去污等等,如生成对抗方法;亦或是图像分割,如unet。对于方法的结果都需要进行有效的展示,凸显方法的优越性能,往往细节的部分才是我们所更加关注的。但往往在图像本身很难观察到这些细节,因此有意识的将需要的局部放大是展示效果的有效方法。
函数实现
单一局部放大置于图像内部
思路也简单,首先获取到需要关注的部分,然后在将其进行插值放大,最后放在原图的某一位置即可。
本函数参数传入图像,需要目标区域的指标,其次放在的位置,支持四个角以及中心,以及放大的倍数。
目标区域为矩形,长宽比任意。
def Partial_magnification(pic, target, location='lower_right', ratio=1):''':param pic: input pic:param target: Intercept area, for example [target_x, target_y, target_w, target_h]:param location: lower_right,lower_left,top_right,top_left,center:param ratio: gain:return: oringal pic, pic'''img = copy.copy(pic)w, h = pic.shape[1], pic.shape[0],target_x, target_y = target[0], target[1]target_w, target_h = target[2], target[3]cv2.rectangle(pic, (target_x, target_y), (target_x + target_w, target_y + target_h), (255, 255, 0), 2)new_pic = pic[target_y:target_y + target_h, target_x:target_x + target_w]new_pic = cv2.resize(new_pic, (target_w*ratio, target_h*ratio), interpolation=cv2.INTER_CUBIC)if location == 'lower_right':pic[h-1-target_h*ratio:h-1, w-1-target_w*ratio:w-1] = new_piccv2.line(pic, (target_x + target_w, target_y + target_h), (w-1-target_w*ratio, h-1-target_h*ratio), (255, 0, 0),2)elif location == 'lower_left':pic[h-1-target_h*ratio:h-1, 0:target_w*ratio] = new_picelif location == 'top_right':pic[0:target_h*ratio, w-1-target_w*ratio:w-1] = new_picelif location == 'top_left':pic[0:target_h*ratio, 0:target_w*ratio] = new_picelif location == 'center':pic[int(h/2-target_h*ratio/2):int(h/2+target_h*ratio/2),int(w/2-target_w*ratio/2):int(w/2+target_w*ratio/2)] = new_picreturn img, pic
多局部放大拼接于图形侧面
当存在多个局部需要放大时,若都置于原图内部,遮挡图片内容的可能性较大,因此将其置于图像侧面。
本函数仅支持两个局部区域放大。和第一个函数不同的是两个局部放大的区域长宽比需要一致,为了方便拼接。没有设置放大倍数,默认局部放大后拼接图片与原图等高。
def Partial_magnification_2(pic, target1, target2, *args):''':param pic: input pic:param target: Intercept area, for example [target_x, target_y, target_w, target_w]:param location: lower_right,lower_left,top_right,top_left,center:param ratio: gain:return: oringal pic, pic'''# imgg = copy.copy(pic)w, h = pic.shape[1], pic.shape[0],assert target1[0]+target1[2] < w and target1[1]+target1[3] < h,\'The target1 area is too large and exceeds the image size'assert target2[0]+target2[2] < w and target2[1]+target2[3] < h,\'The target2 area is too large and exceeds the image size'assert target1[2] > 10 or target1[3] > 10, \'The target1 area is too small, not recommended'assert target2[2] > 10 or target2[3] > 10, \'The target2 area is too small, not recommended'assert target1[0] > 0 and target1[0] < w, \'The starting point of the target1 area is beyond the scope of the image'assert target2[0] > 0 and target2[0] < w, \'The starting point of the target2 area is beyond the scope of the image'if target2[2] / target2[3] == target1[2] / target1[3]: #R = target2[2]/target2[3]if target1[1] > target2[1]:target1_x, target1_y = target2[0], target2[1]target1_w, target1_h = target2[2], target2[3]target2_x, target2_y = target1[0], target1[1]target2_w, target2_h = target1[2], target1[3]else:target1_x, target1_y = target1[0], target1[1]target1_w, target1_h = target1[2], target1[3]target2_x, target2_y = target2[0], target2[1]target2_w, target2_h = target2[2], target2[3]cv2.rectangle(pic, (target1_x, target1_y), (target1_x + target1_w, target1_y + target1_h), (255, 250, 255), 2)cv2.rectangle(pic, (target2_x, target2_y), (target2_x + target2_w, target2_y + target2_h), (255, 252, 255), 2)new_pic1 = pic[target1_y:target1_y + target1_h, target1_x:target1_x + target1_w]new_pic1 = cv2.resize(new_pic1, (int(h//2 * R), h//2), cv2.INTER_CUBIC)new_pic2 = pic[target2_y:target2_y + target2_h, target2_x:target2_x + target2_w]new_pic2 = cv2.resize(new_pic2, (int(h//2 * R), h//2))img = np.zeros((h, int(h//2 * R), 3), np.uint8)img[0:h//2, 0:int(h//2 * R)] = new_pic1img[h//2:h, 0:int(h//2 * R)] = new_pic2hmerge = np.hstack((pic, img))cv2.line(hmerge, (target1_x + target1_w, target1_y), (w, 0), (255, 255, 255), 2)cv2.line(hmerge, (target2_x + target2_w, target2_y + target2_h), (w, h), (255, 255, 255), 2)return hmergeelse:raise ValueError('Make sure the aspect ratio of target is consistent !')
主函数
为方便复现提供了参考的主程序,如果需要批量放大,添加循环即可。
if __name__ == '__main__':img = cv2.imread(r'./test/Image_11L.jpg') target1 = [250, 250, 100, 100]pic, pic1 = Partial_magnification(img, target1, location='lower_right', ratio=4)target2 = [180, 400, 100, 50]pic, pic2 = Partial_magnification(pic, target2, location='lower_left', ratio=2)hmerge = np.hstack((pic1, pic2))if max(hmerge.shape[0], hmerge.shape[1]) > 1000:# cv2.namedWindow('merge', 0)# cv2.resizeWindow('merge', 1000, int(1000*hmerge.shape[0]/hmerge.shape[1]))# cv2.imshow('merge', hmerge)# cv2.waitKey(0)# cv2.destroyAllWindows()hmerge = cv2.cvtColor(hmerge, cv2.COLOR_BGR2RGB)fig = plt.figure(figsize=(40, 20)) # figsize 尺寸plt.imshow(hmerge)plt.savefig('aaa_bbb.png', dpi=200) # dpi分辨率plt.show()else:plt.imshow(hmerge)plt.show()
if __name__ == '__main__':img = cv2.imread(r'./test/Image_11L.jpg') target1 = [250, 650, 100, 100]target2 = [450, 400, 100, 100]pic1 = Partial_magnification_2(img, target1, target2)hmerge = cv2.cvtColor(pic1, cv2.COLOR_BGR2RGB)fig = plt.figure(figsize=(30, 20))fig.patch.set_facecolor('gray')plt.imshow(hmerge)plt.savefig('aaa_bbb.png', dpi=300, bbox_inches='tight')plt.show()
效果
以下是两个函数的测试结果,主程序如上。
目标区域选取的方法
上述的目标的指定放大的区域全靠大致位置加不断调整,使得绘图不够直观的准确,今添加选取指定区域的方法,利用opencv 直接直观的选取指定区域。
目标区域选取程序
def on_EVENT_change(event, x, y, flags, param):if event == cv2.EVENT_LBUTTONDOWN:point.append((x, y))target.append((x, y))xy = "%d,%d" % (x, y)cv2.circle(copyImg, (x, y), 1, (255, 0, 0), thickness=-1)cv2.putText(copyImg, xy, (x, y), cv2.FONT_HERSHEY_COMPLEX_SMALL, 1.0, (0, 0, 0), thickness=1)cv2.imshow("image", copyImg)elif event == cv2.EVENT_LBUTTONUP:point.append((x, y))target.append((x, y))xy = "%d,%d" % (x, y)cv2.rectangle(copyImg, point[0], point[1], (0, 255, 0), thickness=1, lineType=8, shift=0)cv2.circle(copyImg, (x, y), 1, (255, 0, 0), thickness=-1)cv2.putText(copyImg, xy, (x, y), cv2.FONT_HERSHEY_COMPLEX_SMALL, 1.0, (255, 0, 0), thickness=1)cv2.imshow("image", copyImg)point.clear()elif event == cv2.EVENT_RBUTTONDOWN:cv2.imshow("image", img)cv2.setMouseCallback("image", on_EVENT_LBUTTONDOWN)def on_EVENT_LBUTTONDOWN(event, x, y, flags, param):if event == cv2.EVENT_LBUTTONDOWN:point.append((x, y))target.append((x, y))xy = "%d,%d" % (x, y)cv2.circle(img, (x, y), 1, (255, 0, 0), thickness=-1)cv2.putText(img, xy, (x, y), cv2.FONT_HERSHEY_COMPLEX_SMALL, 1.0, (0, 0, 0), thickness=1)cv2.imshow("image", img)elif event == cv2.EVENT_LBUTTONUP:point.append((x, y))target.append((x, y))xy = "%d,%d" % (x, y)cv2.rectangle(img, point[0], point[1], (0, 255, 0), thickness=1, lineType=8, shift=0)cv2.circle(img, (x, y), 1, (255, 0, 0), thickness=-1)cv2.putText(img, xy, (x, y), cv2.FONT_HERSHEY_COMPLEX_SMALL, 1.0, (255, 0, 0), thickness=1)cv2.imshow("image", img)point.clear()elif event == cv2.EVENT_RBUTTONDOWN:cv2.imshow("image", copyImg)cv2.setMouseCallback("image", on_EVENT_change)if __name__ == '__main__': #Partial_magnification
# point、target、img、"image"不能修改,和前文定义函数绑定point = []target = []path = "xxx.jpg"img = cv2.imread(path)copyImg = copy.deepcopy(img)cv2.namedWindow("image")cv2.setMouseCallback("image", on_EVENT_LBUTTONDOWN)cv2.imshow("image", img)cv2.waitKey(0)cv2.destroyAllWindows()location = ['top_left', 'top_right', 'lower_right', 'lower_left', 'center']img = cv2.imread(path) # C:\Users\Administrator\Desktop\dd.jpgfor i in range(len(target)//2):if target[2*i+1][0]-target[2*i][0]>0:target1 = [target[2*i][0], target[2*i][1], target[2*i+1][0]-target[2*i][0], target[2*i+1][1]-target[2*i][1]]else:raise ValueError('请从左往右,从下往上 框取区域')if i == 0:pic, pic1 = Partial_magnification(img, target1, location=location[i], ratio=2)else:pic, pic1 = Partial_magnification(pic, target1, location=location[i], ratio=2)fig = plt.figure(figsize=(20, 20)) # figsize 尺寸plt.imshow(cv2.cvtColor(pic, cv2.COLOR_BGR2RGB))plt.savefig('xxx.png', dpi=300, bbox_inches='tight') # dpi 分辨率plt.show()
目标区域选取效果
使用说明
cv2上选取区域需要从左往右,从下往上 框取区域(局部放大函数的限制),最多五个区域(放大后的区域选项决定。如果选取区域在一个图相交,影响选取时右键使用备份绘制。
总结
以上就是本文的主要内容,都是OpenCV和plt的基本使用上实现图像的局部放大。
修改记录
2022.10.17 : 添加目标区域选取的方法
致谢
欲尽善本文,因所视短浅,怎奈所书皆是瞽言蒭议。行文至此,诚向予助与余者致以谢意。
【学习记录】图片局部放大相关推荐
- Opencv 入门篇学习记录(图片)
title: Opencv 入门篇学习记录(图片) Opencv 入门篇学习记录(图片) 前言 很早以前就接触Python了,大学的时候还自学了一段时间去做了课设,写了一些最速梯度下降法.黄金分割法. ...
- PS学习记录-图片添加白色描边效果
目录 效果展示 操作步骤 1.选取要描边的图像 2.编辑-描边选项 3.得到描边后的图片 效果展示 有时候,需要给图像增加一圈描边,让图像更有层次感(其实我个人感觉更像小时候的橡皮质感,哈哈哈),可以 ...
- 项目学习记录-图片服务器FastDFS
FastDFS简介 这是一款C语言开发的分布式文件系统,Fast Distribution File Server,为互联网量身定制,充分考虑了冗余备份,负载均衡,线性扩容等.使用FastDFS很容易 ...
- 【学习记录】图片行列切割与子图行列拼接之中央裁剪法
写在前面 :本博客仅作记录学习之用,部分图片来自网络,如需使用请注明出处,同时如有侵犯您的权益,请联系删除! 文章目录 前言 分割与拼接 拼接问题 分割和拼接的用途 函数实现 切割函数 拼接函数 中央 ...
- HTML5与CSS3权威指南之CSS3学习记录
title: HTML5与CSS3权威指南之CSS3学习记录 toc: true date: 2018-10-14 00:06:09 学习资料--<HTML5与CSS3权威指南>(第3版) ...
- ASP.NETCore学习记录(一)
ASP.NETCore学习记录(一) asp.net core介绍 Startup.cs ConfigureServices Configure 0. ASP.NETCore 介绍 ASP.N ...
- Android开发技术周报176学习记录
Android开发技术周报176学习记录 教程 当 OkHttp 遇上 Http 2.0 http://fucknmb.com/2018/04/16/%E5%BD%93OkHttp%E9%81%87% ...
- php+打开文件和其子文件,【php学习记录】 引用、打开文件
[php学习记录] 引用.打开文件 一.PHP echo 和 print 语句 echo 和 print 区别: echo - 可以输出一个或多个字符串 print - 只允许输出一个字符串,返回值总 ...
- Android Launch Mode的学习记录
我理解的Task实际上就是Activity堆栈,由于Task里面都是以Activity为单位的,所以一个Task里可以包含来自不同App的Activity,这里把跟Task相关的学习记录下. 关于La ...
最新文章
- 首位猪心移植患者去世!术后存活2个月,创造医学里程碑;官方讣告:哀悼并感谢所作巨大贡献...
- -gMIS持续优化更新, +InSiteSearch站内搜索
- DVWA--Command Injection(命令执行)--四个等级
- Android中ExpandableListView控件基本使用
- 普罗米修斯监控linux,Prometheus(普罗米修斯)搭建监控
- python机器学习库sklearn——线性回归
- Tomcat:JAVA_HOME should point to a JDK not a JRE解决
- 华硕ProArt创16 2022:为创作而生
- SENT:Sentence-level Distant Relation Extraction via Negtive Training-ACL2021
- ipad 邮箱服务器端口,ipad邮箱设置,牛排,YAHOO邮箱(后缀为yahoo
- Hadoop2.7.1+Hbase1.2.1集群环境搭建(3)1.x和2.x都支持的集群安装方式
- 彪悍的人生不需要解释!
- python初级练习
- t分布, 卡方x分布,F分布
- SM2262EN+东芝BICS3 1TB开卡报错处理
- win10升级win11后office文件无法直接打开的问题
- hashmap hash冲突怎么解决_10个HashMap问题搞定面试官
- 小程聊微服务-自己动手扩展分布式调用链
- PID的增量式与位置式
- 中高级测试工程师68道面试题
热门文章
- 偷,偷的爽啊。。。。
- 专题总纲目录 管理类联考 MBA/MEM/MPA/MPACC总纲
- .net Core 6.0 部署到欧拉(Linux)系统上,“The type initializer for ‘Gdip‘ threw an exception” 报错的解决方案
- Solidity 最新 0.5.8 中文文档发布
- 如何用java解一元二次方程方程
- 解决小米pad USB安装apk时AS报错:INSTALL_FAILED_USER_RESTRICTED
- 选择vray Next for SketchUp创建具启发性的、逼真的渲染的8大理由!
- 中国移动电商APP用户活跃度
- iphone开发(一)
- JS前端取得并解析后台服务器返回的JSON数据的方法