OpenCV从入门到放弃

开篇致谢最为致命,首先感谢大佬81页计算机视觉指南

最近公司比较闲,本来应该去刷LeetCode的,但是刷自闭了,于是技能树继续向广度发展,正巧前两天看到这个指南了,于是就学着玩玩。

第一步当然是搞一台linux虚拟机,这里我选择了ubuntu Desktop版,然后是安装python,OpenCV库。


sudo apt-get update && sudo apt-get install python3-pip
# 升级
python3 -m pip install --upgrade pip
# 换源
python3 -m pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple
python3 -m pip install opencv-python imutils

给你们瞅一眼我的目录结构

然后就是跟着教程练习几个常用函数的使用,以下代码不保证可运行,这里我建议用python交互模式练习,用文件形式有点麻烦


import imutils
import cv2
# 这个图片是Pixiv找的,教程上的图我没找到,有一说一,这图画的很好
# 这张图目前没什么要求,有想要圈起来的东西就行
image=cv2.imread("78172570_p0.jpg")
(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("RGB at 100,50:{},{},{}".format(R,G,B))

roi=image[300:450,450:600]
cv2.imshow("ROI",roi)
cv2.waitKey(0)

resized=cv2.resize(image,(720,720))
cv2.imshow("Fixed Resizing",resized)
cv2.waitKey(0)

r=720.0/w
dim=(720,int(h*r))
resized=cv2.resize(image,dim)
cv2.imshow("Aspect Ratio Resize",resized)
cv2.waitKey(0)
resized=imutils.resize(image,height=720)
(h_l,w_l,d_l)=resized.shape
cv2.imshow("Imutils Resize",resized)
cv2.waitKey(0)

center=(w_l//2,h_l//2)
M=cv2.getRotationMatrix2D(center,-45,1.0)
rotated=cv2.warpAffine(resized,M,(w_l,h_l))
cv2.imshow("OpenCV Rotation",rotated)
cv2.waitKey(0)

rotated=imutils.rotate_bound(resized,45)
cv2.imshow("Imutils Bound Rotation",rotated)
cv2.waitKey(0)
#高斯模糊……
blurred=cv2.GaussianBlur(resized,(11,11),0)
cv2.imshow("Blurred",blurred)
cv2.waitKey(0)

output=image.copy()
#output 左上(x,y),右下(x,y)
#我们最常在新闻上看到的长方形
cv2.rectangle(output,(450,300),(600,450),(0,0,255),2)
cv2.imshow("Rectangle",output)
cv2.waitKey(0)

output=image.copy()
#画圈
cv2.circle(output,(525,375),75,(255,0,0),1)1 for 1px,-1 for solid
cv2.imshow("Circle",output)
cv2.waitKey(0)

output=image.copy()
#划线
cv2.line(output,(725,0),(725,500),(0,0,255),3)
cv2.imshow("Line",output)
cv2.waitKey(0)

output=image.copy()
#写字
cv2.putText(output,"EnderCaster Exercise",(10,25),cv2.FONT_HERSHEY_SIMPLEX,0.7,(255,204,102),2)
cv2.imshow("Text",output)
cv2.waitKey(0)

第一部分的基础练习就完了,然后是抠图图像处理


import cv2
import imutils
import argparse
# 同样是Pixiv找的,这张图的要求是背景单一,角色没有关联
image=cv2.imread("split_test.jpg")
cv2.imshow("Image",image)
cv2.waitKey(0)

# 灰度图
gray=cv2.cvtColor(image,cv2.COLOR_BGR2GRAY)
cv2.imshow("Gray",gray)
cv2.waitKey(0)
# 边缘查找
edge=cv2.Canny(gray,30,150)
cv2.imshow("Edge",edge)
cv2.waitKey(0)

# that not work well
# 阈值,用灰阶图处理效果比较好
thresh=cv2.threshold(gray,242,255,cv2.THRESH_BINARY_INV)[1]
cv2.imshow("Threshold",thresh)
cv2.waitKey(0)

# 看名称像是查找封闭区间,所以上一步没处理好这里可能会多出来一些无效的
cnts=cv2.findContours(thresh.copy(),cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)
cnts=imutils.grab_contours(cnts)
output=image.copy()
for c in cnts:
    cv2.drawContours(output,[c],-1,(240,0,159),3)
    cv2.imshow("Contours",output)
    cv2.waitKey(0)
# Rita 人名
text="I found {} Rita!".format(len(cnts))
cv2.putText(output,text,(20,25),cv2.FONT_HERSHEY_SIMPLEX,0.7,(240,0,159),2)
cv2.imshow("Contours",output)
cv2.waitKey(0)
# 去噪
## 腐蚀
## 裁切式...这个去的有点猛
mask=thresh.copy()
mask=cv2.erode(mask,None,iterations=1)
cv2.imshow("Eroded",mask)
cv2.waitKey(0)
## 膨胀
## 扩展式
mask=thresh.copy()
mask=cv2.dilate(mask,None,iterations=3)
cv2.imshow("Dilated",mask)
cv2.waitKey(0)
# 按位与,慎用,效果比较令人迷惑
mask=thresh.copy()
output=cv2.bitwise_and(image,image,mask=mask)
cv2.imshow("Output",output)
cv2.waitKey(0)

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注