OpenCV C++ 检测图片局部花屏:代码示例及原理
以下是一个简单的 OpenCV C++ 程序,用于检测图片是否存在局部花屏。
#include <opencv2/opencv.hpp>
#include <iostream>
using namespace cv;
using namespace std;
int main(int argc, char** argv)
{
// 读取图片
Mat img = imread('test.jpg');
// 将图片转换为灰度图
Mat gray;
cvtColor(img, gray, COLOR_BGR2GRAY);
// 对灰度图进行高斯滤波
Mat blurred;
GaussianBlur(gray, blurred, Size(9, 9), 0);
// 对滤波后的图像进行二值化处理
Mat thresh;
threshold(blurred, thresh, 200, 255, THRESH_BINARY);
// 查找轮廓
vector<vector<Point>> contours;
findContours(thresh, contours, RETR_EXTERNAL, CHAIN_APPROX_SIMPLE);
// 检测每个轮廓的高宽比是否在指定范围内
for (int i = 0; i < contours.size(); i++) {
Rect rect = boundingRect(contours[i]);
float aspect_ratio = float(rect.width) / float(rect.height);
if (aspect_ratio > 1.5 && aspect_ratio < 3) {
// 存在局部花屏
cout << '局部花屏' << endl;
return 0;
}
}
// 图片正常,没有局部花屏
cout << '正常图片' << endl;
return 0;
}
程序的核心部分是对图片进行高斯滤波和二值化处理,然后查找轮廓并计算每个轮廓的高宽比是否在指定范围内。如果发现高宽比在1.5到3之间的轮廓,就判断图片存在局部花屏。
原文地址: https://www.cveoy.top/t/topic/odZf 著作权归作者所有。请勿转载和采集!