【问题标题】:Setting transparency in OpenCV not working在 OpenCV 中设置透明度不起作用
【发布时间】:2013-07-20 18:27:14
【问题描述】:

我正在为这样的四通道Mat 设置透明度(基于一些计算)。但是当我在窗口上显示图像时,图像没有发生变化。任何帮助将是一个很大的支持。

void feather_touch(Rect enclosingRect, Mat frame){

    Point center(frame.size().width * 0.5, frame.size().height * 0.5);
    int inclussive_circle_radius = (sqrt((frame.cols * frame.cols + frame.rows * frame.rows))) / 2;
    for(int i = 0; i < frame.rows; i++){
        for(int j = 0; j < frame.cols; j++){
            Point point(i, j);
            if(!inRect(point, enclosingRect)){
                Vec4b channels = frame.at<Vec4b>(i, j);
                int dx   = center.x - point.x;
                int dy   = center.y - point.y;
                int dist = sqrt((dx * dx) + (dy * dy));
                float alpha = (float)dist/(float)inclussive_circle_radius;
                int a = (int)((1 - alpha) * 255);
                frame.at<Vec4b>(i, j)[3] = a;
            }
        }
    }
}


bool inRect(cv::Point p,Rect rect) {
    return p.x >= rect.x && p.x <= (rect.x + rect.width) && p.y >= rect.y && p.y <= (rect.y + rect.height);
}

【问题讨论】:

标签: c++ opencv mat


【解决方案1】:

我得到了答案:OpenCV 中的imshow 不支持透明度。
我使用addWeighted 功能替换了它。现在我的函数看起来像这样:

float alpha = ((float)dist/(float)inclussive_circle_radius);
//int a = (int)((1 - alpha) * 255);
//frame.at<Vec4b>(i, j)[3] = a;
Rect rect(j, i, 1, 1);
Mat mat = frame(rect);
Mat sub = layer(rect);

if(dist > (enclosingRect.width*0.5)){
    addWeighted(mat, alpha, sub, 1 - alpha, 0, mat);
    mat.copyTo(frame(rect));
}else{
    sub.copyTo(frame(rect));
}

【讨论】:

    猜你喜欢
    • 2016-10-23
    • 2011-05-23
    • 2014-10-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多