

fails because BGR images are 8u images, so it doesn't accept floats.įor your trial 2, note that opencv usually works with numpy arrays (as in zeros example above). Print("total_white_pixels_in_video_sequence : ", total_white_pixels_in_video_sequence)Īlso note that you can test separately the conversion of fully black images: gray = cv2.cvtColor(np.zeros((256,256,3), dtype=np.ubyte), cv2.COLOR_BGR2GRAY) Raise Exception('Failed to read the frame number: '.format(frame_number, number_of_frames)) Number_of_frames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))įor frame_number in range(number_of_frames): cap = cv2.VideoCapture('control_random.mp4') Here is the updated code, it works with one of my mp4. This allows to use a regular for loop and keep this boolean for error cases. Maybe you used the while loop because the number of frames is kind of weird to get, you have to use: number_of_frames = video_capture.get(cv2.CAP_PROP_FRAME_COUNT) Therefore to avoid your problem you have to read the returned value "_" and break if it's False (indicating that no frame have been acquired).
Color2gray python undefined code#
The original error probably happen in the end of the video.Īt least we can say for sure that in the end of the video this code will output an error because _, frame = cap.read() will return frame=None in the end. My guess is that you had this error at different time points and that's what's confusing here. Your help is highly appreciated and desperately needed. Transformations = get_view_matrix(z=scrDist)


This piece of my code create my animation: for Nframes in range(8): I can also do it while I am creating the animation, and before generating the video. My question: is it worth it to debug this, or is there another way to compute the number of white pixels per frame. According to some URL that no longer exists, in a previous (ancient) version the constant was named COLORBGR2LAB (Python is case sensitive). I checked the integrity of my video file, following this link recommendations, and I tried this command in my Linux terminal: ffmpeg -v error -i control_random.mp4 -f null - 2>error.log but it is not printing anything. I checked the path, and it is in the same directory of this code. When I print frame, I get an array of zeros. Then I tried gray = cv2.cvtColor(cv2.UMat(frame), cv2.COLOR_BGR2GRAY), and got the error:ĪttributeError: 'cv2.UMat' object has no attribute 'shape'.TypeError: Expected cv::UMat for argument 'src' I tried gray = cv2.cvtColor(np.float32(frame), cv2.COLOR_BGR2GRAY), but I still got an error:.I found here two possible solutions to this error: Print ("total_white_pixels_in_video_sequence : ", total_white_pixels_in_video_sequence)Įrror: OpenCV(4.1.0) /io/opencv/modules/imgproc/src/color.cpp:182:Įrror: (-215:Assertion failed) !_src.empty() in function 'cvtColor' Total_white_pixels_in_video_sequence += np.count_nonzero(gray = 255) # Counting the number of pixels with given value: define range of gray color in HSV Gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) In an attempt to compute the number of white pixels in each frame of my animation(white circle moving on black frame), I came across this OpenCV function: cap = cv2.VideoCapture('control_random.mp4')
