Opencv-Python 进阶

OpenCV-Python 进阶



1.Rescale & Resize图片的缩放和调整大小

Rescale为成比例的同时缩放x轴和y轴,resize为缩放指定尺寸

Resize:调整对象的大小只会改变对象的尺寸(宽度和高度)。例如,当您调整具有特定描边厚度的对象的大小时,当对象调整大小时,描边厚度保持不变

Rescale:缩放对对象进行转换,根据初始X、Y和Z值的倍数(仅限三维对象)调整对象的大小。例如,当缩放具有特定描边厚度的对象时,描边厚度会随着对象的缩放而缩放。这意味着如果你只按对象的x轴值(对象的宽度)缩放对象,对象宽度的描边厚度将与对象高度的描边厚度(y轴)不同。

python - Is there a difference between resizing and rescaling an image in computer vision? - Stack Overflow

1.1 Rescale

def rescaleFrame(frame, scale=0.75):
    # 适用于视频,图片,实时视频
    width = int(frame.shape[1] * scale)#照片宽度
    height = int(frame.shape[0] * scale)#照片高度

    dimensions = (width,height)

    return cv.resize(frame, dimensions, interpolation=cv.INTER_AREA)
def changeRes(width,height):
    # 只适用于实时视频
    capture.set(3,width)
    capture.set(4,height)

1.2 Resize

resized = cv.resize(img, (500,500), interpolation=cv.INTER_CUBIC)#interpolation为插值方式
cv.imshow('Resized', resized)

image-20220417104244452

2. Color Space颜色空间的相互转换

2.1 BGR --> Gray

gray = cv.cvtColor(img, cv.COLOR_BGR2GRAY)#第二个参数为指定的颜色代码
cv.imshow('Gray', gray)

在这里插入图片描述

2.2 BGR –> HSV

hsv = cv.cvtColor(img, cv.COLOR_BGR2HSV)
cv.imshow('HSV', hsv)

在这里插入图片描述

2.3 BGT–>LAB

