OpenCV_Python 入门
创建/显示窗口
方法 说明 参数 返回 namedWindow 创建一个窗口 窗口名称,WINDOW_NORMAL(窗口属性) resizeWindow 设置窗口大小 窗口名称,宽,高 imshow 显示窗口 窗口名称,要显示的图像 destroyAllWindows 关闭所有窗口 waitKey 延时监听键盘按下 0无限 / 单位毫秒 键盘按下键的ASCII码
import cv2
cv2. namedWindow( "img" , cv2. WINDOW_NORMAL)
cv2. resizeWindow( "img" , 800 , 600 )
while True :
cv2. imshow( "img" , 0 )
key = cv2. waitKey( 0 )
if key & 0xFF == ord ( 'q' ) :
break
cv2. destroyAllWindows( )
显示图片
方法 说明 参数 返回 imread 读取图片 路径,IMREAD_GRAYSCALE(灰)/IMREAD_COLOR(彩)/其它 读取到的图片资源
import cv2
while True :
cv2. imshow( "img" , cv2. imread( "../1.jpg" , cv2. IMREAD_GRAYSCALE) )
key = cv2. waitKey( 0 )
if key & 0xFF == ord ( 'q' ) :
break
cv2. destroyAllWindows( )
保存图片
方法 说明 参数 返回 imwrite 保存图片 保存的文件名称,图片资源
import cv2
img = cv2. imread( "../1.jpg" , cv2. IMREAD_COLOR)
while True :
cv2. imshow( "img" , img)
key = cv2. waitKey( 0 )
if key & 0xFF == ord ( 'q' ) :
break
elif key & 0xFF == ord ( 's' ) :
cv2. imwrite( "11.png" , img)
cv2. destroyAllWindows( )
视频采集
方法 说明 参数 返回 VideoCapture 摄像机 0(开启本机摄像头) VideoCapture.read 获取视频帧 ret(读取到帧为true),frame(视频帧) VideoCapture.release 释放资源
import cv2
cv2. namedWindow( "frame" , cv2. WINDOW_NORMAL)
cv2. resizeWindow( "frame" , 800 , 600 )
cap = cv2. VideoCapture( 0 )
while True :
ret, frame = cap. read( )
cv2. imshow( 'frame' , frame)
key = cv2. waitKey( 1 )
if key & 0xFF == ord ( 'q' ) :
break
cap. release( )
cv2. destroyAllWindows( )
读取视频文件
方法 说明 参数 返回 VideoCapture 摄像机 本机视频文件路径
import cv2
cv2. namedWindow( "frame" , cv2. WINDOW_NORMAL)
cv2. resizeWindow( "frame" , 800 , 600 )
cap = cv2. VideoCapture( "../2.mp4" )
while True :
ret, frame = cap. read( )
cv2. imshow( 'frame' , frame)
key = cv2. waitKey( 1 )
if key & 0xFF == ord ( 'q' ) :
break
cap. release( )
cv2. destroyAllWindows( )
视频录制
方法 说明 参数 返回 VideoWriter_fourcc 编码器 *MJPG / 其它 VideoWriter 视频输出 文件名称,编码器,帧数,(宽,高) VideoWriter.write 视频写入 帧 VideoWriter.release 释放资源
import cv2
fourcc = cv2. VideoWriter_fourcc( * 'MJPG' )
vw = cv2. VideoWriter( 'output.avi' , fourcc, 20.0 , ( 640 , 480 ) )
cv2. namedWindow( "frame" , cv2. WINDOW_NORMAL)
cv2. resizeWindow( "frame" , 800 , 600 )
cap = cv2. VideoCapture( 0 )
while True :
ret, frame = cap. read( )
cv2. imshow( 'frame' , frame)
vw. write( frame)
key = cv2. waitKey( 1 )
if key & 0xFF == ord ( 'q' ) :
break
cap. release( )
vw. release( )
cv2. destroyAllWindows( )
import cv2
fourcc = cv2. VideoWriter_fourcc( * 'MJPG' )
vw = cv2. VideoWriter( 'output.avi' , fourcc, 20.0 , ( 640 , 480 ) )
cv2. namedWindow( "frame" , cv2. WINDOW_NORMAL)
cv2. resizeWindow( "frame" , 640 , 480 )
cap = cv2. VideoCapture( 0 )
while cap. isOpened( ) :
ret, frame = cap. read( )
if not ret: break
cv2. imshow( 'frame' , frame)
cv2. resizeWindow( "frame" , 640 , 480 )
vw. write( frame)
key = cv2. waitKey( 1 )
if key & 0xFF == ord ( 'q' ) :
break
cap. release( )
vw. release( )
cv2. destroyAllWindows( )
鼠标控制
方法 说明 参数 返回 setMouseCallback 监听鼠标 窗口名称,监听事件,额外参数
import cv2
def mouse_callback ( event, x, y, flags, param) :
print ( event, x, y, flags, param)
cv2. namedWindow( "img" , cv2. WINDOW_NORMAL)
cv2. resizeWindow( "img" , 800 , 600 )
cv2. setMouseCallback( "img" , mouse_callback, "123" )
img = cv2. imread( "../1.jpg" )
while True :
cv2. imshow( "img" , img)
key = cv2. waitKey( 0 )
if key & 0xFF == ord ( 'q' ) :
break
cv2. destroyAllWindows( )
TrackBar 滑块
方法 说明 参数 返回 createTrackbar 创建 滑块名称,窗口名称,value 当前值,count 最大值,callback 回调,额外参数 getTrackbarPos 获取 滑块名称,窗口名称 当前值
import cv2
import numpy as np
def callback ( ) :
pass
cv2. namedWindow( "img" , cv2. WINDOW_NORMAL)
cv2. createTrackbar( "R" , "img" , 0 , 255 , callback)
cv2. createTrackbar( "G" , "img" , 0 , 255 , callback)
cv2. createTrackbar( "B" , "img" , 0 , 255 , callback)
img = np. zeros( ( 480 , 640 , 3 ) , np. uint8)
while True :
r = cv2. getTrackbarPos( "R" , "img" )
g = cv2. getTrackbarPos( "G" , "img" )
b = cv2. getTrackbarPos( "B" , "img" )
img[ : ] = [ b, g, r]
cv2. imshow( "img" , img)
key = cv2. waitKey( 10 )
if key & 0xFF == ord ( 'q' ) :
break
cv2. destroyAllWindows( )
色彩
opencv 采用 BGR 格式的颜色显示 HSV 色相 / 饱和度 / 明度 HSL 色相 / 饱和度 / 明度 色彩空间转换
方法 说明 参数 返回 cvtColor 色彩转换 获取到的图片资源,色彩转换
import cv2
def callback ( ) :
pass
cv2. namedWindow( 'image' , cv2. WINDOW_NORMAL)
cv2. resizeWindow( 'image' , 800 , 600 )
img = cv2. imread( "../1.jpg" , 1 )
colorspaces = [ cv2. COLOR_BGR2RGB, cv2. COLOR_BGR2BGRA,
cv2. COLOR_BGR2GRAY, cv2. COLOR_BGR2HSV_FULL,
cv2. COLOR_BGR2YUV]
cv2. createTrackbar( 'curcolor' , 'image' , 0 , len ( colorspaces) - 1 , callback)
while True :
v = cv2. getTrackbarPos( 'curcolor' , 'image' )
cvt_img = cv2. cvtColor( img, colorspaces[ v] )
cv2. imshow( 'image' , cvt_img)
key = cv2. waitKey( 10 )
if key & 0xFF == ord ( 'q' ) :
break
cv2. destroyAllWindows( )
Numpy 基本操作
方法 参数 说明 array [] zeros (行,列,通道数),类型 值为0 ones (行,列,通道数),类型 值为1 full (行,列,通道数),值,类型 identity 3 三行三列 eye 3,5,k=1 三行五列, 从第 1-1列开始
import numpy as np
a = np. array( [ 1 , 2 , 3 ] )
b = np. array( [ [ 1 , 2 , 3 ] , [ 4 , 5 , 6 ] ] )
print ( a)
print ( b)
c = np. zeros( ( 8 , 8 , 3 ) , np. uint8)
print ( c)
d = np. ones( ( 8 , 8 , 3 ) , np. uint8)
print ( d)
e = np. full( ( 8 , 8 , 3 ) , 10 , np. uint8)
print ( e)
f = np. identity( 4 )
print ( f)
y = np. eye( 5 , 7 , k= 1 )
print ( y)
import numpy as np
import cv2
img = np. zeros( ( 640 , 480 , 3 ) , np. uint8)
print ( img[ 100 , 100 ] )
count = 0
while count < 100 :
img[ count, 100 ] = [ 0 , 0 , 255 ]
count = count+ 1
cv2. imshow( "img" , img)
key = cv2. waitKey( 0 )
if key & 0xFF == ord ( 'q' ) :
cv2. destroyAllWindows( )
ROI 获取子矩阵 [y1:y2,x1:x2] [:,:] 整个矩阵 [:]
import numpy as np
import cv2
img = np. zeros( ( 640 , 480 , 3 ) , np. uint8)
roi = img[ 100 : 200 , 100 : 200 ]
img[ : ] = [ 255 , 0 , 0 ]
roi[ : , : ] = [ 0 , 0 , 255 ]
roi[ 10 : 20 , 10 : 20 ] = [ 255 , 255 , 255 ]
cv2. imshow( "img" , img)
key = cv2. waitKey( 0 )
if key & 0xFF == ord ( 'q' ) :
cv2. destroyAllWindows( )
Mat
import numpy as np
import cv2
img = cv2. imread( '../1.jpg' )
img2 = img
img3 = img. copy( )
img[ 10 : 100 , 10 : 100 ] = [ 0 , 0 , 255 ]
cv2. imshow( 'img' , img)
cv2. imshow( 'img2' , img2)
cv2. imshow( 'img3' , img3)
cv2. waitKey( 0 )
import numpy as np
import cv2
img = cv2. imread( '../1.jpg' )
print ( img. shape)
print ( img. size)
print ( img. dtype)
import numpy as np
import cv2
img = np. zeros( ( 480 , 640 , 3 ) , np. uint8)
b, g, r = cv2. split( img)
b[ 10 : 100 , 10 : 100 ] = 255
g[ 10 : 100 , 10 : 100 ] = 255
img2 = cv2. merge( [ b, g, r] )
cv2. imshow( "img" , img)
cv2. imshow( "b" , b)
cv2. imshow( "g" , g)
cv2. imshow( "img2" , img2)
cv2. waitKey( 0 )
图形绘制
import numpy as np
import cv2
img = np. zeros( ( 480 , 640 , 3 ) , np. uint8)
cv2. line( img, ( 10 , 20 ) , ( 300 , 400 ) , ( 0 , 0 , 255 ) , 2 , 16 )
cv2. rectangle( img, ( 100 , 120 ) , ( 160 , 200 ) , ( 0 , 255 , 0 ) , 2 )
cv2. rectangle( img, ( 180 , 300 ) , ( 160 , 200 ) , ( 0 , 255 , 0 ) , - 1 )
cv2. circle( img, ( 300 , 300 ) , 30 , ( 255 , 0 , 0 ) , 5 )
cv2. circle( img, ( 360 , 360 ) , 30 , ( 255 , 0 , 0 ) , - 1 )
cv2. ellipse( img, ( 300 , 420 ) , ( 100 , 30 ) , 0 , 0 , 360 , ( 255 , 0 , 0 ) , 5 )
cv2. ellipse( img, ( 300 , 420 ) , ( 100 , 30 ) , 0 , 0 , 360 , ( 255 , 0 , 0 ) , - 1 )
pts = np. array( [ ( 300 , 10 ) , ( 150 , 100 ) , ( 450 , 100 ) ] , np. int32)
cv2. polylines( img, [ pts] , True , ( 0 , 255 , 0 ) )
cv2. fillPoly( img, [ pts] , ( 0 , 255 , 0 ) )
cv2. putText( img, "Hello World!!!" , ( 100 , 100 ) , cv2. FONT_HERSHEY_PLAIN, 3 , ( 255 , 255 , 255 ) )
cv2. imshow( "img" , img)
cv2. waitKey( 0 )
import numpy as np
import cv2
current = 0
startpos = ( 0 , 0 )
def mouse_callback ( event, x, y, flags, param) :
global current, startpos
if event & cv2. EVENT_LBUTTONDOWN == cv2. EVENT_LBUTTONDOWN:
startpos = ( x, y)
elif event & cv2. EVENT_LBUTTONUP == cv2. EVENT_LBUTTONUP:
if current == 0 :
cv2. line( img, startpos, ( x, y) , ( 0 , 0 , 255 ) )
elif current == 1 :
cv2. rectangle( img, startpos, ( x, y) , ( 0 , 0 , 255 ) )
elif current == 2 :
a = ( x- startpos[ 0 ] )
b = ( y- startpos[ 1 ] )
r = ( a** 2 + b** 2 ) ** 0.5
cv2. circle( img, startpos, int ( r) , ( 0 , 0 , 255 ) )
else :
print ( "无其它" )
cv2. namedWindow( "img" , cv2. WINDOW_NORMAL)
cv2. setMouseCallback( "img" , mouse_callback, "123" )
img = np. zeros( ( 480 , 640 , 3 ) , np. uint8)
while True :
cv2. imshow( "img" , img)
key = cv2. waitKey( 1 )
if key & 0xFF == ord ( 'q' ) :
break
elif key == ord ( 'l' ) :
current = 0
elif key == ord ( 'r' ) :
current = 1
elif key == ord ( 'c' ) :
current = 2
cv2. destroyAllWindows( )
图像运算
import numpy as np
import cv2
img = cv2. imread( '../1.jpg' )
img2 = np. ones( img. shape, np. uint8) * 50
img11 = cv2. add( img, img2)
img22 = cv2. subtract( img, img2)
img33 = cv2. multiply( img, img2)
img44 = cv2. divide( img, img2)
img55 = cv2. addWeighted( img, 0.1 , img2, 0.9 , 0 )
cv2. imshow( 'img' , img)
cv2. imshow( 'img11' , img11)
cv2. imshow( 'img22' , img22)
cv2. imshow( 'img33' , img33)
cv2. imshow( 'img44' , img44)
cv2. imshow( 'img55' , img55)
cv2. waitKey( 0 )
import numpy as np
import cv2
img1 = np. zeros( ( 200 , 200 ) , np. uint8)
img1[ 50 : 150 , 50 : 150 ] = 255
img11 = cv2. bitwise_not( img1)
img2 = np. zeros( ( 200 , 200 ) , np. uint8)
img22 = np. zeros( ( 200 , 200 ) , np. uint8)
img2[ 20 : 120 , 20 : 120 ] = 255
img22[ 80 : 150 , 80 : 150 ] = 255
img222 = cv2. bitwise_xor( img2, img22)
cv2. imshow( 'img1' , img1)
cv2. imshow( 'img11' , img11)
cv2. imshow( 'img222' , img222)
cv2. waitKey( 0 )
import numpy as np
import cv2
img = cv2. imread( '../1.jpg' )
roi = img[ 0 : 200 , 0 : 200 ]
logo = np. zeros( ( 200 , 200 , 3 ) , np. uint8)
logo[ 20 : 120 , 20 : 120 ] = [ 0 , 0 , 255 ]
logo[ 80 : 180 , 80 : 180 ] = [ 0 , 255 , 0 ]
mask = np. zeros( ( 200 , 200 ) , np. uint8)
mask[ 20 : 120 , 20 : 120 ] = 255
mask[ 80 : 180 , 80 : 180 ] = 255
m = cv2. bitwise_not( mask)
tmp = cv2. bitwise_and( roi, roi, mask= m)
dst = cv2. add( tmp, logo)
img[ 0 : 200 , 0 : 200 ] = dst
cv2. imshow( 'logo' , dst)
cv2. waitKey( 0 )
图像变换
import numpy as np
import cv2
img = cv2. imread( '../1.jpg' )
img1 = cv2. resize( img, ( 224 , 224 ) )
img2 = cv2. resize( img, None , fx= 0.6 , fy= 0.6 , interpolation= cv2. INTER_AREA)
img3 = cv2. resize( img, None , fx= 1.6 , fy= 1.6 , interpolation= cv2. INTER_AREA)
cv2. imshow( 'image' , img)
cv2. imshow( 'image1' , img1)
cv2. imshow( 'image2' , img2)
cv2. imshow( 'image3' , img3)
cv2. waitKey( 0 )
import numpy as np
import cv2
img = cv2. imread( '../1.jpg' )
img1 = cv2. flip( img, 0 )
img2 = cv2. flip( img, 1 )
img3 = cv2. flip( img, - 1 )
cv2. imshow( 'image' , img)
cv2. imshow( 'image1' , img1)
cv2. imshow( 'image2' , img2)
cv2. imshow( 'image3' , img3)
cv2. waitKey( 0 )
import numpy as np
import cv2
img = cv2. imread( '../1.jpg' )
img1 = cv2. rotate( img, cv2. ROTATE_90_CLOCKWISE)
cv2. imshow( 'image' , img1)
cv2. waitKey( 0 )
import numpy as np
import cv2
img = cv2. imread( '../1.jpg' )
h, w, ch = img. shape
print ( h, w)
src = np. float32( [ [ 200 , 150 ] , [ 400 , 150 ] , [ 400 , 500 ] ] )
dst = np. float32( [ [ 100 , 200 ] , [ 300 , 250 ] , [ 150 , 600 ] ] )
M = cv2. getAffineTransform( src, dst)
img2 = cv2. warpAffine( img, M, ( w, h) )
cv2. imshow( 'img' , img)
cv2. imshow( 'img2' , img2)
cv2. waitKey( 0 )
import numpy as np
import cv2
img = cv2. imread( '../1.jpg' )
src = np. float32( [ [ 10 , 300 ] , [ 100 , 300 ] , [ 20 , 500 ] , [ 150 , 450 ] ] )
dst = np. float32( [ [ 0 , 0 ] , [ 120 , 0 ] , [ 0 , 300 ] , [ 120 , 300 ] ] )
M = cv2. getPerspectiveTransform( src, dst)
img1 = cv2. warpPerspective( img, M, ( 300 , 120 ) )
cv2. imshow( 'img' , img)
cv2. imshow( 'img1' , img1)
cv2. waitKey( 0 )
图像滤波
import numpy as np
import cv2
img = cv2. imread( '../1.jpg' )
kernel = np. ones( ( 5 , 5 ) , np. uint32) / 25
dst = cv2. filter2D( img, - 1 , kernel)
cv2. imshow( 'img' , img)
cv2. imshow( 'dst' , dst)
cv2. waitKey( 0 )
import numpy as np
import cv2
img = cv2. imread( '../1.jpg' )
dst = cv2. blur( img, ( 5 , 5 ) )
cv2. imshow( 'img' , img)
cv2. imshow( 'dst' , dst)
cv2. waitKey( 0 )
import numpy as np
import cv2
img = cv2. imread( '../1.jpg' )
dst = cv2. GaussianBlur( img, ( 5 , 5 ) , sigmaX= 1 )
cv2. imshow( 'img' , img)
cv2. imshow( 'dst' , dst)
cv2. waitKey( 0 )
import numpy as np
import cv2
img = cv2. imread( '../1.jpg' )
dst = cv2. medianBlur( img, 5 )
cv2. imshow( 'img' , img)
cv2. imshow( 'dst' , dst)
cv2. waitKey( 0 )
import numpy as np
import cv2
img = cv2. imread( '../1.jpg' )
dst = cv2. bilateralFilter( img, 5 , 20 , 50 )
cv2. imshow( 'img' , img)
cv2. imshow( 'dst' , dst)
cv2. waitKey( 0 )
import numpy as np
import cv2
img = cv2. imread( './12.png' )
dst = cv2. Sobel( img, cv2. CV_64F, 1 , 0 , ksize= 5 )
dst1 = cv2. Sobel( img, cv2. CV_64F, 0 , 1 , ksize= 5 )
dst2 = cv2. add( dst, dst1)
cv2. imshow( 'img' , img)
cv2. imshow( 'dst' , dst2)
cv2. waitKey( 0 )
import numpy as np
import cv2
img = cv2. imread( './12.png' )
dst = cv2. Scharr( img, cv2. CV_64F, 1 , 0 )
dst1 = cv2. Scharr( img, cv2. CV_64F, 0 , 1 )
dst2 = cv2. add( dst, dst1)
cv2. imshow( 'img' , img)
cv2. imshow( 'dst' , dst2)
cv2. waitKey( 0 )
import numpy as np
import cv2
img = cv2. imread( './12.png' )
dst = cv2. Laplacian( img, cv2. CV_64F, ksize= 5 )
cv2. imshow( 'img' , img)
cv2. imshow( 'dst' , dst)
cv2. waitKey( 0 )
边缘检测
import numpy as np
import cv2
img = cv2. imread( '../1.jpg' )
dst = cv2. Canny( img, 100 , 200 )
cv2. imshow( 'img' , img)
cv2. imshow( 'dst' , dst)
cv2. waitKey( 0 )
形态学图像处理
import numpy as np
import cv2
img = cv2. imread( '../1.jpg' )
img1 = cv2. cvtColor( img, cv2. COLOR_BGR2GRAY)
ret, img2 = cv2. threshold( img1, 180 , 255 , cv2. THRESH_BINARY)
cv2. imshow( 'img' , img)
cv2. imshow( 'img1' , img1)
cv2. imshow( 'img2' , img2)
cv2. waitKey( 0 )
import numpy as np
import cv2
img = cv2. imread( '../1.jpg' )
img1 = cv2. cvtColor( img, cv2. COLOR_BGR2GRAY)
dst = cv2. adaptiveThreshold( img1, 255 , cv2. ADAPTIVE_THRESH_GAUSSIAN_C, cv2. THRESH_BINARY, 3 , 0 )
cv2. imshow( 'img' , img)
cv2. imshow( 'img1' , img1)
cv2. imshow( 'img2' , dst)
cv2. waitKey( 0 )
import numpy as np
import cv2
img = cv2. imread( './img.png' )
kernel = cv2. getStructuringElement( cv2. MORPH_RECT, ( 3 , 3 ) )
img1 = cv2. erode( img, kernel, iterations = 1 )
img2 = cv2. dilate( img1, kernel, iterations = 1 )
cv2. imshow( 'img' , img)
cv2. imshow( 'img1' , img1)
cv2. imshow( 'img2' , img2)
cv2. waitKey( 0 )
import numpy as np
import cv2
img = cv2. imread( './img_1.png' )
img3 = cv2. imread( './img_3.png' )
img6 = cv2. imread( './img_2.png' )
imgd = cv2. imread( './img_4.png' )
kernel = cv2. getStructuringElement( cv2. MORPH_RECT, ( 7 , 7 ) )
img2 = cv2. morphologyEx( img, cv2. MORPH_OPEN, kernel)
img33 = cv2. morphologyEx( img3, cv2. MORPH_CLOSE, kernel)
img66 = cv2. morphologyEx( img6, cv2. MORPH_GRADIENT, kernel)
imgdd = cv2. morphologyEx( imgd, cv2. MORPH_TOPHAT, cv2. getStructuringElement( cv2. MORPH_RECT, ( 19 , 19 ) ) )
imgh = cv2. morphologyEx( img3, cv2. MORPH_BLACKHAT, kernel)
cv2. imshow( 'img2' , img2)
cv2. imshow( 'img33' , img33)
cv2. imshow( 'img66' , img66)
cv2. imshow( 'imgdd' , imgdd)
cv2. imshow( 'imgh' , imgh)
cv2. waitKey( 0 )
图像轮廓
import numpy as np
import cv2
img = cv2. imread( './img_5.png' )
gray = cv2. cvtColor( img, cv2. COLOR_BGR2GRAY)
ret, img1 = cv2. threshold( gray, 150 , 255 , cv2. THRESH_BINARY)
contours, hierarchy = cv2. findContours( img1, cv2. RETR_TREE, cv2. CHAIN_APPROX_SIMPLE)
cv2. drawContours( img, contours, - 1 , ( 0 , 0 , 255 ) , 1 )
area = cv2. contourArea( contours[ 0 ] )
print ( area)
len = cv2. arcLength( contours[ 0 ] , True )
print ( len )
cv2. imshow( 'img' , img)
cv2. waitKey( 0 )
import numpy as np
import cv2
img = cv2. imread( './img_6.png' )
gray = cv2. cvtColor( img, cv2. COLOR_BGR2GRAY)
ret, img1 = cv2. threshold( gray, 150 , 255 , cv2. THRESH_BINARY)
contours, hierarchy = cv2. findContours( img1, cv2. RETR_TREE, cv2. CHAIN_APPROX_SIMPLE)
cv2. drawContours( img, contours, 0 , ( 255 , 0 , 0 ) , 1 )
approx = cv2. approxPolyDP( contours[ 0 ] , 20 , True )
hull = cv2. convexHull( contours[ 0 ] )
def drawShape ( img, approx) :
i = 0
while i < len ( approx) :
if i == len ( approx) - 1 :
x, y = approx[ i] [ 0 ]
x1, y1 = approx[ 0 ] [ 0 ]
cv2. line( img, ( x, y) , ( x1, y1) , ( 0 , 0 , 255 ) , 1 )
else :
x, y = approx[ i] [ 0 ]
x1, y1 = approx[ i + 1 ] [ 0 ]
cv2. line( img, ( x, y) , ( x1, y1) , ( 0 , 0 , 255 ) , 1 )
i = i+ 1
drawShape( img, approx)
drawShape( img, hull)
cv2. imshow( 'img' , img)
cv2. waitKey( 0 )
import numpy as np
import cv2
img = cv2. imread( './img_7.png' )
gray = cv2. cvtColor( img, cv2. COLOR_BGR2GRAY)
ret, img1 = cv2. threshold( gray, 150 , 255 , cv2. THRESH_BINARY)
contours, hierarchy = cv2. findContours( img1, cv2. RETR_TREE, cv2. CHAIN_APPROX_SIMPLE)
r = cv2. minAreaRect( contours[ 1 ] )
box = cv2. boxPoints( r)
box = np. int8( box)
cv2. drawContours( img, [ box] , 0 , ( 0 , 0 , 255 ) , 2 )
x, y, w, h = cv2. boundingRect( contours[ 1 ] )
cv2. rectangle( img, ( x, y) , ( x+ w, y+ h) , ( 0 , 255 , 0 ) , 2 )
cv2. imshow( "img" , img)
cv2. waitKey( 0 )
特征点检测
哈里斯角点检测 Shi-Tomasi 角点检测 SIFT SURF ORB
import numpy as np
import cv2
img = cv2. imread( './12.png' )
gray = cv2. cvtColor( img, cv2. COLOR_BGR2GRAY)
orb = cv2. ORB. create( )
kp, des = orb. detectAndCompute( gray, None )
cv2. drawKeypoints( gray, kp, img)
cv2. imshow( "img" , img)
cv2. waitKey( 0 )
特征匹配
import numpy as np
import cv2
img1 = cv2. imread( './img.png' )
img2 = cv2. imread( './img_1.png' )
gray1 = cv2. cvtColor( img1, cv2. COLOR_BGR2GRAY)
gray2 = cv2. cvtColor( img2, cv2. COLOR_BGR2GRAY)
sift = cv2. SIFT. create( )
kp1, des1 = sift. detectAndCompute( gray1, None )
kp2, des2 = sift. detectAndCompute( gray2, None )
bf = cv2. BFMatcher( cv2. NORM_L1)
matches = bf. match ( des1, des2)
img3 = cv2. drawMatches( img1, kp1, img2, kp2, matches, None , flags= 2 )
cv2. imshow( "img" , img3)
cv2. waitKey( 0 )
import numpy as np
import cv2
img1 = cv2. imread( './img.png' )
img2 = cv2. imread( './img_1.png' )
gray1 = cv2. cvtColor( img1, cv2. COLOR_BGR2GRAY)
gray2 = cv2. cvtColor( img2, cv2. COLOR_BGR2GRAY)
sift = cv2. SIFT. create( )
kp1, des1 = sift. detectAndCompute( gray1, None )
kp2, des2 = sift. detectAndCompute( gray2, None )
flann = cv2. FlannBasedMatcher( dict ( trees= 5 , algorithm= 1 ) , dict ( checks= 50 ) )
matches = flann. knnMatch( des1, des2, k= 2 )
good = [ ]
for i, ( m, n) in enumerate ( matches) :
if m. distance < 0.7 * n. distance:
good. append( m)
ret = cv2. drawMatchesKnn( img1, kp1, img2, kp2, [ good] , None )
cv2. imshow( "img" , ret)
cv2. waitKey( 0 )
图像分割
import numpy as np
import cv2
img = cv2. imread( "./img.png" )
gray = cv2. cvtColor( img, cv2. COLOR_BGR2GRAY)
ret, thresh = cv2. threshold( gray, 0 , 255 , cv2. THRESH_BINARY_INV+ cv2. THRESH_OTSU)
kernel = np. ones( ( 3 , 3 ) , np. uint8)
open1 = cv2. morphologyEx( thresh, cv2. MORPH_OPEN, kernel, iterations= 2 )
bg = cv2. dilate( open1, kernel, iterations= 2 )
dist = cv2. distanceTransform( open1, cv2. DIST_L2, 5 )
re1t, fg = cv2. threshold( dist, 0.7 * dist. max ( ) , 255 , cv2. THRESH_BINARY)
fg = np. uint8( fg)
unknow = cv2. subtract( bg, fg)
ret3, marker = cv2. connectedComponents( fg)
marker = marker + 1
marker[ unknow == 255 ] = 0
result = cv2. watershed( img, marker)
img[ result == - 1 ] = [ 0 , 0 , 255 ]
cv2. imshow( "img" , img)
cv2. waitKey( 0 )
import numpy as np
import cv2
class App :
rect = ( 0 , 0 , 0 , 0 )
flag_rect = False
startX = 0
startY = 0
def onmouse ( self, event, x, y, flags, param) :
print ( "onmouse" )
if event == cv2. EVENT_LBUTTONDOWN:
self. flag_rect = True
self. startX = x
self. startY = y
elif event == cv2. EVENT_LBUTTONUP:
self. flag_rect = False
cv2. rectangle( self. img, ( self. startX, self. startY) , ( x, y) , ( 0 , 0 , 255 ) , 2 )
self. rect = ( min ( self. startX, x) , min ( self. startY, y) , abs ( self. startX - x) , abs ( self. startY - y) )
print ( "左键抬起" )
elif event == cv2. EVENT_MOUSEMOVE:
if self. flag_rect:
self. img = self. img2. copy( )
cv2. rectangle( self. img, ( self. startX, self. startY) , ( x, y) , ( 255 , 0 , 0 ) , 2 )
print ( "移动" )
def run ( self) :
print ( "run" )
cv2. namedWindow( 'input' )
cv2. setMouseCallback( 'input' , self. onmouse)
self. img = cv2. imread( './img.png' )
self. img2 = self. img. copy( )
self. mask = np. zeros( self. img. shape[ : 2 ] , np. uint8)
self. output = np. zeros( self. img. shape, np. uint8)
while 1 :
cv2. imshow( 'input' , self. img)
cv2. imshow( 'output' , self. output)
k = cv2. waitKey( 100 )
if k == 27 :
break
if k == ord ( 'g' ) :
bgmodel = np. zeros( ( 1 , 65 ) , np. float64)
fgmodel = np. zeros( ( 1 , 65 ) , np. float64)
cv2. grabCut( self. img2, self. mask, self. rect, bgmodel, fgmodel, 1 , cv2. GC_INIT_WITH_RECT)
mask2 = np. where( ( self. mask == 1 ) | ( self. mask == 3 ) , 255 , 0 ) . astype( 'uint8' )
self. output = cv2. bitwise_and( self. img2, self. img2, mask= mask2)
App( ) . run( )
import numpy as np
import cv2
img = cv2. imread( "./img.png" )
img2 = cv2. pyrMeanShiftFiltering( img, 20 , 30 )
img3 = cv2. Canny( img2, 150 , 300 )
contours, _ = cv2. findContours( img3, cv2. RETR_EXTERNAL, cv2. CHAIN_APPROX_SIMPLE)
cv2. drawContours( img, contours, - 1 , ( 0 , 0 , 255 ) , 2 )
cv2. imshow( "img" , img)
cv2. imshow( "img2" , img2)
cv2. imshow( "img3" , img3)
cv2. waitKey( 0 )
import numpy as np
import cv2
cap = cv2. VideoCapture( '../2.mp4' )
mog = cv2. createBackgroundSubtractorMOG2( )
while cap. isOpened( ) :
ret, frame = cap. read( )
fgmask = mog. apply ( frame)
cv2. imshow( 'frame' , fgmask)
if cv2. waitKey( 1 ) & 0xFF == ord ( 'q' ) :
break
cap. release( )
cv2. destroyAllWindows( )
图像修复
import numpy as np
import cv2
img = cv2. imread( "./img_1.png" )
mask = cv2. imread( "./img_2.png" , 0 )
mask = cv2. resize( mask, ( img. shape[ 1 ] , img. shape[ 0 ] ) )
print ( img. shape)
print ( mask. shape)
dst = cv2. inpaint( img, mask, 3 , cv2. INPAINT_TELEA)
cv2. imshow( "img" , dst)
cv2. waitKey( 0 )
哈尔级联法
人脸识别
import numpy as np
import cv2
facer = cv2. CascadeClassifier(
'D:\SoftWare\py\Lib\site-packages\cv2\data\haarcascade_frontalface_default.xml'
)
img = cv2. imread( './img_3.png' )
gray = cv2. cvtColor( img, cv2. COLOR_BGR2GRAY)
faces = facer. detectMultiScale( gray, 1.1 , 5 )
for ( x, y, w, h) in faces:
cv2. rectangle( img, ( x, y) , ( x + w, y + h) , ( 255 , 0 , 0 ) , 2 )
cv2. imshow( 'img' , img)
cv2. waitKey( 0 )
import numpy as np
import cv2
facer = cv2. CascadeClassifier(
'D:\SoftWare\py\Lib\site-packages\cv2\data\haarcascade_frontalface_default.xml'
)
img = cv2. VideoCapture( 0 )
while True :
ret, frame = img. read( )
gray = cv2. cvtColor( frame, cv2. COLOR_BGR2GRAY)
faces = facer. detectMultiScale( gray, 1.1 , 5 )
for ( x, y, w, h) in faces:
cv2. rectangle( frame, ( x, y) , ( x + w, y + h) , ( 255 , 0 , 0 ) , 2 )
cv2. imshow( 'img' , frame)
k = cv2. waitKey( 10 )
if k == 27 :
break
img. release( )
cv2. destroyAllWindows( )
import numpy as np
import cv2
facer = cv2. CascadeClassifier(
'D:\SoftWare\py\Lib\site-packages\cv2\data\haarcascade_eye.xml'
)
img = cv2. imread( './img_3.png' )
gray = cv2. cvtColor( img, cv2. COLOR_BGR2GRAY)
faces = facer. detectMultiScale( gray, 1.1 , 5 )
for ( x, y, w, h) in faces:
cv2. rectangle( img, ( x, y) , ( x + w, y + h) , ( 255 , 0 , 0 ) , 2 )
cv2. imshow( 'img' , img)
cv2. waitKey( 0 )