Skip to content

IanSmith123/laji_opencv

Repository files navigation

0x00

笔记地址

学习一下opencv的函数的用法

Refer:

0x01

import cv2

打开文件,保存文件

img = cv2.imread("test.png")
cv2.imwrite("test.jpg", img)
type(img)
# output: <type 'numpy.ndarray'>

转换成为灰度图

img = cv2.imread("test.png")
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

现在得gray就是一个灰度图了,使用方式和img一样

0x02 视频

可以直接打开本机摄像头或者读取视频

cap = cv2.VideoCapture(0)
while True:
  ret, im = cap.read()
  cv2.imshow("camera", im)
  cv2.waitKey(100)
  if not os.path.exists('cap.png'):
    cv2.imwrite("cap.png", im)

逻辑上有点问题,但是大概是这个意思, ret是一个Bool值,表示操作成功或者失败, im是某一帧图像

cap = cv2.VideoCapture("test.mp4")

其他的和上面打开本机摄像头操作一样了

0x03 提取图片轮廓

def filter_carrot(im):
    '''
    type(im) = ndarray of a pic
    return null
    '''
    
    after = cv2.cvtColor(im,cv2.COLOR_BGR2GRAY)

    # convert gray pic to binary
    ret, binary = cv2.threshold(after, 120, 255, cv2.THRESH_BINARY)
    print(ret)
    
    # find border line
    contours, hierarchy = cv2.findContours(binary,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
    
    
    # draw border line in source pic
    cv2.drawContours(im, contours, -1, (0,222,255), 2)
    
    
    cv2.imshow("img", im)
    cv2.waitKey()
    cv2.imwrite("bin.png", binary)

About

opencv从入门到放弃

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages