当前位置: 首页 > news >正文

Opencv图像处理:旋转、打包、多图像匹配

文章目录

  • 一、图像的旋转
    • 1、使用numpy方法实现旋转
      • 1)顺时针旋转90度
      • 2)逆时针旋转90度
    • 2、使用opencv的方法实现图像旋转
      • 1)顺时针旋转90度
      • 2)逆时针旋转90度
      • 3)旋转180度
    • 3、效果
  • 二、多图像匹配
    • 1、模板
    • 2、匹配对象
    • 3、代码实现
      • 1)预处理
      • 2)定义find_temp函数
      • 3)进行模板匹配
  • 三、打包与np.where()函数
    • 1、np.where()函数
      • 1)作为条件选择器
      • 2)作为条件索引获取器(省略 x 和 y)
    • 2、打包与解包
      • 1)打包
      • 2)解包


一、图像的旋转

1、使用numpy方法实现旋转

  • 读取图片并重设图片大小
import cv2
import numpy as npimg=cv2.imread("kele.png")
img=cv2.resize(img,dsize=None,fx=0.5,fy=0.5)cv2.imshow('yuantu',img)

1)顺时针旋转90度

# 旋转90度,k=-1,表示顺时针旋转90度
rotated_image1=np.rot90(img,k=-1)
cv2.imshow('totated_image1',rotated_image1)

2)逆时针旋转90度

# 旋转90度,k=-1,表示顺时针旋转90度
rotated_image1=np.rot90(img,k=-1)
cv2.imshow('totated_image1',rotated_image1)

2、使用opencv的方法实现图像旋转

1)顺时针旋转90度

rotated_image=cv2.rotate(img,cv2.ROTATE_90_CLOCKWISE)   #顺时针旋转90
cv2.imshow('shun90',img)

2)逆时针旋转90度

rotated_image1=cv2.rotate(img,cv2.ROTATE_90_COUNTERCLOCKWISE)   #逆时针旋转90度
cv2.imshow('ni90',rotated_image1)

3)旋转180度

rotated_image2=cv2.rotate(img,cv2.ROTATE_180)   #旋转180度
cv2.imshow('180',rotated_image2)
cv2.waitKey(0)

3、效果

在这里插入图片描述


二、多图像匹配

  • 这是之前写的单模板、多模板匹配,可以先去看一下方便理解:Opencv图像处理:模板匹配对象

1、模板

在这里插入图片描述

2、匹配对象

在这里插入图片描述

3、代码实现

1)预处理

import cv2
import numpy as npimg_rgb=cv2.imread('image.jpg')
img_gray=cv2.cvtColor(img_rgb,cv2.COLOR_BGR2GRAY)
template=cv2.imread('tem.jpg',0)
template1 = np.rot90(template,k=-1)
template2 = np.rot90(template,k=1)
h,w=template.shape[:2]

2)定义find_temp函数

def find_temp(temp):res=cv2.matchTemplate(img_gray,temp,cv2.TM_CCOEFF_NORMED)threshold=0.9loc=np.where(res>=threshold)for pt in zip(*loc[::-1]):cv2.rectangle(img_rgb,pt,(pt[0]+w,pt[1]+h),(0,0,255),1)

3)进行模板匹配

find_temp(template)
find_temp(template1)
find_temp(template2)
cv2.imshow('', img_rgb)
cv2.waitKey(0)

三、打包与np.where()函数

1、np.where()函数

1)作为条件选择器

np.where(condition, x=None, y=None)

  • condition:布尔数组或表达式,用于指定条件。
  • x, y(可选):当条件为 True 时返回 x 对应位置的元素,为 False 时返回 y 对应位置的元素。
  • 返回值:形状与 condition 相同的数组,元素来自 x 或 y。
