#author("2023-09-26T17:27:28+08:00","default:Admin","Admin")
#author("2023-09-27T09:14:51+08:00","default:Admin","Admin")
[[Python]]

&color(red){※This article is based on Python 3.7.3};

#contents

* 环境 [#ib21716f]

安装命令

 pip install opencv-python

** 简单的例程 [#v98560f7]

#codeprettify{{
import cv2 as cv
img = cv.imread("2.jpg")
cv.imshow("2", img)
cv.waitKey(0)
cv.destroyAllWindows()
}}

* 图片处理 [#d7da9319]

** 灰度化与二值化 [#kb9fd4c4]

图像灰度化
 gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)  

图像二值化
 ret, binary = cv2.threshold(gray, 127, 255, cv2.THRESH_BINARY)  


** 获取二值图像轮廓及中心点坐标代码 [#o0b3a010]

#codeprettify{{
groundtruth = cv2.imread(groundtruth_path)[:, :, 0]
h1, w1 = groundtruth.shape
contours, cnt = cv2.findContours(groundtruth.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
if len(contours) != 1:#轮廓总数
    continue
M = cv2.moments(contours[0])  # 计算第一条轮廓的各阶矩,字典形式
center_x = int(M["m10"] / M["m00"])
center_y = int(M["m01"] / M["m00"])
image = np.zeros([h1, w1], dtype=groundtruth.dtype)
cv2.drawContours(image, contours, 0, 255, -1)#绘制轮廓,填充
cv2.circle(image, (center_x, center_y), 7, 128, -1)#绘制中心点
cv2.imwrite("1.png", image)
}}

* 图片降噪 [#pdfc57f0]

** 中值滤波 [#x7c43703]

中值滤波是一种简单而有效的去噪方法,特别适合去除椒盐噪声(随机黑白像素点)。它可以有效地保留图像的边缘信息和细节,但它也可能导致图像变得模糊,特别是在大尺寸滤波器的情况下。
#codeprettify{{
import cv2

img = cv2.imread('image.jpg')
median = cv2.medianBlur(img, 5)
cv2.imshow('Median filter', median)
cv2.waitKey(0)
cv2.destroyAllWindows()
}}

** 高斯滤波 [#w69fe7af]

高斯滤波器是一种线性滤波器,适用于去除高斯噪声(随机高斯分布的噪声)。它通过对周围像素的加权平均来减少噪声,并在去除噪声的同时保留图像的边缘信息。与中值滤波相比,它可以更好地控制模糊程度,但它不能有效地去除椒盐噪声。

#codeprettify{{
import cv2

img = cv2.imread('image.jpg')
gaussian = cv2.GaussianBlur(img, (5,5), 0)
cv2.imshow('Gaussian filter', gaussian)
cv2.waitKey(0)
cv2.destroyAllWindows()
}}

** 双边滤波 [#j609d9fc]

双边滤波是一种非线性滤波器,可以同时平滑像素的颜色和保留边缘信息。它可以去除高斯噪声和椒盐噪声,并且相对于其他滤波器,双边滤波器的效果更好。但是,双边滤波器需要更长的处理时间,并且可能会在图像中引入一些新的噪声。

#codeprettify{{
import cv2

img = cv2.imread('image.jpg')
bilateral = cv2.bilateralFilter(img, 9, 75, 75)
cv2.imshow('Bilateral filter', bilateral)
cv2.waitKey(0)
cv2.destroyAllWindows()
}}

** 小波变换 [#z3899189]

小波变换可以分解图像成不同频率的细节信息,从而可以选择性地去除噪声。小波变换通常用于处理高斯噪声和其他复杂的噪声类型。小波变换可以同时保留图像的边缘和细节信息,但它可能会引入一些新的噪声,并且需要更长的处理时间。
#codeprettify{{
import cv2
import pywt

img = cv2.imread('image.jpg', 0)
coeffs = pywt.dwt2(img, 'haar')
cA, (cH, cV, cD) = coeffs
cv2.imshow('Wavelet denoising', pywt.idwt2((cA, (None, None, None)), 'haar'))
cv2.waitKey(0)
cv2.destroyAllWindows()
}}

* 调用USB摄像头 [#m99c483f]

设置相机id之后还要设置一下分辨率,否则某些相机,第二次打不开画面
#codeprettify{{
video = int(input("please enter the number 0 or 1 or 2: "))
cap = cv2.VideoCapture(video,cv2.CAP_DSHOW)
cap.set(cv2.CAP_PROP_FRAME_WIDTH, 1280)  # 设置分辨率
cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 480)

while cap.isOpened():
    ...

#使用完后,要释放
cap.release()
cv2.destroyAllWindows()
}}

有个USB摄像头不使用 cv2.CAP_DSHOW 参数会报错
 CvCapture_MSMF::grabFrame videoio(MSMF): can't grab frame. Error: -2147483638


#hr();
コメント:
#comment_kcaptcha

トップ   編集 差分 バックアップ 添付 複製 名前変更 リロード   新規 一覧 検索 最終更新   ヘルプ   最終更新のRSS