opencv库
をテンプレートにして作成
[
トップ
] [
新規
|
一覧
|
検索
|
最終更新
|
ヘルプ
|
ログイン
]
開始行:
[[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_B...
** 获取二值图像轮廓及中心点坐标代码 [#o0b3a010]
#codeprettify{{
groundtruth = cv2.imread(groundtruth_path)[:, :, 0]
h1, w1 = groundtruth.shape
contours, cnt = cv2.findContours(groundtruth.copy(), cv2....
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, No...
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 fram...
#hr();
コメント:
#comment_kcaptcha
終了行:
[[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_B...
** 获取二值图像轮廓及中心点坐标代码 [#o0b3a010]
#codeprettify{{
groundtruth = cv2.imread(groundtruth_path)[:, :, 0]
h1, w1 = groundtruth.shape
contours, cnt = cv2.findContours(groundtruth.copy(), cv2....
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, No...
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 fram...
#hr();
コメント:
#comment_kcaptcha
ページ名: