opencv入门笔记(一)

图像处理基本操作

图像读取

cv2.imread(filename[, flags]) 

读取图像,返回该图像的像素值

参数描述
filename图片文件名(路径中不要有中文)
flags(可选参数值)
大于0时返回3通道彩色图像
等于0时返回灰度图像

返回值类型:numpy.ndarray

图像显示

cv2.imshow(winname,mat)

生成一个窗口,显示图像

参数描述
winname生成窗口的名称
mat待显示的图像

图像保存

cv2.imwrite(filename, image[, params])

将处理后的图像保存为文件

参数描述
filename要保存的图像文件名
image要保存的图像(数组)

图像翻转

cv2.flip(src, flipCode[, dst])
参数描述
src原始图像
flipCode翻转类型
flipCode=0:沿着x轴翻转
flipCode>0:沿着y轴翻转
flipCode<0:同时沿着xy轴翻转

图像缩放

cv2.resize(src, dsize[, dst[, fx[, fy[, interpolation]]]]) 
参数描述
src原始图像
dsize目标图像大小
dst目标图像
fx水平方向缩放比例
fy竖直方向缩放比例

如何绘制简单的图形

绘制线段

cv.line(img, pt1, pt2, color, thickness=1, lineType=8, shift=0) 
参数描述
img图像或画布
pt1线段起点(x,y)
pt2线段终点(x,y)
color线段颜色
thickness线段宽度

示例(在画布上绘制线段)

import numpy as np
import cv2


canvas=np.zeros((500,300,3),np.int8)

canvas=cv2.line(canvas,(50,50),(250,50),(255,0,0),5)
canvas=cv2.line(canvas,(150,50),(0,200),(0,255,0),10)
canvas=cv2.line(canvas,(0,200),(300,200),(0,0,255),15)
canvas=cv2.line(canvas,(300,200),(150,50),(255,0,0),20)
canvas=cv2.line(canvas,(150,200),(150,500),(255,255,0),25)

cv2.imwrite('../imgs/draw_lines.jpg',canvas)

运行结果
在这里插入图片描述

绘制矩形

cv.rectangle(img, pt1, pt2, color, thickness=1, lineType=8, shift=0)
参数描述
img图像或画布
pt1矩形左上角坐标(x,y)
pt2矩形右下角坐标(x,y)
color线段颜色
thickness线段宽度,当该值小于0时将绘制一个实心矩形

示例(标记图片中的花朵位置)

import cv2

img=cv2.imread('../imgs/R-C_resize.jpg')
# 生成一个空心矩形
canvas=cv2.rectangle(img,(50,23),(347,286),(0,0,255),1)
# 生成两个实心矩形
canvas=cv2.rectangle(canvas,(10,23),(40,53),(0,0,255),-1)
canvas=cv2.rectangle(canvas,(10,256),(40,286),(0,0,255),-1)
cv2.imwrite('rect_img.jpg',canvas)

运行结果
在这里插入图片描述

绘制圆形

cv2.circle(img, center, radius, color[, thickness[, lineType[, shift]]])
参数描述
img图像或画布
center圆心坐标
radius圆半径
color线段颜色
thickness线段宽度,当该值小于0时将绘制一个实心圆

示例(同心圆绘制)

import cv2
import numpy as np

#生成画布
canvas=np.zeros((500,600,3),np.uint8)
# 定义参数
radius=180
color=(255,255,255)
for i in range(4):
    canvas=cv2.circle(canvas,(300,250),radius,color,10)
    radius-=30
canvas=cv2.circle(canvas,(300,250),radius,color,-1)
cv2.imwrite('circle.jpg',canvas)

运行结果
在这里插入图片描述

绘制多边形

cv.polyLine(img, polys, isClosed, color, thickness=1, lineType=8, shift=0)
参数描述
img图像或画布
polys由多边形各个顶点组成的列表
isClosed多边形是否闭合
color线段颜色
thickness线段宽度

示例(绘制两个六边形)

import cv2
import numpy as np
polys = np.array([[(200, 200),
                   (400, 200),
                   (500, 200 + 100 * np.sqrt(3)),
                   (400, 200 + 200 * np.sqrt(3)),
                   (200, 200 + 200 * np.sqrt(3)),
                   (100, 200 + 100 * np.sqrt(3))]],dtype=np.int32)
canvas = np.zeros((999, 999, 3), np.uint8)
canvas = cv2.polylines(canvas, [polys], isClosed=True, color=(255, 255, 255),thickness=10)
canvas = cv2.polylines(canvas, [polys+300], isClosed=True, color=(255, 255, 255),thickness=10)
cv2.imwrite('../imgs/polys.jpg', canvas)

运行结果
在这里插入图片描述

运行过程中的一个报错:p.checkVector(2, CV_32S) >= 0 in function ‘cv::polylines’
原因:上面的代码中顶点坐标出现了 3 \sqrt3 3 ,使用dtype参数将其转化为整数即可

绘制文字

