[雪峰磁针石博客]计算机视觉opcencv工具深度学习快速实战2 opencv快速入门

简介: pandas 0.23.4 官方文档.pdf Python Data Science Handbook - 2017.pdf 《Python数据科学手册》是对以数据深度需求为中心的科学、研究以及针对计算和统计方法的参考书。

opencv基本操作

# -*- coding: utf-8 -*-
# Author:    xurongzhong#126.com wechat:pythontesting qq:37391319
# 技术支持 钉钉群:21745728(可以加钉钉pythontesting邀请加入) 
# qq群:144081101 591302926  567351477
# CreateDate: 2018-11-17

import imutils
import cv2

# 读取图片信息
image = cv2.imread("jp.png")
(h, w, d) = image.shape
print("width={}, height={}, depth={}".format(w, h, d))

# 显示图片
cv2.imshow("Image", image)
cv2.waitKey(0)

# 访问像素
(B, G, R) = image[100, 50]
print("R={}, G={}, B={}".format(R, G, B))

# 选取图片区间 ROI (Region of Interest)
roi = image[60:160, 320:420]
cv2.imshow("ROI", roi)
cv2.waitKey(0)

# 缩放
resized = cv2.resize(image, (200, 200))
cv2.imshow("Fixed Resizing", resized)
cv2.waitKey(0)

# 按比例缩放
r = 300.0 / w
dim = (300, int(h * r))
resized = cv2.resize(image, dim)
cv2.imshow("Aspect Ratio Resize", resized)
cv2.waitKey(0)

# 使用imutils缩放
resized = imutils.resize(image, width=300)
cv2.imshow("Imutils Resize", resized)
cv2.waitKey(0)

# 顺时针旋转45度
center = (w // 2, h // 2)
M = cv2.getRotationMatrix2D(center, -45, 1.0)
rotated = cv2.warpAffine(image, M, (w, h))
cv2.imshow("OpenCV Rotation", rotated)
cv2.waitKey(0)

# 使用imutils旋转
rotated = imutils.rotate(image, -45)
cv2.imshow("Imutils Rotation", rotated)
cv2.waitKey(0)


# 使用imutils无损旋转
rotated = imutils.rotate_bound(image, 45)
cv2.imshow("Imutils Bound Rotation", rotated)
cv2.waitKey(0)

# apply a Gaussian blur with a 11x11 kernel to the image to smooth it,
# useful when reducing high frequency noise 高斯模糊
# https://www.pyimagesearch.com/2016/07/25/convolutions-with-opencv-and-python/
blurred = cv2.GaussianBlur(image, (11, 11), 0)
cv2.imshow("Blurred", blurred)
cv2.waitKey(0)

# 画框
output = image.copy()
cv2.rectangle(output, (320, 60), (420, 160), (0, 0, 255), 2)
cv2.imshow("Rectangle", output)
cv2.waitKey(0)

# 画圆
output = image.copy()
cv2.circle(output, (300, 150), 20, (255, 0, 0), -1)
cv2.imshow("Circle", output)
cv2.waitKey(0)

# 划线
output = image.copy()
cv2.line(output, (60, 20), (400, 200), (0, 0, 255), 5)
cv2.imshow("Line", output)
cv2.waitKey(0)

# 输出文字
output = image.copy()
cv2.putText(output, "https://china-testing.github.io", (10, 25), 
    cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 255, 0), 2)
cv2.imshow("Text", output)
cv2.waitKey(0)

原图:

图片.png

选取图片区间 ROI (Region of Interest)

图片.png

缩放

图片.png

按比例缩放

图片.png

旋转

图片.png

使用imutils无损旋转

图片.png

高斯模糊

图片.png

画框

图片.png

画圆

图片.png

划线

图片.png

输出文字

Text_screenshot_17.11.2018.png

执行时的输出

$ python opencv_tutorial_01.py 
width=600, height=322, depth=3
R=41, G=49, B=37

本节英文原版代码下载

关于旋转这块,实际上pillow做的更好。比如同样逆时针旋转90度。

opencv的实现:

import imutils
import cv2

image = cv2.imread("jp.png")
rotated = imutils.rotate(image, 90)
cv2.imshow("Imutils Rotation", rotated)
cv2.waitKey(0)

Text_screenshot_17.11.2018.png

pillow的实现:

from PIL import Image

im = Image.open("jp.png")
im2 = im.rotate(90, expand=True)
im2.show()

test.jpg

更多参考: python库介绍-图像处理工具pillow中文文档-手册(2018 5.*)

deep_learning_face_detection_opencv.gif

参考资料

识别俄罗斯方块

# -*- coding: utf-8 -*-
# Author:    xurongzhong#126.com wechat:pythontesting qq:37391319
# 技术支持 钉钉群:21745728(可以加钉钉pythontesting邀请加入) 
# qq群:144081101 591302926  567351477
# CreateDate: 2018-11-19
# python opencv_tutorial_02.py --image tetris_blocks.png

import argparse
import imutils
import cv2

ap = argparse.ArgumentParser()
ap.add_argument("-i", "--image", required=True,
    help="path to input image")
args = vars(ap.parse_args())

image = cv2.imread(args["image"])
cv2.imshow("Image", image)
cv2.waitKey(0)

# 转为灰度图
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
cv2.imshow("Gray", gray)
cv2.waitKey(0)

# 边缘检测
edged = cv2.Canny(gray, 30, 150)
cv2.imshow("Edged", edged)
cv2.waitKey(0)

