C语言使用opencv检测图片是否黑屏
以下是一个简单的C语言程序,使用OpenCV库来检测一张图片是否是黑屏:
#include <stdio.h>
#include <opencv2/opencv.hpp>
using namespace cv;
int main(int argc, char** argv)
{
if (argc != 2)
{
printf("Usage: %s <image_file>\n", argv[0]);
return -1;
}
Mat src = imread(argv[1], IMREAD_GRAYSCALE);
if (src.empty())
{
printf("Unable to read image file: %s\n", argv[1]);
return -1;
}
int rows = src.rows;
int cols = src.cols;
int count = 0; // 记录黑色像素点的数量
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
if (src.at<unsigned char>(i, j) == 0)
{
count++;
}
}
}
float ratio = (float)count / (float)(rows * cols); // 计算黑色像素点的比例
if (ratio > 0.9) // 如果比例大于0.9,则认为是黑屏
{
printf("The image is black screen.\n");
}
else
{
printf("The image is not black screen.\n");
}
return 0;
}
这个程序接受一个图片文件作为参数,并使用OpenCV库中的imread函数将其读入内存。然后,程序遍历图片的每一个像素点,统计黑色像素点的数量。最后,程序计算出黑色像素点的比例,如果比例大于0.9,则认为是黑屏
原文地址: https://www.cveoy.top/t/topic/efJl 著作权归作者所有。请勿转载和采集!