0%

OpenCV:图像处理

颜色变换cvtColor

1
2
3
4
5
6
7
8
9
10
11
import cv2
img=cv2.imread('pic.png')
gray_img=cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
hsv_img=cv2.cvtColor(img,cv2.COLOR_BGR2HSV)
rgb=cv2.cvtColor(img,cv2.COLOR_BGR2RGB)
cv2.imshow("img",img)
cv2.imshow("gray",gray_img)
cv2.imshow("hsv",hsv_img)
cv2.imshow("RGB",rgb)
cv2.waitKey(0)
cv2.destroyWindow()

画基本图形

画矩形

1
def rectangle(img, pt1, pt2, color, thickness=None, lineType=None, shift=None):
1
2
3
4
5
6
import cv2
img=cv2.imread('pic.png')
img=cv2.rectangle(img,(100,100),(300,400),(255,14,65),6)
cv2.imshow("1",img)
cv2.waitKey(0)
cv2.destroyWindow()

画圆

1
def circle(img, center, radius, color, thickness=None, lineType=None, shift=None):
1
2
3
4
5
6
import cv2
img=cv2.imread('pic.png')
img=cv2.circle(img,(300,200),100,(155,64,240),-1)
cv2.imshow("1",img)
cv2.waitKey(0)
cv2.destroyWindow()

画椭圆

1
def ellipse(img, center, axes, angle, startAngle, endAngle, color, thickness=None, lineType=None, shift=None):
1
2
3
4
5
6
import cv2
img=cv2.imread('pic.png')
img=cv2.ellipse(img,(100,200),(50,20),0,0,360,(255,0,0),-1,8)
cv2.imshow("1",img)
cv2.waitKey(0)
cv2.destroyWindow()

画线段

1
def line(img, pt1, pt2, color, thickness=None, lineType=None, shift=None):
1
2
3
4
5
6
7
8
import cv2
import numpy as np
img=cv2.imread('pic.png')
Pts=np.array([[10,5],[20,30],[70,20],[50,10]],np.int32)
img=cv2.line(img,[0,0],[300,200],(0,0,255),2)
cv2.imshow("1",img)
cv2.waitKey(0)
cv2.destroyWindow()

画多边形

1
def polylines(img, pts, isClosed, color, thickness=None, lineType=None, shift=None):
1
2
3
4
5
6
7
8
import cv2
import numpy as np
img=cv2.imread('pic.png')
Pts=np.array([[10,5],[20,30],[70,20],[50,10]],np.int32)
img=cv2.polylines(img,[Pts],True,(255,0,0),6)
cv2.imshow("1",img)
cv2.waitKey(0)
cv2.destroyWindow()

填充多边形

1
def fillPoly(img, pts, color, lineType=None, shift=None, offset=None): 
1
2
3
4
5
6
7
8
import cv2
import numpy as np
img=cv2.imread('pic.png')
Pts=np.array([[10,5],[20,30],[70,20],[50,10]],np.int32)
img=cv2.fillPoly(img,[Pts],(255,0,0))
cv2.imshow("1",img)
cv2.waitKey(0)
cv2.destroyWindow()