lab = cv.cvtColor(img, cv.COLOR_BGR2LAB)
cv.imshow('LAB', lab)

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-vfDbcd3x-1650171955573)(https://gitee.com/Miracle_Fan/figure-beds/raw/master/img/image-20220417105433574.png)]

2.4 BGR—>RGB

rgb = cv.cvtColor(img, cv.COLOR_BGR2RGB)
cv.imshow('RGB', rgb)

在这里插入图片描述

2.5 lab –> BGR

lab_bgr = cv.cvtColor(lab, cv.COLOR_LAB2BGR)
cv.imshow('LAB --> BGR', lab_bgr)

在这里插入图片描述

3. 颜色通道的split&merge

3.1 通道分离Split

blank = np.zeros(img.shape[:2], dtype='uint8')

b,g,r = cv.split(img)

cv.imshow('Blue', b)
cv.imshow('Green', g)
cv.imshow('Red', r)

#输出图片像素
print(img.shape)
print(b.shape)
print(g.shape)
print(r.shape)

(427, 640, 3)#三通道BRG图像
(427, 640)
(427, 640)
(427, 640)

在这里插入图片描述

3.2 通道合并Merge

1.显示单色多通道图像

blank = np.zeros(img.shape[:2], dtype='uint8')
#uint8针对图片的特定类型
blue = cv.merge([b,blank,blank])
green = cv.merge([blank,g,blank])
red = cv.merge([blank,blank,r])
cv.imshow('Blue', blue)
cv.imshow('Green', green)
cv.imshow('Red', red)

在这里插入图片描述

  1. 还原为原图像
merged = cv.merge([b,g,r])
cv.imshow('Merged Image', merged)

image-20220417110812336

4. 图片平滑Blur

4.1 高斯模糊

gauss = cv.GaussianBlur(img, (3,3), 0)
cv.imshow('Gaussian Blur', gauss)

卷积核大小越大,图片越模糊

kernal=3x3

在这里插入图片描述

kernal=7x7

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-HOlqvarG-1650171955580)(https://gitee.com/Miracle_Fan/figure-beds/raw/master/img/image-20220417111246974.png)]

4.2均值模糊

average = cv.blur(img, (3,3))
cv.imshow('Average Blur', average)

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-ZtCdHx2O-1650171955581)(https://gitee.com/Miracle_Fan/figure-beds/raw/master/img/image-20220417111541295.png)]

4.3中值模糊

median = cv.medianBlur(img, 3)
cv.imshow('Median Blur', median)

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-Tug30pfL-1650171955581)(https://gitee.com/Miracle_Fan/figure-beds/raw/master/img/image-20220417111615304.png)]

4.4双边滤波

bilateral = cv.bilateralFilter(img, 10, 35, 25)
cv.imshow('Bilateral', bilateral)

在这里插入图片描述

5.按位操作Bitwise

blank = np.zeros((400,400), dtype='uint8')#构建400x400维度黑色像素点

rectangle = cv.rectangle(blank.copy(), (30,30), (370,370), 255, -1)
circle = cv.circle(blank.copy(), (200,200), 200, 255, -1)

cv.imshow('Rectangle', rectangle)
cv.imshow('Circle', circle)

# bitwise AND -->交集
bitwise_and = cv.bitwise_and(rectangle, circle)
cv.imshow('Bitwise AND', bitwise_and)

# bitwise OR --> 并集
bitwise_or = cv.bitwise_or(rectangle, circle)
cv.imshow('Bitwise OR', bitwise_or)

# bitwise XOR --> 异或
bitwise_xor = cv.bitwise_xor(rectangle, circle)
cv.imshow('Bitwise XOR', bitwise_xor)

# bitwise 非
bitwise_not = cv.bitwise_not(circle)
cv.imshow('Circle NOT', bitwise_not)

image-20220417114140325

6.掩膜Masking

circle = cv.circle(blank.copy(), (img.shape[1]//2 + 45,img.shape[0]//2), 100, 255, -1)
cv.imshow('circle',circle )
masked = cv.bitwise_and(img,img,mask=circle)#通过按位与操作使得只在circle里面显示像素,其他像素点都为0
cv.imshow('Circle Shaped Masked Image', masked)

image-20220417120417982

7.色域直方图显示-Histogram Computation

7.1灰度直方图

gray = cv.cvtColor(img, cv.COLOR_BGR2GRAY)
cv.imshow('Gray', gray)
gray_hist = cv.calcHist([gray], [0], mask, [256], [0,256] )

image-20220417115155861

image-20220417115108403

7.2彩色直方图

colors = ('b', 'g', 'r')
for i,col in enumerate(colors):
    hist = cv.calcHist([img], [i], None, [256], [0,256])#第四个参数为直方图份数,第五个参数为像素值区间
    plt.plot(hist, color=col)
    plt.xlim([0,256])#限制像素值在0-256之间

image-20220417115949300

8. 阈值处理-Threshold

8.1简单阈值化处理

#150为阈值,大于裕值的将像素点赋值255
threshold, thresh = cv.threshold(gray, 150, 255, cv.THRESH_BINARY)
cv.imshow('Simple Thresholded', thresh)

threshold, thresh_inv = cv.threshold(gray, 150, 255, cv.THRESH_BINARY_INV )
cv.imshow('Simple Thresholded Inverse', thresh_inv)

8.2自适应阈值化处理

adaptive_thresh = cv.adaptiveThreshold(gray, 255, cv.ADAPTIVE_THRESH_GAUSSIAN_C, cv.THRESH_BINARY, 11, 9)
cv.imshow('Adaptive Thresholding', adaptive_thresh)

image-20220417121233021

9.边缘检测-Edge Dection

9.1 Laplacian-拉普拉斯算子

lap = cv.Laplacian(gray, cv.CV_64F)
lap = np.uint8(np.absolute(lap))
cv.imshow('Laplacian', lap)

image-20220417120816927

9.2 Sobel

sobelx = cv.Sobel(gray, cv.CV_64F, 1, 0)#Sobel(src: Any,ddepth: Any,dx: Any,dy: Any,)
sobely = cv.Sobel(gray, cv.CV_64F, 0, 1)
combined_sobel = cv.bitwise_or(sobelx, sobely)

9.3 Canny

canny = cv.Canny(gray, 150, 175)
 #Canny(image: Any,
  #        threshold1: Any,
   #       threshold2: Any)
cv.imshow('Canny', canny)

image-20220417120927374


【学习资料】:

Learn How to Use the OpenCV Computer Vision Library (freecodecamp.org)

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Miracle Fan

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

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

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

打赏作者

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

抵扣说明:

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

余额充值