opencv-python核心操作

陌路散爱 提交于 2020-02-29 11:21:46

图像的基础操作

加载图像

>>> import numpy as np
>>> import cv2 as cv
>>> img = cv.imread('messi5.jpg')

it returns an array of Blue, Green, Red values
获取某点BGR值

>>> px = img[100,100]
>>> print( px )
[157 166 200]
#accessing only blue pixel
>>> blue = img[100,100,0]
>>> print( blue )
157

修改像素点的值

>>> img[100,100] = [255,255,255]
>>> print( img[100,100] )
[255 255 255]

更好修改像素的方法

# accessing RED value
>>> img.item(10,10,2)
59
# modifying RED value
>>> img.itemset((10,10,2),100)
>>> img.item(10,10,2)
100

获取图像规格

>>> print( img.shape )
(342, 548, 3)

获取像素点数目

>>> print( img.size )
562248

获取图像类型

>>> print( img.dtype )
uint8

像素复制

>>> ball = img[280:340, 330:390]
>>> img[273:333, 100:160] = ball

像素通道分割

>>> b,g,r = cv.split(img)
>>> img = cv.merge((b,g,r))

或者

>>> b = img[:,:,0]

或者让某一通道变成某一值

>>> img[:,:,2] = 0

图像算术运算

图像相加

>>> x = np.uint8([250])
>>> y = np.uint8([10])
>>> print( cv.add(x,y) ) # 250+10 = 260 => 255
[[255]]
>>> print( x+y )          # 250+10 = 260 % 256 = 4
[4]

图像混合

img1 = cv.imread('ml.png')
img2 = cv.imread('opencv-logo.png')
dst = cv.addWeighted(img1,0.7,img2,0.3,0)
cv.imshow('dst',dst)
cv.waitKey(0)
cv.destroyAllWindows()

位操作

# Load two images
img1 = cv.imread('messi5.jpg')
img2 = cv.imread('opencv-logo-white.png')
# I want to put logo on top-left corner, So I create a ROI
rows,cols,channels = img2.shape
roi = img1[0:rows, 0:cols]
# Now create a mask of logo and create its inverse mask also
img2gray = cv.cvtColor(img2,cv.COLOR_BGR2GRAY)
ret, mask = cv.threshold(img2gray, 10, 255, cv.THRESH_BINARY)
mask_inv = cv.bitwise_not(mask)
# Now black-out the area of logo in ROI
img1_bg = cv.bitwise_and(roi,roi,mask = mask_inv)
# Take only region of logo from logo image.
img2_fg = cv.bitwise_and(img2,img2,mask = mask)
# Put logo in ROI and modify the main image
dst = cv.add(img1_bg,img2_fg)
img1[0:rows, 0:cols ] = dst
cv.imshow('res',img1)
cv.waitKey(0)
cv.destroyAllWindows()

源地址

https://docs.opencv.org/3.4/d0/d86/tutorial_py_image_arithmetics.html

表现评估和改善方法

用opencv评估结果,查看程序运行时间,获取时间

e1 = cv.getTickCount()
# your code execution
e2 = cv.getTickCount()
time = (e2 - e1)/ cv.getTickFrequency()

优化

# check if optimization is enabled
In [5]: cv.useOptimized()
Out[5]: True
In [6]: %timeit res = cv.medianBlur(img,49)
10 loops, best of 3: 34.9 ms per loop
# Disable it
In [7]: cv.setUseOptimized(False)
In [8]: cv.useOptimized()
Out[8]: False
In [9]: %timeit res = cv.medianBlur(img,49)
10 loops, best of 3: 64.1 ms per loop

效果评估ipython

In [10]: x = 5
In [11]: %timeit y=x**2
10000000 loops, best of 3: 73 ns per loop
In [12]: %timeit y=x*x
10000000 loops, best of 3: 58.3 ns per loop
In [15]: z = np.uint8([5])
In [17]: %timeit y=z*z
1000000 loops, best of 3: 1.25 us per loop
In [19]: %timeit y=np.square(z)
1000000 loops, best of 3: 1.16 us per loop

In [35]: %timeit z = cv.countNonZero(img)
100000 loops, best of 3: 15.8 us per loop
In [36]: %timeit z = np.count_nonzero(img)
1000 loops, best of 3: 370 us per loop

源地址

https://docs.opencv.org/3.4/dc/d71/tutorial_py_optimization.html

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!