opencv-python
ไธ่ฌๆง่ฏดๆ
ๅฎ่ฃ
่ฏฆ็ปๅฏไปฅๅ่ opencv-python ไปๅบ็ README ๆๆกฃ
pip install opencv-python # ๆ ้ๅฆๅค็ผ่ฏๅฎ่ฃ
opencv
ๆณจๆ๏ผๅญๅจๅ็งไฝฟ็จ pip ็ๆนๅผ่ฟ่กๅฎ่ฃ
๏ผไฝๅช่ฝ้ๆฉๅ
ถไธญ็ไธ็ง๏ผ่ฅๅทฒ็ปๅฎ่ฃ
ไบๅ
ถไธญ็ไธไธช๏ผๆณ่ฆๅฎ่ฃ
ๅฆไธไธช๏ผๅๅฟ
้กปๅฐไนๅ็ไฝฟ็จ pip uninstall
ๅธ่ฝฝๆใ
pip install opencv-python # ๅชๅ
ๅซไธป่ฆ็ๆจกๅ
pip install opencv-contrib-python # ๅฎๆดๅฎ่ฃ
๏ผๅ
ๅซไธป่ฆๆจกๅๅcontrib/extraๆจกๅ
pip install opencv-python-headless # ๅ
ๅซไธป่ฆๆจกๅ๏ผไฝๅป้คไบcv2.imshow่ฟ็ง่พๅบ็ๅ่ฝ๏ผ้็จไบๆ ๅพๅฝข็้ข็ๆๅกๅจ๏ผไฝ็งฏๆฏopencv-python่ฆๅฐ
pip install opencv-contrib-python-headless
ๅฏผๅ
ฅ
ๆณจๆ็น
cv2
ๅ
้ๅฝๆฐ็ๅๆฐ size
ไธ่ฌไธบ (width, height)
๏ผ่กจ็คบๅฎฝไธ้ซใ
cv2
ไฝฟ็จไฝฟ็จ numpy
ๆฐ็ปๆฅ่กจ็คบๅพๅ๏ผๆฐ็ป็ๆฐๆฎ็ฑปๅๅฟ
้กปไธบ uint8
๏ผๅณ dtype=np.uint8
ใๅนถไธๆฐ็ป็ๅฝข็ถไธบ channel_last
็ๆนๅผ๏ผไพๅฆ๏ผ
RGB ๅพๅ็่กจ็คบๅฝขๅผๆฏๅฝข็ถไธบ (H, W, 3)
็ๆฐ็ป๏ผๅ
ถไธญ้้็ๅญๅจ้กบๅบไธบ BGR
๏ผ่ฎธๅคๅฝๆฐไพๅฆ cv2.putText
ๆฅๆๅๆฐ color
่กจ็คบ้ข่ฒ๏ผๅ
ถๅๅผๅฟ
้กปไธบไธไธชๆดๆฐ็ไธๅ
็ป๏ผไธๅๅผไธบ [0, 255]
ๅบ้ดๅ
็ๆดๆฐ๏ผๅๆ ท้ตๅพช BGR
็้กบๅบ๏ผไพๅฆ๏ผcolor=(255, 0, 0)
่กจ็คบ่่ฒใ
็ฐๅบฆๅพๅ็่กจ็คบๅฝขๅผๆฏๅฝข็ถไธบ (H, W)
็ๆฐ็ป
cv2
ไธญ็ๅฝๆฐๅฏน numpy
ๆฐ็ป่ฟ่กๆไฝๆถๅพๅพ้่ฆ numpy
ๆฐ็ป็ๅ
้จๅญๅจๆฏ C ่ฟ็ปญ็๏ผnumpy
ไธญๅฏนๆฐ็ป็ๆไบๆไฝไผไฝฟๅพๆฐ็ปๅ็ไธๆฏ C ่ฟ็ปญ็๏ผไพๅฆ๏ผ
่ด้ด้็ๅ็ๆไฝ๏ผarr = arr[::-1,:,:]
่ฟ็งๆ
ๅตไธ้่ฆ้ฆๅ
ไฝฟ็จๅฆไธๆนๆณๅฐ numpy
ๆฐ็ป่ฝฌๆข๏ผๅๅฉ็จ cv2
็็ธๅ
ณๅฝๆฐๅฏน่ฝฌๆขๅ็ๆฐ็ป่ฟ่กๆไฝ
arr = np.ascontiguousarray(arr)
cv2
็่ฎธๅคๅฝๆฐๆฏ inplace ็๏ผไพๅฆไธไบ็ปๅถๅพๅ็ๅฝๆฐใไพๅฆ๏ผ
import numpy as np
import cv2
image = np.zeros((224, 224, 3), dtype=np.uint8)
print(image.any()) # False
# ้ๅธธ็ๅๆณๆฏไธ้่ฆๆฅๆถ่ฟๅๅผ็,ๆญคๅคๆฏไธบไบ่ฏดๆ้ฎ้ข
x = cv2.putText(image, "mark", (100, 200), cv2.FONT_HERSHEY_COMPLEX, 5, (0, 0, 255))
print(x.any(), image.any(), x is image) # True, True, True
ๅฆๆไธๆณๅฏนๅๅพ่ฟ่กไฟฎๆน๏ผ้่ฆ้ฆๅ
่ฐ็จ numpy
ๆฐ็ป็ copy
ๆนๆณๅ่ฟ่กๅค็๏ผไพๅฆ๏ผ
image = np.zeros((224, 224, 3), dtype=np.uint8)
print(image.any()) # False
x = cv2.putText(image.copy(), "mark", (100, 200), cv2.FONT_HERSHEY_COMPLEX, 5, (0, 0, 255))
print(x.any(), image.any(), x is image) # True, False, False
cv2
้่ฎธๅคๅฝๆฐ็ๅๆฐ็ๅๅผไผๆฏโๆ ๅฟไฝโ๏ผไพๅฆ๏ผcv2.COLOR_BGR2RGB
๏ผcv2.LINE_8
๏ผๅ
ถๆฅๆบไบ opencv
็ C++ ๆบ็ ไธญๆๅฎไน็ๅฎ๏ผไธ่ฌ็จไบๆงๅถๅไธๆฅๅฃไธ็ไธๅ็ฎๆณ๏ผ่ฟไบๆ ๅฟไฝๅฎ้
ไธๆฏไธไบๆดๆฐ๏ผไพๅฆ๏ผcv2.COLOR_BGR2RGB==4
๏ผcv2.LINE_8==8
ใ้ๅธธๆ
ๅตไธๆจ่ไฝฟ็จๅฎๅ่ไธ่ฆ็ดๆฅไฝฟ็จๅฎๆไปฃ่กจ็ๆดๆฐใ
ๅพๅ่ฏปๅๆไฝ
่ฏฆ็ปๅฏๅ่ๅฎๆนๆๆกฃ
imread
ไธ imwrite
cv2.imread
cv2.imread(filename, flags=cv2.IMREAD_COLOR) -> bool
image = cv2.imread("x.jpg")
cv2.IMREAD_COLOR๏ผๅพๅฐ็ๅพๅไธบ BGR ๆ ผๅผ็ numpy
ๆฐ็ป
cv2.IMREAD_UNCHANGED๏ผๅๆ ท่งฃ็ ๏ผไพๅฆ๏ผ่ฅๅพๅๅๆฌๅญๅจไบ RGBA
ๆ ผๅผ็ๆฐๆฎ๏ผๅ่ฏปๅบๆฅไนไผๆฏๅ้้็ numpy
ๆฐ็ป
cv2.IMREAD_GRAYSCALE๏ผๅพๅฐ็ๅพๅๆฏๅฝข็ถไธบ (H, W)
็็ฐๅบฆๅพๅ
ๅคๆณจ๏ผ้่ฆๆ ผๅคๆณจๆ๏ผ่ฅ่ฏปๅ
ฅไธๆๅ๏ผๆฏๅฆๆไปถ่ทฏๅพไธๅญๅจ๏ผimage
ๅฐไผๆฏ None
๏ผไฝ่ฏฅๆก่ฏปๅ
ฅ่ฏญๅฅๅนถไธไผๆฅ้ๅผ่ตท็จๅบไธญๆญใ
cv2.imwrite
cv2.imwrite(filename, img, params=()) -> bool
cv2.imwrite("x.jpg")
็ฌฌไธไธชๅๆฐๅฏ้๏ผๅซไนๅฆไธ๏ผ
cv2.imwrite("x.jpg", img, (cv2.IMWRITE_JPEG_QUALITY๏ผ 95)) # jpgๆ ผๅผ้ป่ฎคๅผ
cv2.imwrite("x.png", img, (cv2.IMWRITE_PNG_COMPRESSION, 3)) # pngๆ ผๅผ้ป่ฎคๅผ
cv2.IMWRITE_JPEG_QUALITY=1๏ผJPEG ๆ ผๅผ๏ผ0-100 ็ๆดๆฐ๏ผ่กจ็คบๅพๅ่ดจ้๏ผ้ป่ฎคไธบ 95ใ
cv2.IMWRITE_PNG_COMPRESSION=16๏ผPNG ๆ ผๅผ๏ผ0-9่กจ็คบๅ็ผฉ็บงๅซ๏ผ็บงๅซ่ถ้ซๅพๅ่ถๅฐ๏ผ้ป่ฎคๅผไธบ 3ใ
ๅคๆณจ๏ผJPEG ๆ ผๅผไธบๆๆๅ็ผฉ๏ผ่ PNG ๆ ผๅผไธบๆ ๆๅ็ผฉ๏ผPNG ็ๅ็ผฉ็บงๅซไผๅฝฑๅ็ผ็ ่งฃ็ ๆถ้ดใ
ๅคๆณจ๏ผimread ไธ imwrite ็่ฟๅๅผ่ฅไธบ True๏ผๅ่กจ็คบ่ฏปๅ
ฅๆๅๅ
ฅๆๅ๏ผๅฆๅ่กจ็คบๅคฑ่ดฅใ
imencode
ไธ imdecode
imread
ไธ imwrite
ๆฏ้ซ็บงๆฅๅฃ๏ผๅฝ่ทฏๅพไธญๅญๅจไธญๆๅญ็ฌฆๆถ๏ผimread
ไธ imwrite
ไผๆ ๆณ่ฏปๅๆไปถ๏ผ่ฟ็งๆถๅๅฏไปฅ็ปๅ numpy
่ฟ่กๅค็๏ผ
ๅฉ็จไฝ็บงๆฅๅฃ imencode
ไธ imdecode
ๅฏนๆฐๆฎ่ฟ่ก็ผ็ ไธ่งฃ็
ๆไปถ IO ๆไฝไฝฟ็จ numpy
็็ธๅ
ณๆนๆณไพๅฆ fromfile
๏ผtofile
็ญๆนๆณ่ฟ่ก
้ฆๅ
ไป็ป imencode
ไธ imdecode
ๆนๆณ
cv2.imencode
cv2.imencode(ext, img, params=()) -> (bool, np.array)
# ext่กจ็คบๆฉๅฑๅ,็จไบๅณๅฎๅ็ผฉๆนๅผ, imgไธบ(H,W[,C])ๆฐ็ป,paramsไธบๅ็ผฉๅๆฐ
# flag=True่กจ็คบ็ผ็ ๆๅ, encoded_imgๆฏไธไธชๅฝข็ถไธบ(K, 1)็uint8ๆฐ็ป
flag, encoded_img = cv2.imencode(".jpg", img, (cv2.IMWRITE_JPEG_QUALITY๏ผ 95))
cv2.imdecode
cv2.imdecode(buf, flags) -> np.array
# arrๅฏไปฅๆฏไธไธช(K,)็uint8ๆฐ็ป, flags็ๅๅผไธimreadไธ่ด
cv2.imdecode(arr, cv2.IMREAD_COLOR)
ๆณจ๏ผๆญคๅคไผผไนๆ ้ๆๅฎๆไปไนๆนๅผ่งฃ็ ๏ผๆไบๅฏ็ใ
x = np.zeros((224, 224, 3))
y = cv2.imdecode(cv2.imencode(".png", x)[1], cv2.IMREAD_UNCHANGED)
(x==y).all() # True
ๆฅไธๆฅไป็ป numpy
็็ธๅ
ณๆนๆณ๏ผๆณจๆ่ฟไบๆนๆณๆฏไธ cv2
ๆ ๅ
ณ็ไธๅฅ numpy
ๅญๅจๆไปถ็ๅๆณใ
np.ndarray.tofileใnp.ndarray.tobytes
np.ndarray.tofile
็จไบๅฐๆฐ็ปๅญๅ
ฅๆไปถ
arr.tofile(path, ...) -> None
arr.tofile("x.png")
ๆด็ป่ด็ๅๅ
ฅๆนๅผๆฏ๏ผๅ
ๅฐๆฐ็ป็จ np.tobytes
่ฝฌๆขไธบๅญ่๏ผๅๅฐ็ๅพๅฐ็ๅญ่ไปฅไบ่ฟๅถๅฝขๅผๅๅ
ฅๆไปถ
b = cv2.imencode(".png", image)[1].tobytes()
with open("x.png", "wb") as fw:
fw.write(b)
(cv2.imread("x.png") == image).all() # True
np.fromfile
็จไบ่ฏปๅ็จ np.ndarray.tofile
ๅญๅ
ฅ็ๆไปถ
np.fromfile(path, dtype=np.uint8, ...) -> np.array
arr = np.fromfile("x.jpg", dtype=np.uint8)
ๆด็ป่ด็่ฏปๅๆนๅผๆฏ๏ผๅ
ๅฐ่ฆ่ฏปๅ็ๆไปถไปฅไบ่ฟๅถๅฝขๅผ่ฏปๅ๏ผๅๅฐๅพๅฐ็ๅญ่็จ np.frombuffer
ๅพๅฐ
with open("x.png", "rb") as fr:
b = fr.read()
image = cv2.imdecode(np.frombuffer(b, np.uint8), cv2.IMREAD_UNCHANGED)
ๆป็ป๏ผๅคช้ฟไธ็็็ดๆฅ็่ฟ้๏ผ
# ๅๆไปถ, ไธค็ง้ฝๅฏไปฅ
cv2.imencode(".jpg", image)[1].tofile(path) # image: (H, W, 3) np.ndarray, uint8, BGR format
b = cv2.imencode(".png", image)[1].tobytes()
with open("x.png", "wb") as fw:
fw.write(b)
# ่ฏปๆไปถ, ไธค็ง้ฝๅฏไปฅ
image = cv2.imdecode(np.fromfile(path, dtype=np.uint8), cv2.IMREAD_COLOR) # image: (H, W, 3) np.ndarray, uint8, BGR format
with open("a.jpg", "rb") as fr:
b = fr.read()
image = cv2.imdecode(np.frombuffer(b, np.uint8), cv2.IMREAD_UNCHANGED)
RGB ไธ BGR ่ฝฌๆข๏ผcv2 ้ป่ฎคไฝฟ็จ BGR ๆ ผๅผ๏ผ
# ๅฐsrcBGR: uint8ๆฐ็ป(H, W, 3)่ฝฌๆขไธบdestRGB: uint8ๆฐ็ป(H, W, 3)
destRGB = cv2.cvtColor(srcBGR, cv2.COLOR_BGR2RGB)
ๅคๆณจ๏ผๅฏไปฅ่ฐๆด cv2.cvtColor
ๅฝๆฐ็็ฌฌไบไธชๅๆฐ็ๅๅผ่ฟ่ก่ฎธๅคๅ
ถไป็่ฝฌๆข๏ผไพๅฆ๏ผ
cv2.COLOR_BGR2GRAY
๏ผ่กจ็คบๅฐ BGR ๅพ็่ฝฌๆขไธบ็ฐๅบฆๅพ็
ๅจๅพๅไธ็ปๅถ
่ฏฆ็ปๅฏๅ่ๅฎๆนๆๆกฃ
็ป็น๏ผๅฎ้
ไธๆฏๅจ็ปๅ๏ผ
cv.circle(img, center, radius, color, thickness=1, lineType=cv2.LINE_8, shift=0)
# centerไธบๅ
็ป(x, y), radius่กจ็คบๅๅพ, colorไธบไธๅ
็ป(B, G, R), thickness่กจ็คบ็บฟ็็ฒ็ป
cv2.circle(img, (100, 20), 2, (0, 0, 255), thickness=2)
ๅคๆณจ๏ผ
ๆทปๅ ๆๅญ๏ผ้่ฑๆไผไนฑ็ ๏ผ
ๅ
ณไบbottomLeftOrigin็ๆ้๏ผๅพ
่กฅๅ
cv2.putText(image, text, org, font, fontScale, color, thickness=1, lineType=cv2.LINE_8, bottomLeftOrigin=False)
# orgๆฏๅๆ ,fontๆฏๅญไฝ,fontScaleๆฏๅญไฝ
# ๅฝbottomLeftOriginไธบTrueๆถ,่กจ็คบorgๆฏๆๅญ็ๅทฆไธ่งๅๆ ,ๅๅผไธบFalseๆถ๏ผ้ป่ฎคๅผ๏ผ่กจ็คบorgไธบๆๅญ็ๅทฆไธ่งๅๆ ,ๅๅผไธบTrueๆถ่กจ็คบorgไธบๆๅญ็ๅทฆไธ่งๅๆ
cv2.putText(image, "mark", (100, 200), cv2.FONT_HERSHEY_COMPLEX, 5, (0, 0, 255), thickness=2, bottomLeftOrigin=True)
่ทๅๅญไฝๅคงๅฐ
ๆทปๅ ็บฟใ็ฉๅฝขใๅค่พนๅฝข
็ป็บฟๆฎต
cv2.line(img, pt1, pt2, color[, thickness, lineType[, shift]])
# pt1,pt2ๅๅซไธบ่ตท็นๅ็ป็น
cv2.line(img, (10, 10), (200, 10), (0, 0, 255))
็ป็ฉๅฝข
cv2.rectangle(img, pt1, pt2, color[, thickness[, lineType[, shift]]]) -> img
# pt1, pt2ไธบๅทฆไธไธๅณไธ่ง็ๅๆ
cv2.rectangle(image, (10, 10), (20, 30), (0, 0, 255))
็ปๅค่พนๅฝข
ๆนๆณไธ๏ผไฝฟ็จ cv2.polylines
# ๆญคๅฝๆฐ็จไบ็ปๅคไธช็ธๅbๅค่พนๅฝข
cv2.polylines(img, pts, isClosed, color[, thickness[, lineType, [shift]]])
# pts ็ไผ ๅๆนๅผๅพ่ฏกๅผ:
# ็ปไธไธชๅ่พนๅฝข
# (1)ptsๅฏไปฅไฝฟ็จไธไธช้ฟๅบฆไธบ1็ๅ่กจ, ๅ่กจๅ
ไธบไธไธช(4, 2)ๅฝข็ถ็ๆฐ็ป: [(4, 2)]
# (2)ptsไนๅฏไปฅไฝฟ็จไธไธชๅฝข็ถไธบ(1, 4, 2)็ๆฐ็ป: (1, 4, 2)
pts = np.array([[10, 10], [30, 30], [20, 30], [10, 20]])
cv2.polylines(img, [pts], True, (0, 0, 255))
# ็ปKไธชๅคๅ่พนๅฝข
# (1)ptsๅฏไปฅไฝฟ็จไธไธช้ฟๅบฆไธบK็ๅ่กจ, ๅ่กจๅ
ไธบไธไธช(4, 2)ๅฝข็ถ็ๆฐ็ป: [(4, 2),...,(4,2)]
# (2)ptsไนๅฏไปฅไฝฟ็จไธไธชๅฝข็ถไธบ(K, 4, 2)็ๆฐ็ป: (K, 4, 2)
pts = np.array([[[10, 10], [30, 30], [20, 30], [10, 20]],
[[110, 110], [130, 130], [120, 130], [110, 120]]])
cv2.polylines(img, pts, True, (0, 0, 255))
# ่ฏกๅผ็ๆฏ [(4, 1, 2)], [(1, 4, 2)] ่ฟ็งไผ ๆณไนๆฏๅฏน็
ๆนๆณไบ๏ผไฝฟ็จ cv2.drawContours
cv2.drawContours(image, contours, contourIdx, color[, thickness[, lineType[, hierarchy[, maxLevel[, offset]]]]]) -> image
# contoursไธบไธไธชไบ็ปดๆฐ็ป็ๅ่กจ, ไบ็ปดๆฐ็ป็ๅฝข็ถไธบ(K, 2)
# contoursIdxไธบไธไธชๆดๆฐ, ่กจ็คบ่ฆ็ป้ฃไธช, ่ฅๅๅผไธบ-1, ๅ่กจ็คบๅ
จ้จ็ปๅบ
contours = np.array([[20, 20], [40, 20], [40, 40], [30, 30], [20, 40]])
cv2.drawContours(image, [contours], 0, (0, 0, 255))
ๅพๅๅๆข
import sklearn.transform as trans
tform = trans.SimilarityTransform()
src = np.array([[100, 100], [200, 50], [200, 200], [100, 150]])
dst = np.array([[60, 70], [80, 70], [80, 100], [60, 100]])
tform.estimate(src, dst)
M = tform.params # 3*3ๆฐ็ป, ๆๅไธ่กไธบ[0, 0, 1]
src3d = np.ones(3, src.shape[0])
src3d[:-1, :] = src.T
# M @ src3d ~= dst
# M =
# [[a0, -b0, a1],
# [b0, a0, b1],
# [0, 0, 1]]
ๅๆขๅ
ฌๅผไธบ๏ผtform.estimate
ๅฎ้
ไธๅฐฑๆฏๅจไผฐ่ฎก M
๏ผ๏ผ
ๅณ๏ผ
cv2.minAreaRect
# bbox: (k, 2) np.array
center_point, w_h, angle = cv2.minAreaRect(bbox)
# angle่กจ็คบๆๆญฃๅ็็ฉๅฝข้่ฆ็ปcenter_point้ๆถ้ๆ่ฝฌๅคๅฐๅบฆๆ่ฝๅๆขๅฐbbox,็ฉๅฝข็ๅฎฝ้ซๅๅซไธบw_h
# w_h: ๆๅฐ็ฉๅฝข็ๅฎฝๅ้ซ
# center_point๏ผbbox็ไธญๅฟ็น
# ๅคๆณจ๏ผbbox้็็น็้กบๅบๅฏไปฅๆฏไปปๆ็๏ผๅพๅฐ็็ปๆๆฏไธ่ด็
cv2.getRotationMatrix2D
ๆญคๅฝๆฐ็จไบ่ทๅ็ปๆไธไธญๅฟ็นๆ่ฝฌๅ็ๅๆข็ฉ้ต
ๆ
ๅๆข็ฉ้ตไธบ๏ผ
M = cv2.getRotationMatrix2D(center, angle, scale)
# Mๆฏๅฝข็ถไธบ(2, 3)็np.arrayๆฐ็ป, centerๅณไธบไธไพ็(x0,y0), angleๅณไธบtheta, scaleๅณไธบalpha
cv2.warpAffine
dst = cv.warpAffine(src, M, dsize[, dst[, flags[, borderMode[, borderValue]]]])
# Mๆฏไธไธช2*3็ๆฐ็ป, srcไธบๅพๅ, dsizeไธบ็ฎๆ ๅพๅdst็ๅคงๅฐ(w,h), flags่กจ็คบๆๅผๆนๅผ
้ๅๆขๆนๅผๅฏไปฅๆจ็ฎไธบ
cv2.getPespectiveTransform๏ผๅพ
่กฅๅ
๏ผ
ไธfindHomography, warpPerspective, perspectiveTransform็ธๅ
ณ
ๆฑ่งฃๅฆไธๆน็จ
M = cv.getPerspectiveTransform(src, dst[, solveMethod])
# src, dstๅไธบ(4, 2)ๆฐ็ป, ๆฑ่งฃ็Mไธบ3*3ๆฐ็ป
opencv ๅค็่ง้ข
import cv2
cap = cv2.VideoCapture("xx.mp4")
total_frames: float = cap.get(cv2.CAP_PROP_FRAME_COUNT) # ๆปๅธงๆฐ
fps: float = cap.get(cv2.CAP_PROP_FPS) # ๆฏ็ง็ๅธงๆฐ
cap.set(cv2.CAP_PROP_POS_FRAMES, 3) # ๅ็ฌฌไธๅธง
succeed: bool, frame: np.ndarray = cap.read() # ๅ็ฌฌไธๅธง, frameๅณไธบ(H, W, 3) uint8, BGR cv2ๆ ผๅผ็ๅพ็
ๅพๅไฟฎๅค
cv2.inpaint
OpenCV C++ API
ๅฎ่ฃ
๏ผwindows VS2017 + OpenCV C++
https://sevenold.github.io/2019/01/opencv-setup/
Tricks and Discussion and Have Fun
ๆพไธๅฐๅ
ณไบ opencv-python ็ๅ็กฎๆฅๅฃๆๆกฃ