Newbie help with overlay in C++

I am learning both Python and C++ with opencv on Raspberry pi 4. I have a small example file that works in python but is too slow. I tried to convert to C++ and find two parts that stump me and google. the lines are:
if (watermark[{i, j}][3] != 0) { that seems to be a syntax error about square brackets and the line about numpy.//overlay = np.zeros({frame_h, frame_w, 4}); for this I need help converting to c++ way! I will append the C++ file below. I can also provide the python code if necessary. the file attached will compile and run, but does not really work. The commented out parts reflect problems with the two above lines. Any help or pointers will be greatly appreciated. here is code:

#include <opencv2/imgcodecs.hpp>
#include <opencv2/highgui.hpp>
//OpenCV header to use VideoCapture class//
//`pkg-config --cflags --libs opencv4`
using namespace std;
using namespace cv;

int main() {
    cout << "Starting Camera!" << endl;
    VideoCapture cap(0);//Declaring an object to capture stream of frames from default camera
    string img_path="t2white.png";
    string save_path="t2white.avi";
    Mat myTool=imread(img_path, -1);
    Mat frame;
    Mat watermark;
    Mat myImage;
    Mat overlay;
    int h_offset, w_offset, offset, i, j;

    //resize(myTool, watermark, Size(), 0.50, 0.50);
    resize(myTool, watermark, Size(100, 100), INTER_LINEAR);
    cvtColor(myTool, watermark, COLOR_BGR2GRAY);
    namedWindow("Pic", WINDOW_AUTOSIZE);
    //imshow("Pic", myTool);
    imshow("Pic", watermark);

    int watermark_h=watermark.size().height;
    int watermark_w=watermark.size().width;

    cout << "Height and Width :" << watermark.rows << "x" << watermark.cols << endl;

    cout << watermark_h  << endl;
    cout << watermark_w  << endl;
    //512 for each!
    //resize to 100 each...

    //int frame_h=frame.size().height;
    //int frame_w=frame.size().width;
    //cout << frame_h  << endl;
    //cout << frame_w  << endl;
    //960 by 1280

    while (true) {;
    cvtColor(frame, watermark, COLOR_BGR2BGRA);
    //cout << frame_h  << endl;
    //cout << frame_w  << endl;

    //overlay = np.zeros({frame_h, frame_w, 4});

    for (i=0; i<watermark_h; i++) {
        for (j=0; j<watermark_w; j++) {
        //cout << i << endl;
        //cout << j << endl;
            if (watermark[{i, j}][3] != 0) {
                offset = 350;
                h_offset = (frame_h - watermark_h) - offset;
                w_offset = (frame_w - watermark_w) - offset;
                overlay[{h_offset + i, w_offset + j}] = watermark[{i, j}];


    //addWeighted(overlay, 0.25, watermark, 1.0, 0, frame);
    //cvtColor(watermark,frame, COLOR_BGRA2BGR);
    imshow("frame", frame);

    char c = (char)waitKey(25);//Allowing 25 milliseconds frame processing time and initiating break condition//
      if (c == 27){ //If 'Esc' is entered break the loop

    cap.release();//Releasing the buffer memory
    return 0;


Please edit your post and select c++ code and use </> icon to indent it

First of all destroyWindow(“Pic”);
destroyWindow(“frame”); are not necessary and I think Mat myImage declaration is also not needed
You can use Mat::zeros() function to create a matrix of zeros

Thank you. I was trying many different things to get something on screen to work. You are correct. I started over and made more progress. I gave up trying to convert python to c++. I am working my way thru a new c++ tutorial now. I may never get the overlay/watermark python to work, but at least I am making some progress with c++ and having more fun.

1 Like