import cv2 import numpy from matplotlib import pyplot as plt #Define file path for input video videoInputPath = r'E:\Footage\DJI_0001.MOV' #Capture video and find frame count, frame rate, and time between frames. cap = cv2.VideoCapture(videoInputPath) frameCount = int(cap.get(cv2.CAP_PROP_FRAME_COUNT)) frameRate = cap.get(cv2.CAP_PROP_FPS) timeBetweenFrames = 1000/frameRate print(str(frameCount) + " frames total.") print(str(frameRate) + " frames per second.") print(str(timeBetweenFrames) + " ms per frame.") #Loop for image processing video frames #for i in range(0, frameCount): for i in range(0, 1): #Read frames and convert to grayscale ret, frame = cap.read() gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) pixelCount = gray.size print("Frame " + str(i) + " of " + str(frameCount - 1) + " has " + str(pixelCount) + " total pixels.") # Display histogram for image plt.hist(gray.ravel(), 256, [0, 256]) plt.show() # Count how many pixels are at each intensity value histValues = [0] * 256 for j in range(1, 255): pixelRange = cv2.inRange(gray, j, j) valueCount = cv2.countNonZero(pixelRange) #Assign the count for each pixel value to the index value that matches the pixel value histValues[j] = valueCount #Plot bar chart histogram plt.bar(j, valueCount) nonZeroPixels = numpy.sum(histValues) zeroPixels = pixelCount - nonZeroPixels histValues[0] = zeroPixels listLength = len(histValues) #Show bar chart histogram plt.show() #Plot and show the line chart histogram plt.plot(histValues) plt.show() #Find location of maximum value maxValue = float(numpy.max(histValues)) maxValueIndex = int(numpy.argmax(histValues)) print("Max value is " + str(maxValue) + " at pixel value " + str(maxValueIndex) + ".") print("Frames per second: " + str(frameRate)) #Close video cap.release()