opencv-python图像处理2

图像梯度

cv.Sobel(), cv.Scharr(), cv.Laplacian()

import numpy as np
import cv2 as cv
from matplotlib import pyplot as plt
img = cv.imread('dave.jpg',0)
laplacian = cv.Laplacian(img,cv.CV_64F)
sobelx = cv.Sobel(img,cv.CV_64F,1,0,ksize=5)
sobely = cv.Sobel(img,cv.CV_64F,0,1,ksize=5)
plt.subplot(2,2,1),plt.imshow(img,cmap = 'gray')
plt.title('Original'), plt.xticks([]), plt.yticks([])
plt.subplot(2,2,2),plt.imshow(laplacian,cmap = 'gray')
plt.title('Laplacian'), plt.xticks([]), plt.yticks([])
plt.subplot(2,2,3),plt.imshow(sobelx,cmap = 'gray')
plt.title('Sobel X'), plt.xticks([]), plt.yticks([])
plt.subplot(2,2,4),plt.imshow(sobely,cmap = 'gray')
plt.title('Sobel Y'), plt.xticks([]), plt.yticks([])
plt.show()

在这里插入图片描述
源地址

https://docs.opencv.org/3.4/d5/d0f/tutorial_py_gradients.html

canny edge detection

cv.Canny()

import numpy as np
import cv2 as cv
from matplotlib import pyplot as plt
img = cv.imread('messi5.jpg',0)
edges = cv.Canny(img,100,200)
plt.subplot(121),plt.imshow(img,cmap = 'gray')
plt.title('Original Image'), plt.xticks([]), plt.yticks([])
plt.subplot(122),plt.imshow(edges,cmap = 'gray')
plt.title('Edge Image'), plt.xticks([]), plt.yticks([])
plt.show()

在这里插入图片描述
源地址

https://docs.opencv.org/3.4/da/d22/tutorial_py_canny.html

图像金字塔

img = cv.imread('messi5.jpg')
lower_reso = cv.pyrDown(higher_reso)

在这里插入图片描述

higher_reso2 = cv.pyrUp(lower_reso)

在这里插入图片描述
在这里插入图片描述

使用金字塔混合图像

在这里插入图片描述

import cv2 as cv
import numpy as np,sys
A = cv.imread('apple.jpg')
B = cv.imread('orange.jpg')
# generate Gaussian pyramid for A
G = A.copy()
gpA = [G]
for i in xrange(6):
    G = cv.pyrDown(G)
    gpA.append(G)
# generate Gaussian pyramid for B
G = B.copy()
gpB = [G]
for i in xrange(6):
    G = cv.pyrDown(G)
    gpB.append(G)
# generate Laplacian Pyramid for A
lpA = [gpA[5]]
for i in xrange(5,0,-1):
    GE = cv.pyrUp(gpA[i])
    L = cv.subtract(gpA[i-1],GE)
    lpA.append(L)
# generate Laplacian Pyramid for B
lpB = [gpB[5]]
for i in xrange(5,0,-1):
    GE = cv.pyrUp(gpB[i])
    L = cv.subtract(gpB[i-1],GE)
    lpB.append(L)
# Now add left and right halves of images in each level
LS = []
for la,lb in zip(lpA,lpB):
    rows,cols,dpt = la.shape
    ls = np.hstack((la[:,0:cols/2], lb[:,cols/2:]))
    LS.append(ls)
# now reconstruct
ls_ = LS[0]
for i in xrange(1,6):
    ls_ = cv.pyrUp(ls_)
    ls_ = cv.add(ls_, LS[i])
# image with direct connecting each half
real = np.hstack((A[:,:cols/2],B[:,cols/2:]))
cv.imwrite('Pyramid_blending2.jpg',ls_)
cv.imwrite('Direct_blending.jpg',real)

源地址

https://docs.opencv.org/3.4/dc/dff/tutorial_py_pyramids.html

轮廓

** cv.findContours(), cv.drawContours()**

import numpy as np
import cv2 as cv
im = cv.imread('test.jpg')
imgray = cv.cvtColor(im, cv.COLOR_BGR2GRAY)
ret, thresh = cv.threshold(imgray, 127, 255, 0)
im2, contours, hierarchy = cv.findContours(thresh, cv.RETR_TREE, cv.CHAIN_APPROX_SIMPLE)

怎么画轮廓

#To draw all the contours in an image:
cv.drawContours(img, contours, -1, (0,255,0), 3)
#To draw an individual contour, say 4th contour:
cv.drawContours(img, contours, 3, (0,255,0), 3)
#But most of the time, below method will be useful:
cnt = contours[4]
cv.drawContours(img, [cnt], 0, (0,255,0), 3)

源地址

https://docs.opencv.org/3.4/d4/d73/tutorial_py_contours_begin.html

轮廓特征

**cv.moments() **

import numpy as np
import cv2 as cv
img = cv.imread('star.jpg',0)
ret,thresh = cv.threshold(img,127,255,0)
im2,contours,hierarchy = cv.findContours(thresh, 1, 2)
cnt = contours[0]
M = cv.moments(cnt)
print( M )
cx = int(M['m10']/M['m00'])
cy = int(M['m01']/M['m00'])

** cv.contourArea() **

area = cv.contourArea(cnt)
perimeter = cv.arcLength(cnt,True)
epsilon = 0.1*cv.arcLength(cnt,True)
approx = cv.approxPolyDP(cnt,epsilon,True)

在这里插入图片描述

hull = cv.convexHull(points[, hull[, clockwise[, returnPoints]]
hull = cv.convexHull(cnt)

6. 检测凸边界

k = cv.isContourConvex(cnt)

方形边界

直接边界

x,y,w,h = cv.boundingRect(cnt)
cv.rectangle(img,(x,y),(x+w,y+h),(0,255,0),2)

自适应边界

rect = cv.minAreaRect(cnt)
box = cv.boxPoints(rect)
box = np.int0(box)
cv.drawContours(img,[box],0,(0,0,255),2)

在这里插入图片描述
最小化边界圆圈

(x,y),radius = cv.minEnclosingCircle(cnt)
center = (int(x),int(y))
radius = int(radius)
cv.circle(img,center,radius,(0,255,0),2)

在这里插入图片描述
自适应椭圆

ellipse = cv.fitEllipse(cnt)
cv.ellipse(img,ellipse,(0,255,0),2)

在这里插入图片描述
适应线条

rows,cols = img.shape[:2]
[vx,vy,x,y] = cv.fitLine(cnt, cv.DIST_L2,0,0.01,0.01)
lefty = int((-x*vy/vx) + y)
righty = int(((cols-x)*vy/vx)+y)
cv.line(img,(cols-1,righty),(0,lefty),(0,255,0),2)

在这里插入图片描述
源地址

https://docs.opencv.org/3.4/dd/d49/tutorial_py_contour_features.html

发布了25 篇原创文章 · 获赞 0 · 访问量 290

猜你喜欢

转载自blog.csdn.net/weixin_39025679/article/details/104572480