简体   繁体   English

如何屏蔽cv :: Mat中的所有通道?

[英]How to mask all channels in a cv::Mat?

I'm using a HOGDescriptor to detect pedestrians in an image. 我正在使用HOGDescriptor来检测图像中的行人。 My intention is to mask all pixels outside of the person rectangles. 我的意图是遮盖人物矩形之外的所有像素。 I start by creating a mask image of all zeros and for each rectangle would like to overwrite all three channels at an ROI with 0xFF. 我首先创建一个全零的蒙版图像,对于每个矩形,都希望以0xFF的ROI覆盖所有三个通道。

The result ends up being that only a single channel is set to the mask. 结果最终是仅将单个通道设置为掩码。 What I want in effect is to have a black mask with white rectangles. 我想要的实际上是有一个带有白色矩形的黑色蒙版。 I've inspected the matrix output as well and can see that only one component is 255 while the others are 0. 我也检查了矩阵输出,可以看到只有一个分量是255,而其他分量是0。

0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 255 0 0 255 0 0 255 0 0 255 0 0 255 0 0 255 0 0 255 0 0 255 0 0 255 0 0 255 0 0 255 0 0 255 0 0 255 0 0 255 0 0 255 0 0 255 0 0 255 0 0 255 0 0 255 0 0 255 0 0 255 0 0 255 0 0 255 0 0 255 0 0 255 0 0 255 0 0 255 0 0 255 0 0 255 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 255 0 0 255 0 0 255 0 0 255 0 0 255 0 0 255 0 0 255 0 0 255 0 0 255 0 0 255 0 0 255 0 0 255 0 0 255 0 0 255 0 0 255 0 0 255 0 0 255 0 0 255 0 0 255 0 0 255 0 0 255 0 0 255 0 0 255 0 0 255 0 0 255 0 0 255 0 0 255 0 0 255 0 0 255 0 0

void ImageUtil::detectPersonRectangles(cv::Mat image)
{
    cv::HOGDescriptor HoG;
    HoG.setSVMDetector(cv::HOGDescriptor::getDefaultPeopleDetector());

    cv::Mat mask = cv::Mat::zeros(image.size(), image.type());
    std::vector<cv::Rect> found;
    HoG.detectMultiScale(image, found, 0, cv::Size(8,8), cv::Size(32,32), 1.05, 2);
    for (int i = 0; i < found.size(); i++) {
        cv::Rect r = found[i];
        std::cout << " HoG detected rectangle " << i << " : " << r << "\n";
        mask(r) |= 0xFF;
    }

    static int index = 0;
    cv::Mat maskedImage = image & mask;
    std::stringstream name;
    name << "masked_" << index++;
    ImageUtil::dumpDebugImage(mask, name.str());
}

在此处输入图片说明

I'll answer my own question, because I didn't think it was directly obvious at first. 我将回答我自己的问题,因为一开始我并不认为这是直接显而易见的。 We can write a single matrix to the ROI that will be used for each pixel in the ROI. 我们可以将单个矩阵写入ROI,该矩阵将用于ROI中的每个像素。

// Use 0xFF for all channels of the mask.
mask(r) |= cv::Mat(1, image.channels(), CV_8U, {0xFF});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM