Opencv-Python 进阶

Opencv-Python 进阶,第1张

OpenCV-Python 进阶

文章目录 OpenCV-Python 进阶1.Rescale & Resize图片的缩放和调整大小1.1 Rescale1.2 Resize 2. Color Space颜色空间的相互转换2.1 BGR --> Gray2.2 BGR –> HSV2.3 BGT–>LAB2.4 BGR—>RGB2.5 lab –> BGR 3. 颜色通道的split&merge3.1 通道分离Split3.2 通道合并Merge 4. 图片平滑Blur4.1 高斯模糊4.2均值模糊4.3中值模糊4.4双边滤波 5.按位 *** 作Bitwise6.掩膜Masking7.色域直方图显示-Histogram Computation7.1灰度直方图7.2彩色直方图 8. 阈值处理-Threshold8.1简单阈值化处理8.2自适应阈值化处理 9.边缘检测-Edge Dection9.1 Laplacian-拉普拉斯算子9.2 Sobel9.3 Canny


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)

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)

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)

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

4. 图片平滑Blur 4.1 高斯模糊
gauss = cv.GaussianBlur(img, (3,3), 0)
cv.imshow('Gaussian Blur', gauss)

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

kernal=3x3

kernal=7x7

4.2均值模糊
average = cv.blur(img, (3,3))
cv.imshow('Average Blur', average)

4.3中值模糊
median = cv.medianBlur(img, 3)
cv.imshow('Median Blur', median)

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)

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)

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] )

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之间

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)

9.边缘检测-Edge Dection 9.1 Laplacian-拉普拉斯算子
lap = cv.Laplacian(gray, cv.CV_64F)
lap = np.uint8(np.absolute(lap))
cv.imshow('Laplacian', lap)

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)


【学习资料】:

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

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/756171.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-04-30
下一篇 2022-05-18

发表评论

登录后才能评论

评论列表(0条)

保存