机器视觉,opencv基础学习(一)

关于一些基础的opencv图像操作函数的脑图

① 二值化函数简单应用

python 复制代码
import cv2
import numpy as np
import matplotlib.pyplot as plt

img = cv2.imread('12.png', cv2.IMREAD_GRAYSCALE)
img1 = cv2.imread('12.png', cv2.IMREAD_GRAYSCALE)
img2 = cv2.imread('12.png', cv2.IMREAD_GRAYSCALE)

ret, img1 = cv2.threshold(img1, 127, 255, cv2.THRESH_BINARY+cv2.THRESH_OTSU)
ret2, img2 = cv2.threshold(img2, 127, 255, cv2.THRESH_BINARY_INV)
cv2.imshow('img', img)
cv2.imshow('img1', img1)
cv2.imshow('img2', img2)

cv2.waitKey(0)

②自适应二值化函数简单应用

python 复制代码
import cv2

img = cv2.imread('1.png', cv2.IMREAD_GRAYSCALE)

img_adaptive_binary = cv2.adaptiveThreshold(img,
                                            255,
                                            cv2.ADAPTIVE_THRESH_GAUSSIAN_C,
                                            cv2.THRESH_BINARY,
                                            37,
                                            5
                                            )

cv2.imshow('img', img)
cv2.imshow('img_adaptive_binary', img_adaptive_binary)
cv2.waitKey(0)

③腐蚀函数简单应用

python 复制代码
import cv2

img_binary = cv2.imread('2.png')
ret = cv2.getStructuringElement(cv2.MORPH_RECT, (9, 9))
img_erode = cv2.erode(img_binary, ret)
cv2.imshow('img', img_binary)
cv2.imshow('img_erode', img_erode)
cv2.waitKey(0)

④膨胀函数简单应用

python 复制代码
import cv2

# 读取一张彩色图
img = cv2.imread("1.png")
# 转换成灰度图
img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# 转换成二值化图
img_binary = cv2.adaptiveThreshold(img_gray,
                                   255,
                                   cv2.ADAPTIVE_THRESH_GAUSSIAN_C,
                                   cv2.THRESH_BINARY_INV,
                                   7,
                                   5)
# 进行腐蚀和膨胀操作
# 1、创建结构化元素 / 核
kernal = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (3, 3))
# 2、调用腐蚀函数
img_erode = cv2.erode(img_binary, kernal)
# 3、调用膨胀函数
img_erode_dilate = cv2.dilate(img_erode, kernal)
cv2.imshow('image_binary', img_binary)
cv2.imshow('image_erode', img_erode)
cv2.imshow('image_erode_dilate', img_erode_dilate)
cv2.waitKey(0)

⑤仿射变换函数简单应用

python 复制代码
import cv2

img = cv2.imread('5.png')

m = cv2.getRotationMatrix2D((img.shape[1] / 2, img.shape[0] / 2), 45, 0.5)
img_new = cv2.warpAffine(img, m, (img.shape[0], img.shape[1]), flags=cv2.INTER_LINEAR, borderMode=cv2.BORDER_REFLECT_101)

cv2.imshow('img',img)
cv2.imshow('img_new',img_new)
cv2.waitKey(0)

⑥透视变换函数简单应用

python 复制代码
import cv2
import numpy as np

img = cv2.imread("6.png")

#获取透视变换矩阵
#原图中的4个点
points1 = np.array([[223, 118], [680, 176], [139, 380], [654, 451]],dtype=np.float32)
#目标中的4个点
points2 = np.array([[0, 0], [img.shape[1], 0], [0, img.shape[0]], [img.shape[1], img.shape[0]]],dtype=np.float32)
M = cv2.getPerspectiveTransform(points1, points2)
img_warp=cv2.warpPerspective(img,M,(img.shape[1],img.shape[0]))
cv2.imshow("img",img)
cv2.imshow('img_warp', img_warp)
cv2.waitKey(0)

原图:

相关推荐
其实防守也摸鱼6 分钟前
DeepSeek Harness 开源贡献手记:从 Issue 到 Merge 的完整旅程
android·数据库·学习·ai·oracle·自动化
一隅论数智18 分钟前
给AI找对“富矿“:本体协同的六大应用模式与落地战法
大数据·人工智能·经验分享·笔记·学习·学习方法·政务
夏天的清晨23 分钟前
C++入门学习
开发语言·c++·学习
传奇开心果编程23 分钟前
【ArkUI进阶练中学】第18课:Agent亲和架构与应用智能化改造
学习·ui·华为·harmonyos
代码山河25 分钟前
Java的前世今生:从Oak语言到Java 23的发展史
java·学习·架构·教程·面向对象·项目
一木 之林27 分钟前
《OpenAI库基础学习总结:Client 初始化、流式输出 delta 拼接与多轮历史 messages 全流程拆解》
人工智能·学习·计算机视觉·stable diffusion·aigc
m4Rk_32 分钟前
【论文阅读】Agent 记忆机制(90):HyperMem——用超图建模长期记忆中的高阶关联
论文阅读·人工智能·学习·开源·github
星恒随风1 小时前
Linux开发工具详解(二):Git版本控制、GitHub协作与GDB调试实战
linux·笔记·git·学习·github
青柠之夏cc12 小时前
AI与教育:个性化学习助手让“因材施教“成为现实,但代价是隐私?
人工智能·学习
for_ever_love__12 小时前
机器学习入门——手写线性回归与梯度下降
人工智能·python·学习·机器学习·线性回归