的std ::矢量 到cv :: Mat

问题描述 投票:0回答:1

将包含C,:: Vec3b矢量中的R,G和B颜色值的矢量转换为cv :: Mat的最佳方法是什么?像素的位置无关紧要,我只想执行cv :: split操作,然后将所有三个颜色级别放到直方图中。

我尝试了这个,但它不起作用,因为拆分操作返回一个只有一个颜色平面的cv :: Mat:

cv::Mat GetOnlyColoredPixel(cv::Mat& Gray, cv::Mat& Col) {
    std::vector<cv::Vec3b> ColorStack;
    cv::Vec3b vec;
    int nPixel = 0;

    for (int y = 0; y < Gray.rows; y++) {
        for (int x = 0; x < Gray.cols; x++) {
            if (Gray.at<unsigned char>(y, x) == 255) {
                ColorStack.push_back(Col.at<cv::Vec3b>(y, x));
                nPixel++;
            }
        }
    }

    return cv::Mat(1, nPixel, CV_8UC3, &ColorStack[0]);
}

奇怪的是,我真的应该只有黄色像素,但在打印MAT对象时,我会得到所有可能的颜色。 Col.at<cv::Vec3b>(y, x)返回正确的颜色,但按RGB顺序 - 我认为,OpenCV正在使用BGR。

谢谢你,Jan

opencv vector colors mat
1个回答
0
投票

好的,返回错了。这段代码工作正常:

cv::Mat GetOnlyColoredPixel(cv::Mat& Gray, cv::Mat& Col) {
std::vector<cv::Vec3b> ColorStack;
cv::Vec3b vec, vec2;

cv::imwrite("C:\\HomeC\\Test.bmp", Col);

for (int y = 0; y < Gray.rows; y++) {
    for (int x = 0; x < Gray.cols; x++) {
        if (Gray.at<unsigned char>(y, x) == 255) {
            //ColorStack.push_back(Col.at<cv::Vec3b>(y, x));
            vec = Col.at<cv::Vec3b>(y, x);
            ColorStack.push_back(vec);
        }
    }
}

cv::Mat RetVal = cv::Mat(1, ColorStack.size(), CV_8UC3);
memcpy(RetVal.data, ColorStack.data(), ColorStack.size() * sizeof(cv::Vec3b));
return RetVal;
}
© www.soinside.com 2019 - 2024. All rights reserved.