Come creare una forma semitrasparente?

Come creare una forma semitrasparente?

L'immagine seguente illustra la trasparenza utilizzando OpenCV. Devi fare una fusione alfa tra l'immagine e il rettangolo. Di seguito è riportato il codice per un modo per farlo.

#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>

int main( int argc, char** argv )
{
    cv::Mat image = cv::imread("IMG_2083s.png"); 
    cv::Mat roi = image(cv::Rect(100, 100, 300, 300));
    cv::Mat color(roi.size(), CV_8UC3, cv::Scalar(0, 125, 125)); 
    double alpha = 0.3;
    cv::addWeighted(color, alpha, roi, 1.0 - alpha , 0.0, roi); 

    cv::imshow("image",image);
    cv::waitKey(0);
}

In OpenCV 3 questo codice ha funzionato per me:

cv::Mat source = cv::imread("IMG_2083s.png");
cv::Mat overlay;
double alpha = 0.3;

// copy the source image to an overlay
source.copyTo(overlay);

// draw a filled, yellow rectangle on the overlay copy
cv::rectangle(overlay, cv::Rect(100, 100, 300, 300), cv::Scalar(0, 125, 125), -1);

// blend the overlay with the source image
cv::addWeighted(overlay, alpha, source, 1 - alpha, 0, source);

Fonte/Ispirato da:http://bistr-o-mathik.org/2012/06/13/simple-transparency-in-opencv/


Aggiungendo alla risposta di Alexander Taubenkorb, puoi disegnare forme casuali (semitrasparenti) sostituendo il cv::rectangle linea con la forma che vuoi disegnare.

Ad esempio, se vuoi disegnare una serie di cerchi semitrasparenti, puoi farlo come segue:

cv::Mat source = cv::imread("IMG_2083s.png");  // loading the source image
cv::Mat overlay;  // declaring overlay matrix, we'll copy source image to this matrix
double alpha = 0.3;  // defining opacity value, 0 means fully transparent, 1 means fully opaque

source.copyTo(overlay);  // copying the source image to overlay matrix, we'll be drawing shapes on overlay matrix and we'll blend it with original image

// change this section to draw the shapes you want to draw
vector<Point>::const_iterator points_it;  // declaring points iterator
for( points_it = circles.begin(); points_it != circles.end(); ++points_it )  // circles is a vector of points, containing center of each circle
    circle(overlay, *points_it, 1, (0, 255, 255), -1);  // drawing circles on overlay image


cv::addWeighted(overlay, alpha, source, 1 - alpha, 0, source);  // blending the overlay (with alpha opacity) with the source image (with 1-alpha opacity)