import numpy as npa = np.array([1, 2, 3, 4, 5])
# 将大于 3 的元素替换为 10,否则保持原值
result = np.where(a > 3, 10, a)
print(result)  # 输出: [ 1  2  3 10 10]# 更复杂的条件(结合逻辑运算)
b = np.array([10, 20, 30, 40])
condition = (a > 2) & (b < 35)  # 同时满足两个条件
result = np.where(condition, a * 2, b // 2)
print(result)  # 输出: [ 2  4  6 20](仅前3个元素满足条件,最后一个不满足,取 b//2=20)

2)作为条件索引获取器(省略 x 和 y)

np.where(condition)

  • 作用:返回满足条件 condition 的元素的索引(以元组形式表示,每个元素对应数组的一个维度)。
  • 返回值:元组 (ind1, ind2, …, indn),其中 indi 是第 i 维满足条件的索引数组。
a = np.array([1, 2, 3, 4, 4, 5])
# 获取值为 4 的元素的索引
indices = np.where(a == 4)
print(indices)  # 输出: (array([3, 4]),)(一维数组,索引为 3 和 4)# 二维数组示例
b = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
condition = b > 5
indices = np.where(condition)
print(indices)  # 输出: (array([1, 2, 2]), array([2, 0, 1, 2])),对应行和列的索引

2、打包与解包

1)打包

a=[1,2,3]
b=[4,5,6]# 使用zip将他们按位置进行配对
zipped=zip(a,b)
print(list(zipped))
# 输出:[(1,4),(2,5),(3,6)]

2)解包

zip()将多个可迭代对象(列表、元组)进行解压操作

# 假设我们已经有了一个打包好的zip对象
zipped=zip(a,b)# #使用*运算符解包,得到转置的结果
unzipped=zip(*zipped)
loc = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]# 1. loc[::-1]:反转列表
reversed_loc = loc[::-1]
print(reversed_loc)  # 输出: [[7, 8, 9], [4, 5, 6], [1, 2, 3]]# 2. *reversed_loc:解包列表
# 此时相当于 zip([7, 8, 9], [4, 5, 6], [1, 2, 3])# 3. zip(*reversed_loc):使用 zip 函数进行打包
zipped = zip(*reversed_loc)
for pt in zipped:print(pt)
# 输出:
# (7, 4, 1)
# (8, 5, 2)
# (9, 6, 3)

http://www.xdnf.cn/news/120205.html

相关文章:

  • 嵌入式面试高频考点深度解析:内存管理、指针操作与结构体实战指南
  • 高德MCP制作旅游攻略
  • Volcano 实战快速入门 (一)
  • 03_JavaScript
  • mysql快速在不同库中执行相同的sql
  • PCB常见封装类型
  • [U-Net]DA-TRANSUNET
  • 如何将 PDF 中的文本提取为 JSON 格式
  • nfs服务原理、搭建手册、安全配置建议及异常定位手段
  • 跨域 同源策略通俗讲解
  • 在数据链路层扩展以太网
  • Oracle 11g RAC手动打补丁详细步骤
  • NXP----SVR5510芯片layout设计总结
  • LLMind:利用大型语言模型协调人工智能与物联网以执行复杂任务
  • SAIL-RK3588 社区充电桩智能管理方案
  • 如何应对客户提出的不合理需求
  • 利用deepseek快速生成甘特图
  • 基于事件驱动的云原生后端架构设计:从理念到落地
  • Redis 与 Memcache 全面对比:功能、性能与应用场景解析
  • IP的基础知识以及相关机制
  • 焦化烧结行业无功补偿解决方案—精准分组补偿 稳定电能质量沃伦森
  • 基于 RK3588 + 双天线差分 GNSS + RTK 的自主可控技术平台
  • windows安装Mysql
  • 初识Redis · 主从复制(下)
  • linux嵌入式(进程与线程1)
  • 2023 国考
  • Node.js 学习入门指南
  • Java:XML被自动转义
  • QT创建新项目(13)
  • 使用Django REST Framework快速开发API接口