Loading

OpenCV图像处理学习笔记-Day02

OpenCV图像处理学习笔记-Day02

第13课:基础知识-阈值分割

image-20200929090938688 image-20200929091137490 image-20200929091227926 image-20200929091315374 image-20200929091421081 image-20200929091513487 image-20200929091546908 image-20200929091627724

第14课:threshold函数

函数threshold

retval, dst = cv2.threshold(src, thresh, maxval, type)

retval: 阈值

dst: 处理结果

src: 原图像

thresh: 阈值

maxval: 最大值

type: 类型

image-20200929094609752

代码实例

import cv2


img = cv2.imread('./img/lena512.bmp')
_, img_threshold_binary = cv2.threshold(img, 127, 255, cv2.THRESH_BINARY)  # Threshold Binary
_, img_threshold_binary_inverted = cv2.threshold(img, 127, 255, cv2.THRESH_BINARY_INV)
_, img_thresholds = cv2.threshold(img, 127, 255, cv2.THRESH_TRUNC)
_, img_threshold2zero_inv = cv2.threshold(img, 127, 255, cv2.THRESH_TOZERO_INV)
_, img_threshold2zero = cv2.threshold(img, 127, 255, cv2.THRESH_TOZERO)


cv2.imshow('original', img)
cv2.imshow('threshold binay', img_threshold_binary)
cv2.imshow('threshold binary inverted', img_threshold_binary_inverted)
cv2.imshow('thresholds', img_thresholds)
cv2.imshow('threshold to zero inverted', img_threshold2zero_inv)
cv2.imshow('threshold to zero', img_threshold2zero)

cv2.waitKey(0)
cv2.destroyAllWindows()

第15课:图像平滑-均值滤波

image-20200929100215714

image-20200929100245464

image-20200929100257685

image-20200929100326017

image-20200929100414451

函数blur

处理结果 = cv2.blur(原始图像, 核大小)

核大小:以(宽度,高度)形式表示的元组

image-20200929100629214

实例代码

import cv2


img = cv2.imread('image_dir')
img_blur = cv2.blur(img, (5, 5))

cv2.imshow('original', img)
cv2.imshow('result', img_blur)

cv2.waitKey(0)
cv2.destroyAllWindows()

image-20200929100829020

image-20200929100843425

第16课:图像平滑-方框滤波

函数boxFilter

处理结果 = cv2.boxFilter(原始图像, 目标图像深度, 核大小, normalize属性)

目标图像深度: int类型的目标图像深度。通常使用"-1"表示与原始图像一致。

核大小:

image-20200929101325472

normalize属性: 是否对目标图像进行归一化处理。

image-20200929101505782

image-20200929101801151

第17课:图像平滑-高斯滤波

image-20200929101903577

image-20200929101958036

GaussianBlur函数

dst = cv2.GaussianBlur(src, ksize, sigmaX)

src: 原始图像

ksize: 核大小

sigmaX: X的方向方差

image-20200929102253174 image-20200929102317169 image-20200929102350977

第18课:图像平滑-中值滤波

image-20200929102641899

image-20200929102737361

image-20200929102817609

第19课:形态学转换-图像腐蚀

image-20200929103023498

image-20200929103041627

1. 基础

  1. 形态学转换主要针对的是二值图像。
  2. 两个输入对象。对象1:二值图像,对象2:卷积核

image-20200929103421457

image-20200929103503921

2. 函数erode

dst = cv2.erode(src, kernel, iterations)

dst: 处理结果

src: 源图像

kernel: 卷积核

iterations: 迭代次数

3. 实例代码

import cv2
import numpy as np


img = cv2.imread('image_dir')

kernel = np.ones((5, 5), np.uint8)
erosion = cv2.erode(img, kernel)
erosion_ = cv2.erode(img, kernel, 9)

cv2.imshow('original', img)
cv2.imshow('erosion', erosion)

cv2.waitKey(0)
cv2.destroyAllWindows()

image-20200929104222154 image-20200929104310660

第20课:形态学转换-图像膨胀

image-20200929104522817

image-20200929104615047

image-20200929104623701

image-20200929104716938

image-20200929104730815

image-20200929104801525

dliate函数

image-20200929105427808

第21课:形态学转换-开运算

20200929110115.png

20200929110152.png

image-20200929120053996

import cv2
import numpy as np


img = cv2.imread('image_dir')
kernel = np.ones((5, 5), np.uint8)  # (10, 10)

img_open = cv2.morphologyEx(img, cv2.MORPH_OPEN, kernel)

cv2.imshow('orginal', img)
cv2.imshow('result', img_open)

cv2.waitKey(0)
cv2.destroyAllWindows()

第22课:形态学转换-闭运算

image-20200929120905605

image-20200929120915175

image-20200929120938444

image-20200929120947854

第23课:形态学转换-梯度运算

image-20200929121135989

img

img

第24课:形态学转换-图像顶帽

image-20200929121737159

image-20200929121908990

第25课:形态学转换-黑帽操作

image-20200929122042547

image-20200929122345110

第26课:图像梯度-sobel算子理论

image-20200929131922412

image-20200929131949603

image-20200929132101467

image-20200929132111541

第27课:sobel算子及函数使用

image-20200929132614802

ddepth: 处理结果图像的深度

通常情况下,可以将该参数的值设为-1,让处理结果与原始图像保持一致

img

image-20200929133923143

image-20200929133935083

image-20200929133956839

image-20200929135119274

image-20200929135134313

image-20200929135205905

image-20200929135212288

image-20200929135225338

import cv2


img = cv2.imread('./img/man.bmp')

sobelx = cv2.Sobel(img, cv2.CV_64F, 1, 0)
sobelx = cv2.convertScaleAbs(sobelx)

sobely = cv2.Sobel(img, cv2.CV_64F, 0, 1)
sobely = cv2.convertScaleAbs(sobely)

output_img = cv2.addWeighted(sobelx, 0.5, sobely, 0.5, 0)

cv2.imshow('original', img)
cv2.imshow('output image', output_img)

cv2.waitKey()
cv2.destroyAllWindows()

第28课:scharr算子函数及其使用

image-20200929140644781

image-20200929173739794

image-20200929173801382

image-20200929173830566

image-20200929173931350

image-20200929173957635

image-20200929174028851

第29课:sobel算子和scharr算子的比较

image-20200929174350774

image-20200929174518648

image-20200929174532782

第30课:laplacian算子及使用

image-20200929174749858

image-20200929174812098

image-20200929174854084

image-20200929174944912

image-20200929175039817

image-20200929175047939

posted @ 2020-09-29 17:53  coderchen01  阅读(155)  评论(0编辑  收藏  举报