Skip to content

Commit 026ff42

Browse files
committed
update opencv
1 parent 7bb3f4b commit 026ff42

10 files changed

+206
-0
lines changed

opencv/day03/30_图像卷积.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#encoding:utf-8
2+
import cv2 as cv
3+
import numpy as np
4+
5+
# 定义一个卷积核
6+
# kernel = np.ones((3,3),np.float32)/9
7+
8+
src = cv.imread("../img/itheima_salt.jpg")
9+
cv.imshow("src",src)
10+
11+
# dst = cv.filter2D(src, -1, kernel)
12+
dst = cv.blur(src, (3, 3))
13+
cv.imshow("dst",dst)
14+
15+
cv.waitKey()

opencv/day03/31_中值滤波.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#encoding:utf-8
2+
import cv2 as cv
3+
4+
src = cv.imread("../img/itheima_salt.jpg")
5+
6+
# 中值滤波
7+
dst = cv.medianBlur(src, 5)
8+
9+
10+
cv.imshow("src",src)
11+
cv.imshow("dst",dst)
12+
cv.waitKey()

opencv/day03/32_高斯模糊.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#encoding: utf-8
2+
import cv2 as cv
3+
4+
src = cv.imread("../img/itheima_salt.jpg")
5+
6+
dst = cv.GaussianBlur(src, (3, 3), 5)
7+
8+
cv.imshow("dst",dst)
9+
cv.imshow("src",src)
10+
cv.waitKey()

opencv/day03/33_Sobel算子.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#encoding:utf-8
2+
import cv2 as cv
3+
4+
src = cv.imread("../img/brain.jpg")
5+
6+
x_sobel = cv.Sobel(src,cv.CV_32F,1,0)
7+
8+
x_sobel = cv.convertScaleAbs(x_sobel);
9+
10+
cv.imshow("xsobel",x_sobel)
11+
12+
y_sobel = cv.Sobel(src,cv.CV_32F,0,1)
13+
14+
y_sobel = cv.convertScaleAbs(y_sobel);
15+
16+
cv.imshow("ysobel",y_sobel)
17+
18+
xy_sobel = cv.addWeighted(x_sobel,0.5,y_sobel,0.5,0);
19+
cv.imshow("xysobel",xy_sobel)
20+
21+
cv.imshow("src",src)
22+
# xy_sobel = cv.Sobel(src,cv.CV_32F,1,1)
23+
#
24+
# xy_sobel = cv.convertScaleAbs(xy_sobel);
25+
#
26+
# cv.imshow("xysobel",xy_sobel)
27+
28+
29+
30+
31+
cv.waitKey()

opencv/day03/34_拉普拉斯算子.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#encoding:utf-8
2+
import cv2 as cv
3+
4+
src = cv.imread("../img/grbq.jpg")
5+
cv.imshow("src",src)
6+
# 使用拉普拉斯算子
7+
dst = cv.Laplacian(src,cv.CV_32F)
8+
# 将数据缩放到0-255
9+
dst = cv.convertScaleAbs(dst)
10+
11+
cv.imshow("dst",dst)
12+
cv.waitKey()

opencv/day03/35_Canny算法.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#encoding:utf-8
2+
import cv2 as cv
3+
4+
src = cv.imread("../img/itheima.jpg",cv.IMREAD_COLOR)
5+
cv.imshow("src",src);
6+
7+
# 将彩色图像转成灰度图像
8+
grayImg = cv.cvtColor(src,cv.COLOR_BGR2GRAY)
9+
10+
11+
12+
def onChange(value):
13+
# 调用Canny算法
14+
dst = cv.Canny(grayImg, value, value*3);
15+
cv.imshow("canny", dst)
16+
# 先命名窗口
17+
cv.namedWindow("canny")
18+
cv.createTrackbar("lowthresh","canny",0,255,onChange)
19+
20+
21+
cv.waitKey()

