importcv2# OpenCV读取格式是BGR(与其他库使用时注意格式是否一致)img=cv2.imread("img path")# The type of img -> 'numpy.ndarray', dtype=uint8 -> [0, 255]# show the imagecv2.imshow('image',img)# The waiting time(ms), if the para is zero, waiting the key enteringcv2.waitKey(0)# Destroy the windowcv2.destroyWindow('image')# can also use cv2.destroyAllWindows()
importcv2video=cv2.VideoCapture("video path")print(type(video))# cv2.VideoCaptureopened=True# Check that it is opened correctlyifvideo.isOpened():ret,frame=video.read()# ret -> it is read correctly(Every frame), frame -> image (Every frame)else:opened=False# traversal videowhileopened:ret,frame=video.read()ifframeisNone:# After reading the frame is None, exitbreakifret:gray=cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY)# Convert to grayscale(灰度图)cv2.imshow('result',gray)ifcv2.waitKey(100)&0xFF==27:# waiting time, and enter esc to exitbreakvideo.release()"""
The method is automatically called by subsequent VideoCapture::open and by VideoCapture
. destructor.
.
. The C function also deallocates memory and clears \*capture pointer.
"""cv2.destroyAllWindows()
defthreshold(src,thresh,maxval,type,dst=None):# real signature unknown; restored from __doc__"""
threshold(src, thresh, maxval, type[, dst]) -> retval, dst
. @brief Applies a fixed-level threshold to each array element.
.
. The function applies fixed-level thresholding to a multiple-channel array. The function is typically
. used to get a bi-level (binary) image out of a grayscale image ( #compare could be also used for
. this purpose) or for removing a noise, that is, filtering out pixels with too small or too large
. values. There are several types of thresholding supported by the function. They are determined by
. type parameter.
.
. Also, the special values #THRESH_OTSU or #THRESH_TRIANGLE may be combined with one of the
. above values. In these cases, the function determines the optimal threshold value using the Otsu's
. or Triangle algorithm and uses it instead of the specified thresh.
.
. @note Currently, the Otsu's and Triangle methods are implemented only for 8-bit single-channel images.
.
. @param src input array (multiple-channel, 8-bit or 32-bit floating point).
. @param dst output array of the same size and type and the same number of channels as src.
. @param thresh threshold value.
. @param maxval maximum value to use with the #THRESH_BINARY and #THRESH_BINARY_INV thresholding
. types.
. @param type thresholding type (see #ThresholdTypes).
. @return the computed threshold value if Otsu's or Triangle methods used.
.
. @sa adaptiveThreshold, findContours, compare, min, max
"""pass