imshow调整大小+计算白色像素

时间:2016-07-22 08:21:38

标签: python opencv

这是一个从您的计算机上读取视频文件的简单程序。我原来的视频是1080p分辨率。当我运行代码时,程序的屏幕(imshow)超过了我的电脑屏幕,只有720p。如何将其从1080p调整为720或480p。第二个问题,如何在屏幕上找到像素数?

我的代码:

import numpy as np
import cv2

cap = cv2.VideoCapture('apple.mp4')

while(cap.isOpened()):
    ret, frame = cap.read()

    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

    cv2.imshow('frame',gray)
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

cap.release()
cv2.destroyAllWindows()

1 个答案:

答案 0 :(得分:0)

您可以使用cv2.resize()imshow()之前调整图片大小以适应屏幕,并获取您可以使用的Mat中的像素总数gray.shape[0] * gray.shape[1]

import numpy as np
import cv2

cap = cv2.VideoCapture('apple.mp4')

total_white_pixels_in_video_sequence = 0

while(cap.isOpened()):
    ret, frame = cap.read()
    frame = cv2.resize(frame, None, fx = 0.5, fy = 0.5, interpolation = cv2.INTER_CUBIC)

    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

    print "No. of pixels : ", gray.shape[0] * gray.shape[1]
    # Counting the number of pixels with given value
    total_white_pixels_in_video_sequence += np.count_nonzero(gray == 255)

    cv2.imshow('frame',gray)
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

cap.release()
cv2.destroyAllWindows()

print "total_white_pixels_in_video_sequence : ", total_white_pixels_in_video_sequence