opencv/day03/36_双边滤波器.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#encoding:utf-8
2+
import cv2 as cv
3+
4+
src = cv.imread("../img/timg.jpg",cv.IMREAD_COLOR)
5+
cv.imshow("src",src)
6+
7+
# dst = cv.bilateralFilter(src,10,50,50);
8+
d = 0;
9+
sigmaColor = 0;
10+
sigmaSpace = 0;
11+
def onChange(value,index):
12+
global d,sigmaSpace,sigmaColor
13+
if index == 0 : # 修改d值
14+
d = value
15+
elif index == 1: # 修改颜色域值
16+
sigmaColor = value
17+
elif index == 2: # 修改空间域范围
18+
sigmaSpace = value
19+
20+
dst = cv.bilateralFilter(src, d, sigmaColor, sigmaSpace);
21+
cv.imshow("filter", dst)
22+
23+
cv.namedWindow("filter")
24+
cv.createTrackbar("d:","filter",0,255,lambda value: onChange(value,0))
25+
cv.createTrackbar("sigmaColor:","filter",0,255,lambda value: onChange(value,1))
26+
cv.createTrackbar("sigmaSpace:","filter",0,255,lambda value: onChange(value,2))
27+
28+
29+
cv.waitKey()

opencv/day03/37_图像的锐化.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#encoding: utf-8
2+
import cv2 as cv
3+
import numpy as np
4+
5+
src = cv.imread("../img/hehua.jpg")
6+
cv.imshow("src",src)
7+
8+
k = 1
9+
10+
kernel = np.array([[-k,-k,-k],[-k,8*k+1,-k],[-k,-k,-k]])
11+
12+
dst = cv.filter2D(src, -1, kernel)
13+
14+
cv.imshow("dst",dst)
15+
cv.waitKey()
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#encoding:utf-8
2+
import cv2 as cv
3+
import numpy as np
4+
5+
src = cv.imread("../img/weiqi.jpg")
6+
7+
# 将彩图转成灰度图
8+
gray = cv.cvtColor(src, cv.COLOR_BGR2GRAY)
9+
10+
# 将图像转成二值图
11+
value,binary = cv.threshold(gray,100,255,cv.THRESH_BINARY_INV);
12+
print value
13+
# 霍夫变换寻找直线
14+
# 距离精度1
15+
rho = 1
16+
# 弧度变换的步长
17+
theta = np.pi/180
18+
# 累加的阈值, 最终的结果,一条直线上至少要包含十个像素点
19+
thresh = 10
20+
21+
lines = cv.HoughLinesP(binary,rho,theta,thresh,minLineLength=25,maxLineGap=3);
22+
23+
for line in lines: # [[ 14 263 464 263]]
24+
x1,y1,x2,y2 = line[0]
25+
cv.line(src,(x1,y1),(x2,y2),(0,0,255),2)
26+
27+
28+
cv.imshow("src",src)
29+
cv.imshow("gray",gray)
30+
cv.imshow("binary",binary)
31+
cv.waitKey()

opencv/day03/39_霍夫圆.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#encoding:utf-8
2+
import cv2 as cv
3+
4+
src = cv.imread("../img/weiqi.jpg")
5+
6+
# 将彩图转成灰度图
7+
gray = cv.cvtColor(src, cv.COLOR_BGR2GRAY)
8+
9+
# 注意 霍夫圆不需要转换陈成二值图像
10+
method = cv.HOUGH_GRADIENT
11+
# 累加器的分辨率,相当于循环的步长
12+
dp = 1
13+
# 检测到的圆心之间最小的距离
14+
minDist = 20
15+
# canny边缘检测算法的阈值上限
16+
param1 = 50;
17+
# 累计器阈值
18+
param2 = 30
19+
20+
circles = cv.HoughCircles(gray,method,dp,minDist,param1=param1,param2=param2,minRadius=3,maxRadius=20)
21+
22+
for circle in circles[0,:]:
23+
x,y,r = circle
24+
# 绘制圆心
25+
cv.circle(src,(x,y),2,(255,255,0),-1)
26+
# 绘制圆
27+
cv.circle(src,(x,y),r,(0,0,255),2)
28+
29+
cv.imshow("src",src)
30+
cv.waitKey()

0 commit comments

Comments
 (0)