# 门限
thresh = cv2.threshold(gray, 225, 255, cv2.THRESH_BINARY_INV)[1]
cv2.imshow("Thresh", thresh)
cv2.waitKey(0)

# 发现边缘
cnts = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL,
    cv2.CHAIN_APPROX_SIMPLE)
cnts = cnts[0] if imutils.is_cv2() else cnts[1]
output = image.copy()

# loop over the contours
for c in cnts:
    # draw each contour on the output image with a 3px thick purple
    # outline, then display the output contours one at a time
    cv2.drawContours(output, [c], -1, (240, 0, 159), 3)
    cv2.imshow("Contours", output)
    cv2.waitKey(0)

# draw the total number of contours found in purple
text = "I found {} objects!".format(len(cnts))
cv2.putText(output, text, (10, 25),  cv2.FONT_HERSHEY_SIMPLEX, 0.7,
    (240, 0, 159), 2)
cv2.imshow("Contours", output)
cv2.waitKey(0)

# we apply erosions to reduce the size of foreground objects
mask = thresh.copy()
mask = cv2.erode(mask, None, iterations=5)
cv2.imshow("Eroded", mask)
cv2.waitKey(0)

# 扩大
mask = thresh.copy()
mask = cv2.dilate(mask, None, iterations=5)
cv2.imshow("Dilated", mask)
cv2.waitKey(0)

# a typical operation we may want to apply is to take our mask and
# apply a bitwise AND to our input image, keeping only the masked
# regions
mask = thresh.copy()
output = cv2.bitwise_and(image, image, mask=mask)
cv2.imshow("Output", output)
cv2.waitKey(0)

原图和灰度图:

图片.png

边缘检测

图片.png

门限

图片.png

轮廓

图片.png

查找结果

图片.png

腐蚀和扩张

图片.png

图片.png

屏蔽和位操作

图片.png

串在一起执行

$ python opencv_tutorial_02.py --image tetris_blocks.png

test.gif

相关文章
|
机器学习/深度学习 编解码 人工智能
计算机视觉五大技术——深度学习在图像处理中的应用
深度学习利用多层神经网络实现人工智能,计算机视觉是其重要应用之一。图像分类通过卷积神经网络(CNN)判断图片类别,如“猫”或“狗”。目标检测不仅识别物体,还确定其位置,R-CNN系列模型逐步优化检测速度与精度。语义分割对图像每个像素分类,FCN开创像素级分类范式,DeepLab等进一步提升细节表现。实例分割结合目标检测与语义分割,Mask R-CNN实现精准实例区分。关键点检测用于人体姿态估计、人脸特征识别等,OpenPose和HRNet等技术推动该领域发展。这些方法在效率与准确性上不断进步,广泛应用于实际场景。
1973 64
计算机视觉五大技术——深度学习在图像处理中的应用
|
机器学习/深度学习 数据采集 人工智能
深度学习实战指南:从神经网络基础到模型优化的完整攻略
🌟 蒋星熠Jaxonic,AI探索者。深耕深度学习,从神经网络到Transformer,用代码践行智能革命。分享实战经验,助你构建CV、NLP模型,共赴二进制星辰大海。
|
机器学习/深度学习 人工智能 TensorFlow
基于TensorFlow的深度学习模型训练与优化实战
基于TensorFlow的深度学习模型训练与优化实战
928 3
|
机器学习/深度学习 传感器 算法
行人闯红灯检测:基于计算机视觉与深度学习的智能交通解决方案
随着智能交通系统的发展,传统的人工交通违法判断已难以满足需求。本文介绍了一种基于计算机视觉与深度学习的行人闯红灯自动检测系统,涵盖信号灯状态检测、行人检测与跟踪、行为分析及违规判定与报警四大模块,旨在提升交通管理效率与安全性。
|
机器学习/深度学习 监控 算法
车辆违停检测:基于计算机视觉与深度学习的自动化解决方案
随着智能交通技术的发展,传统人工交通执法方式已难以满足现代城市需求,尤其是在违法停车监控与处罚方面。本文介绍了一种基于计算机视觉和深度学习的车辆违停检测系统,该系统能自动监测、识别并报警违法停车行为,大幅提高交通管理效率,降低人力成本。通过使用YOLO算法进行车辆检测,结合区域分析判断车辆是否处于禁停区,实现了从车辆识别到违停判定的全流程自动化。此系统不仅提升了交通管理的智能化水平,也为维护城市交通秩序提供了技术支持。
|
机器学习/深度学习 人工智能 TensorFlow
探索深度学习与计算机视觉的融合:构建高效图像识别系统
探索深度学习与计算机视觉的融合:构建高效图像识别系统
650 0
|
机器学习/深度学习 计算机视觉
深度学习与计算机视觉的最新进展
深度学习与计算机视觉的最新进展
|
计算机视觉
Opencv学习笔记(三):图像二值化函数cv2.threshold函数详解
这篇文章详细介绍了OpenCV库中的图像二值化函数`cv2.threshold`,包括二值化的概念、常见的阈值类型、函数的参数说明以及通过代码实例展示了如何应用该函数进行图像二值化处理,并展示了运行结果。
5044 0
Opencv学习笔记(三):图像二值化函数cv2.threshold函数详解
|
算法 计算机视觉
opencv图像形态学
图像形态学是一种基于数学形态学的图像处理技术,它主要用于分析和修改图像的形状和结构。
486 4

热门文章

最新文章