我正在解决一个问题,我需要为图像中的白色区域找到边界框。由于我正在处理真实世界的图片,因此我在RGB值上设置了阈值,我从中创建了一个蒙版,然后对其进行了标记并从中获取了边框的坐标。这是代码。
import numpy as np
from skimage import io, measure
def bin_labelled_img_to_bboxes(bin_image_labelled):
bboxes = []
for j in np.unique(bin_image_labelled):
if j == 0:
continue
curr = (bin_image_labelled == j)
if np.sum(curr) < 50*50:
continue
indices = np.nonzero(curr)
miny = np.min(indices[0])
minx = np.min(indices[1])
maxy = np.max(indices[0])
maxx = np.max(indices[1])
bboxes.append(((miny, minx), (maxy, maxx)))
return bboxes
class WhiteSeperator(object):
def __init__ (self, img_path):
self.img_path = img_path
self.img = io.imread(self.img_path)
self.bin_image_labelled = np.zeros((self.img.shape[0], self.img.shape[1]))
self.bboxes = []
def get_bin_labelled_img(self):
img = self.img
chan1 = (img[:,:,0] > 200) * (img[:,:,0] <= 255)
chan2 = (img[:,:,0] > 180) * (img[:,:,0] <= 255)
chan3 = (img[:,:,0] > 140) * (img[:,:,0] <= 255)
bin_img = (chan1*chan2*chan3)
bin_image_labelled = measure.label(bin_img)
return bin_image_labelled
def get_white_bboxes(self):
final_white_bboxes = []
self.bin_image_labelled = self.get_bin_labelled_img()
white_bboxes = bin_labelled_img_to_bboxes(self.bin_image_labelled)
for bbox in white_bboxes:
width = bbox[1][1]-bbox[0][1]
height = bbox[1][0]-bbox[0][0]
if height > 80 and width > 200:
self.bboxes.append(bbox)
final_white_bboxes.append(bbox)
return final_white_bboxes
对于高分辨率图像(3000像素x 2000像素),每个图像大约需要3-11秒。我的假设是每幅图像的时间差异取决于找到的白色边框的数量(此处归咎于bin_labelled_img_to_bboxes函数) 由于我必须在视频帧上执行此操作,因此即使3秒也非常慢。可以通过更有效的方式完成上述操作吗?