在透明 Mat 上绘制网格

Draw grid on transparent Mat

我想创建一个在透明背景上带有网格的垫子,我可以将其放在其他垫子之上。我挣扎于透明部分并放在上面

Mat image = imread("pic.jpg");
Mat grid = new Mat(image.size(), CV_8UC4, new Scalar(0, 0, 0, 0);

for (//times)
    // draw grid with: line(grid, ... )     

grid.copyTo(image);

首先网格垫根本不透明它是黑色的。标量不就是这样构造的吗?

new Scalar(Blue, Green, Red, Alpha)

另外,如何将一张图片与另一张图片叠加?这只是覆盖。

这是用 C++ 编写的示例程序,但在 java 中应该非常相似:

    cv::Mat input = cv::imread("../inputData/Lenna.png");

    cv::Mat inputBGRA;
    cv::cvtColor(input, inputBGRA, CV_BGR2BGRA);

    cv::Mat gridSolid = cv::Mat(input.size(), inputBGRA.type(), cv::Scalar(0,0,0,0));
    cv::Mat gridMask = cv::Mat(input.size(), CV_8UC1, cv::Scalar(0));

    cv::Mat gridAlpha = cv::Mat(input.size(), inputBGRA.type(), cv::Scalar(0,0,0,0));


    cv::line(gridSolid, cv::Point(0,0), cv::Point(512,512), cv::Scalar(0,255,0,255), 10);
    cv::line(gridSolid, cv::Point(0,512), cv::Point(512,0), cv::Scalar(0,255,0,255), 10);
    cv::line(gridMask, cv::Point(0,0), cv::Point(512,512), cv::Scalar(255), 10);    // single channel
    cv::line(gridMask, cv::Point(0,512), cv::Point(512,0), cv::Scalar(255), 10);    // single channel

    // copy and use the mask. copying eliminates the original values where the mask is set
    cv::Mat outputCopy = inputBGRA.clone();
    gridSolid.copyTo(outputCopy,gridMask);



    // here set the scalar alpha value to less than 255
    // both lines use different alpha values
    cv::line(gridAlpha, cv::Point(0,0), cv::Point(512,512), cv::Scalar(0,255,0,120), 10);
    cv::line(gridAlpha, cv::Point(0,512), cv::Point(512,0), cv::Scalar(0,255,0,180), 10);

    cv::Mat outputWeightSum = inputBGRA.clone();
    //cv::addWeighted(inputBGRA, 0.5, gridAlpha, 0.5, 0, outputWeightSum);

    // manually add weighted sum PER ALPHA VALUE:
    for(int y=0; y<outputWeightSum.rows; ++y)
        for(int x=0; x<outputWeightSum.cols; ++x)
        {
            // the bigger the alpha value, the less of the original image is kept at that pixel
            cv::Vec4b imgPix = outputWeightSum.at<cv::Vec4b>(y,x);
            cv::Vec4b gridPix = gridAlpha.at<cv::Vec4b>(y,x);

            // use alpha channel vor blending
            float blendpart = (float)gridPix[3]/(float)255;

            // set pixel value to blended value
            outputWeightSum.at<cv::Vec4b>(y,x) = blendpart * gridPix + (1.0f-blendpart) * imgPix;
        }

事实上,在此示例中您不需要 alpha 通道,但如果您有更复杂的 "grids" 和不同的 alpha 值,它可能会很好。

我得到这些结果:

  1. 方法:复制:

  1. 方法:与 alpha 通道混合: