1

我正在为这样的四通道设置透明度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);
}
4

1 回答 1

2

我得到了答案:imshow在 OpenCV 中不支持透明度。
我通过使用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));
}
于 2013-07-21T07:26:42.383 回答