View Single Post
Old 24th February 2021, 09:20   #2  |  Link
Valentin Nikin
Registered User
 
Join Date: Feb 2021
Posts: 4
Simple code example
Code:
 
#include <opencv2/opencv.hpp>
#include <iostream>
#include <stdio.h>

using namespace cv;
using namespace std;

int main(int, char**) {
    Mat frame;
    VideoCapture cap("./video/Test_zapis_34.mp4");
    
    if (!cap.isOpened()) {
        cerr << "ERROR! Unable to open camera\n";
        return -1;
    }

    int min = 5;
    int max = 246;
    int frameCounter = 0;

    std::cout << "Start grabbing" << std::endl << "Press any key to terminate" << std::endl;
    for (;;) {
        cap.read(frame);
        if (frame.empty()) {
            cerr << "ERROR! blank frame grabbed\n";
            break;
        }

        int countPixelsOutsideGamut = 0;

        for (int i = 0; i < frame.rows; i++) {
            for (int j = 0; j < frame.cols; j++) {
                auto cmp = frame.at<Vec3b>(i, j);

                int r = cmp[0];
                int g = cmp[1];
                int b = cmp[2];

                std::cout << r << " " << g << " " << b << std::endl;

                if (r < min || r > max || g < min || g > max || b < min || b > max) {
                    countPixelsOutsideGamut++;
                }
            }
        }

        double percent = static_cast<double>(countPixelsOutsideGamut) / static_cast<double>(frame.rows * frame.cols) * 100;

        std::cout << frameCounter << " - " << percent << std::endl;

        imshow("Live", frame);

        frameCounter++;

        if (waitKey(5) >= 0) {
            break;
        }
    }
    return 0;
}
Valentin Nikin is offline   Reply With Quote