cv2.putText(img, text, org, fontFace, fontScale, color[, thickness[, linetype[, bottomLeftOrigin]]])
参数描述
img图像或画布
text要绘制的文字内容
org文字字符串左下角坐标
fontFace字体样式
fontSacle字体大小
color字体颜色

示例(绘制文字)

import cv2
import numpy as np
# 生成画布
canvas=np.zeros((240,240,3),np.uint8)

cv2.putText(canvas,'hello',(20,60),cv2.FONT_HERSHEY_SCRIPT_SIMPLEX,2,(0,255,0),5)
cv2.putText(canvas,'hello',(20,120),cv2.FONT_ITALIC+cv2.FONT_HERSHEY_TRIPLEX,2,(0,255,0),5)
cv2.putText(canvas,'hello',(20,180),cv2.FONT_HERSHEY_SIMPLEX,2,(0,255,0),5)
cv2.imwrite('../imgs/font_img.jpg',canvas)

运行结果
在这里插入图片描述

阈值处理函数

cv2.threshold(src, thresh, maxval, type[, dst])
参数描述
src被处理的图像
thresh阈值
maxval最大阈值
type阈值处理类型
dst经阈值处理后的图像

阈值处理类型

在这里插入图片描述

阈值处理类型描述范围
cv2.THRESH_BINARY二值化像素值>thresh时,取maxval,否则取00或maxval
cv2.THRESH_BINARY_INV反二值化像素值>thresh时,取0,否则取maxval0或maxval
cv2.THRESH_TRUNC截断阈值处理像素值>thresh时,取阈值,否则像素值不变原始像素值或阈值
cv2.THRESH_TOZERO低于阈值零处理像素值>thresh时,像素值不变,否则取00或原始像素值
cv2.THRESH_TOZERO_INV高于阈值零处理像素值>thresh时,像素值取0,否则像素值不变0或原始像素值

示例:查看各种阈值处理的效果

import cv2
import matplotlib.pyplot as plt
import matplotlib

plt.subplots_adjust(hspace=0.5)
matplotlib.rcParams['font.family']='SimHei'
img=cv2.imread('../imgs/R-C_resize.jpg')

plt.subplot(321)
plt.title('原始图像')
plt.imshow(img)

plt.subplot(322)
plt.title('二值化')
plt.imshow(cv2.threshold(img,150,255,cv2.THRESH_BINARY)[1])

plt.subplot(323)
plt.title('反二值化')
plt.imshow(cv2.threshold(img,150,255,cv2.THRESH_BINARY_INV)[1])

plt.subplot(324)
plt.title('低于阈值零处理')# 低于阈值的像素值归零
plt.imshow(cv2.threshold(img,150,255,cv2.THRESH_TOZERO)[1])

plt.subplot(325)
plt.title('高于阈值零处理')
plt.imshow(cv2.threshold(img,150,255,cv2.THRESH_TOZERO_INV)[1])

plt.subplot(326)
plt.title('截断处理')
plt.imshow(cv2.threshold(img,150,255,cv2.THRESH_TRUNC)[1])

plt.show()

运行结果
在这里插入图片描述

注意:在这里笔者使用的是plt.imshow()函数来展示阈值处理后的图像,当你是用cv2.imshow()函数来展示这些图像时,你会发现两者相别很大,后者显示的图像内容如下,这是因为open默认使用BGR通道,但是matplotlibt默认使用RGB通道,所以会造成这种情况,解决这一问题很简单,在plt.imshow()时加入参数cmap='gray'即可

在这里插入图片描述

自适应阈值函数

阈值根据指定方法进行计算

cv2.adaptiveThreshold(src, maxValue, adaptiveMethod, thresholdType, blockSize, C[, dst]) 
参数描述
src待处理的图像
maxValue阈值处理最值
adaptiveMethod自适应阈值计算方法
thresholdType阈值处理类型
dst经过处理后的图像

阈值处理类型

在这里插入图片描述

阈值计算方法

在这里插入图片描述

示例(自适应阈值处理效果)

import cv2
import matplotlib.pyplot as plt
plt.subplots_adjust(hspace=0.5)
import matplotlib
matplotlib.rcParams['font.family']='SimHei'

img=cv2.imread('../imgs/R-C_resize.jpg')
gray_img=cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)# 将图像转换为灰度
MEAN_img=cv2.adaptiveThreshold(gray_img,255,cv2.ADAPTIVE_THRESH_MEAN_C,cv2.THRESH_BINARY,5,2)

GAUSSION_img=cv2.adaptiveThreshold(gray_img,255,cv2.ADAPTIVE_THRESH_GAUSSIAN_C,cv2.THRESH_BINARY,5,2)

plt.subplot(221)
plt.title('原图像')
plt.imshow(img)

plt.subplot(222)
plt.title('MEAN')
plt.imshow(MEAN_img,cmap='gray')

plt.subplot(223)
plt.title('GAUSSION')
plt.imshow(GAUSSION_img,cmap='gray')


plt.show()

运行结果
在这里插入图片描述

To be continue…

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

夺笋123

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值