网站首页 > 技术文章 正文
作者:俊欣
来源:关于数据分析与可视化
今天小编来给大家介绍3个干货满满的计算机视觉方向的Python实战项目,主要用到的库有
- opencv-python
- numpy
- pillow
要是大家所配置的环境当中没有这几个模块的话,就需要先用pip命令下载安装
pip install opencv-python numpy pillow
边缘检测
边缘检测的基本思想就是简化图像信息,使用边缘线代表图像所携带信息,而这次我们要用到的则是Canny边缘检测算子,在Opencv当中需要调用的是cv.canny()方法即可,代码如下
import cv2 as cv
import matplotlib.pyplot as plt
img = cv.imread('导入图像的路径',0)
edges = cv.Canny(img,100,200)
plt.subplot(121)
plt.imshow(img, cmap='gray')
.........
plt.show()
output
将照片变成素描风格
我们最终要实现的目的在于将照片变成素描风格,大致的逻辑在于首先需要将图片变成灰色图像然后反转,在反转之后进行模糊化处理,代码如下
import cv2
img = cv2.imread("导入照片的路径")
## 将照片灰度化处理
gray_image = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
## 将灰度化的照片反转处理
inverted_gray_image = 255-gray_image
## 将反转的照片模糊化处理
blurred_inverted_gray_image = cv2.GaussianBlur(inverted_gray_image, (19,19),0)
## 再一次的进行反转
inverted_blurred_image = 255-blurred_inverted_gray_image
### 颜色减淡混合处理
sketck = cv2.divide(gray_image, inverted_blurred_image,scale= 256.0)
cv2.imshow("Original Image",img)
cv2.imshow("Pencil Sketch", sketck)
cv2.waitKey(0)
output
判断形状
现在我们需要来判断图片当中图形的轮廓,而识别轮廓的算法在opencv模块当中是有内置的,代码如下
import cv2
import numpy as np
from matplotlib import pyplot as plt
# 导入照片
img = cv2.imread('3.png')
# 将照片灰度化处理,当然要是您的照片已经是黑白的,就可以跳过这一步
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# setting threshold of the gray image
_, threshold = cv2.threshold(gray, 127, 255, cv2.THRESH_BINARY)
# 识别轮廓的方法
contours, _ = cv2.findContours(
threshold, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
i = 0
for contour in contours:
# cv2.approxPloyDP() function to approximate the shape
approx = cv2.approxPolyDP(contour, 0.01 * cv2.arcLength(contour, True), True)
# 找到图片的中心点
M = cv2.moments(contour)
if M['m00'] != 0.0:
x = int(M['m10'] / M['m00'])
y = int(M['m01'] / M['m00'])
# 将轮廓的名字放在各个图形的中央
if len(approx) == 3:
cv2.putText(img, 'Triangle', (x, y),
cv2.FONT_HERSHEY_SIMPLEX, 0.6, (0, 0, 0), 2)
elif len(approx) == 4:
.......
elif len(approx) == 5:
......
elif len(approx) == 6:
......
else:
......
# 将最后的图形呈现出来
cv2.imshow('shapes', img)
cv2.waitKey(0)
cv2.destroyAllWindows()
output
猜你喜欢
- 2024-11-20 Python基础编程——算术运算
- 2024-11-20 Python字符串方法之-字符串填充
- 2024-11-20 Python第14题:最长公共前缀【leetcode】
- 2024-11-20 从 0 到 1:构建强大且易用的规则引擎
- 2024-11-20 python笔记8:静静一起来学习-字符串相关方法
- 2024-11-20 零基础Python完全自学教程14:Python中的序列知识详解
- 2024-11-20 你需要了解的最重要的Python概念
- 2024-11-20 Python整数缓存机制
- 2024-11-20 老鸟进阶必备技能,看懂显示器参数
- 2024-11-20 Python案例:使用for…in循环完成内容长度的计算
- 最近发表
-
- 如何在 Linux 上安装 Java_怎么在linux中安装jdk
- Linux中tar命令打包路径相关问题_linux怎么用tar打包一个目录
- 常用linux系统常用扫描命令汇总_常用linux系统常用扫描命令汇总表
- VM下linux虚拟机新建过程(有图)_linux虚拟机创建新用户命令
- 系统小技巧:迁移通过Wubi方式安装的Ubuntu系统
- 文件系统(八):Linux JFFS2文件系统工作原理、优势与局限
- 如何利用ftrace精确跟踪特定进程调度信息
- prometheus网络监控之fping-exporter
- hyper linux的实操步骤,hyper-v批量管理工具的使用指南
- 2021年,运维工程师笔试真题(二)(附带答案)
- 标签列表
-
- cmd/c (57)
- c++中::是什么意思 (57)
- sqlset (59)
- ps可以打开pdf格式吗 (58)
- phprequire_once (61)
- localstorage.removeitem (74)
- routermode (59)
- vector线程安全吗 (70)
- & (66)
- java (73)
- org.redisson (64)
- log.warn (60)
- cannotinstantiatethetype (62)
- js数组插入 (83)
- resttemplateokhttp (59)
- gormwherein (64)
- linux删除一个文件夹 (65)
- mac安装java (72)
- reader.onload (61)
- outofmemoryerror是什么意思 (64)
- flask文件上传 (63)
- eacces (67)
- 查看mysql是否启动 (70)
- java是值传递还是引用传递 (58)
- 无效的列索引 (74)