-1

I am trying to create a video maker to make test videos for my project. In which I should be able to change the background of the image, create a rectangular object on top of it and move the object in a straight, circular or sine path trajectory. I also want to change the speed of the object.

For now, I have selected background and created a rectangular object and overlayed it on my background image.

I don't know how to go about moving the image. I am giving an initialX and initialY position. To move it I have to change these values. But how to move it in a defined manner and change speed. I am using OpenCV, c++ in QT creator on Ubuntu.

void MainWindow::on_pushButton_Generate_clicked()
{
    int height=ui->lineEdit_Height->text().toInt();
    int width=ui->lineEdit_Width->text().toInt();
    int intensity= ui->lineEdit_Intensity->text().toInt();
    int initialX=ui->lineEdit_initialX->text().toInt();
    int initialY=ui->lineEdit_initialY->text().toInt();

    imageBack= imread(filename);
    cvtColor(imageBack, imageBack, cv::COLOR_RGB2GRAY);


rectangle(imageBack,Point(initialX,initialY),Point(initialX+width,initialY+height),Scalar(intensity),-1,8,0);
            imshow("image",imageBack);

    }

enter image description here

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
A.k.
  • 192
  • 1
  • 22
  • 1
    if it is just velocity and no acceleration or friction it is simple, just put a update timer and redraw the rectangle at initialx += velocity * time , do not forget to use a "clean image" without the previous rectangle. If you need acceleration and other forces, then just apply the physics formulas, just that instead of meters you have pixels – api55 Oct 30 '18 at 07:49
  • Can you show me a snippet of update timer? – A.k. Oct 30 '18 at 07:52
  • you can take a look to [this nice answer](https://stackoverflow.com/questions/37053036/update-gui-at-different-time-interval-in-qt) which basically has all that you need. So basically you create a timer and connect it to a callback function which will update the image. I can give you a complete answer later, but I do not have Qt in this computer, so i need to get home first or I can give you an answer untested :) – api55 Oct 30 '18 at 08:06
  • You can give me an untested answer. I will let you know if I get it. :) – A.k. Oct 30 '18 at 08:13
  • @api55 I got the linear motion now working on sine and circular. – A.k. Oct 30 '18 at 09:44

1 Answers1

1

I haven't worked with Qt in a long time, but basically this will be the gist of it. Also, it is important to note that you can do it in more than one way :) I will tell you 2 that I know. 1 is with a timer which I will explain and the other one is with a widget and the paintEvent which I have not done it, so I will not explain in detail.


For the timer one

First create the timer, and connect it to a function, this way, when the timer "ends" it will call the function and update automatically. This maybe is better if you want different update intervals.

// this is the parent... this could be MainWindow for example
timer= new QTimer(this);
// connect the function to it
connect(timer, SIGNAL(timeout()), this, SLOT(NextImage()));
// set that it will be use for more than 1 time
timer.setSingleShot(false);
// set the time you want to use like for 10hz it is 100ms
timer.setInterval(100);
// and start it
timer.start();

I will assume that you have a place you stored your timer, and the current x and y starting with your initial values. Then create the function to set the new image:

void NextImage()
{
  // lets assume that it is 0.1 seconds as in the first part and the speed is pixel per sec
  currentX = currentX + (0.1 * velocityX);
  currentY = currentY + (0.1 * velocityY);
  // make sure it is inside the image bounds
  currentX = std::min(width - 1, std::max(currentX, 0));
  currentY = std::min(height - 1, std::max(currentY, 0));
  // avoid reading and converting the image each time, just keep an original copy to use
  imageBack = originalImage.clone();

  rectangle(imageBack, Point(initialX,initialY), Point(initialX+width,initialY+height), Scalar(intensity),-1,8,0);

  // you were using imshow, don't forget to use waitKey or use a Qt label
  imshow("image",imageBack);
  waitKey(1);

}

For the paintEvent one, I have not use it so I can not go into details. Here is a link that may help you a little bit, or maybe you get another answer with a better solution :)

api55
  • 11,070
  • 4
  • 41
  • 57
  • I am able to do it. But I want to change setInterval property in real-time. But I am only able to increase it, can't decrease it. – A.k. Nov 28 '18 at 05:39