批量检测图像白边
时间: 2023-07-04 21:14:04 浏览: 59
批量检测图像白边的方法类似于单张图像的检测方法,只需要将单张图像的检测代码封装成一个函数,然后对每张图像调用该函数即可。代码示例如下:
```python
import cv2
import numpy as np
import os
def detect_white_edge(img_path):
img = cv2.imread(img_path)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
hist = cv2.calcHist([gray], [0], None, [256], [0, 256])
white_pixels = np.sum(hist[255:])
total_pixels = gray.shape[0] * gray.shape[1]
white_ratio = white_pixels / total_pixels
if white_ratio < 0.01:
print(f'{img_path}: 图像没有白边')
else:
print(f'{img_path}: 图像有白边')
if __name__ == '__main__':
img_dir = 'image_dir'
for img_file in os.listdir(img_dir):
img_path = os.path.join(img_dir, img_file)
detect_white_edge(img_path)
```
在这个例子中,我们定义了一个名为`detect_white_edge`的函数,该函数接受一个图像路径作为输入参数,然后返回该图像是否有白边的判断结果。在主函数中,我们遍历指定目录下的所有图像文件,对每张图像调用`detect_white_edge`函数即可。