SDL2如何在调整窗口大小后缩放鼠标坐标?

时间:2018-10-06 14:29:38

标签: c++ opencv resize window sdl-2

我正在将OpenCV ans SDL2库用于C ++应用程序。

Opencv用于从网络摄像头捕获帧,然后将该帧转换为显示在SDL窗口中。 SDL用于捕获鼠标的输入。

在SDL窗口中的每一次单击,都会在网络摄像头框架上的单击位置绘制一个圆圈。

问题如下:在运行应用程序时手动调整窗口大小(从OS而不是从代码),将正确绘制已经绘制的点,但是如果我单击鼠标左键以在其中绘制另一个点调整大小后的窗口,该点将完全绘制在窗口范围之外。

这是主要功能:

#ifndef __OPENCV__
#define __OPENCV__
#include "opencv2/opencv.hpp"
#endif
#include <iostream>
#include <unistd.h>
#include <vector>
#include <SDL.h>
#include <SDL_events.h>

using namespace cv;
using namespace std;


int mouseCallback(SDL_MouseButtonEvent ev, Mat frame);


int main(int argc, char* argv[])
{
    /* Initialise SDL */
    if( SDL_Init( SDL_INIT_VIDEO ) < 0)
    {
        fprintf( stderr, "Could not initialise SDL: %s\n", SDL_GetError() );
        exit( -1 );
    }
    const String sourceReference = argv[1];
    int camNum;
    string sensorName;
    try
    {
        camNum = stoi(sourceReference); // throws std::length_error
    }
    catch (const std::exception& e)// reference to the base of a polymorphic object
    {
        std::cout<<"Exception: " << e.what()<<endl; // information from length_error printed
        return -1;
    }
    cout<<"camera initializing\n";
    VideoSettings cam(camNum + CAP_V4L);
    cout<<"camera initialized\n";
    /* or
    VideoCapture captUndTst;
    captUndTst.open(sourceCompareWith);*/
    cout<<"Ch3ck c4m3ra is 0p3n3d\n";
    if ( !cam.isOpened())
    {
        cout << "Could not open reference " << sourceReference << endl;
        return -1;
    }
    Mat frame;
    cam>>frame;
    SDL_Window* win = SDL_CreateWindow("Camera", 100, 100, frame.cols, frame.rows,
        SDL_WINDOW_SHOWN | SDL_WINDOW_RESIZABLE);

    SDL_SetWindowTitle(win, "Camera");
    SDL_Renderer * renderer = SDL_CreateRenderer(win, -1, 0);
    SDL_Event genericEvent;

    SDL_Surface* frameSurface;
    SDL_Texture* frameTexture;
    while(cam.isOpened())
    {
        while( SDL_PollEvent(&genericEvent) )
        {
            switch( genericEvent.type )
            {
                case SDL_MOUSEBUTTONDOWN:
                    mouseCallback(genericEvent.button, frame);
                    break;
                /* SDL_QUIT event (window close) */
                case SDL_QUIT:
                    return 0;
                    break;
                default:
                    break;
            }
        }
        cam>>frame;
        draw(frame_out);

        //Convert to SDL_Surface
        frameSurface = SDL_CreateRGBSurfaceFrom((void*)frame_out.data,
            frame_out.size().width, frame_out.size().height,
            24, frame_out.cols *3,
            0xff0000, 0x00ff00, 0x0000ff, 0);

        if(frameSurface == NULL)
        {
            SDL_Log("Couldn't convert Mat to Surface.");
            return -2;
        }

        //Convert to SDL_Texture
        frameTexture = SDL_CreateTextureFromSurface(renderer, frameSurface);
        if(frameTexture == NULL)
        {
            SDL_Log("Couldn't convert Mat(converted to surface) to Texture."); //<- ERROR!!
            return -1;
        }
        //imshow("Camera", frame_out);
        SDL_RenderCopy(renderer, frameTexture, NULL, NULL);
        SDL_RenderPresent(renderer);
        /* A delay is needed to show (it actually wait for an input)*/
        if(waitKey(delay)>delay){;}

    }
    SDL_DestroyTexture(frameTexture);
    SDL_FreeSurface(frameSurface);
    SDL_DestroyRenderer(renderer);
    SDL_DestroyWindow(win);
    return 0;
}


int mouseCallback(SDL_MouseButtonEvent ev, Mat frame)
{

    if(ev.button == SDL_BUTTON_LEFT)
    {
        mouseLeft( ev.x, ev.y);
    }
}

单击鼠标左键时,调用的函数是mouseCallback,它正在调用定义为的函数mouseLeft

void mouseLeft(int x, int y)
{
  Point pt;// = *((Point*)param);
  pt.x = x;
  pt.y = y;
  pts.push_back(pt);
  int ii=0;
  int size = pts.size()-1;
  while(ii<size && size>0)
  {
      if (euclideanDist(pt, pts.at(ii))<minDistance)
      {
          //cout<<pts->size()<<endl;
          pts.erase (pts.begin()+ii);

          // std::vector::end returns an iterator to the element following
          //the last element of the container, not to the last element.
          pts.erase(pts.end()-1);
          size-=2;
      }
      else
      {
          ;
      }
      ii++;

  }
}

pts是Points的向量(它最初是类的成员,但在此示例中可以全局声明)。如果点击点与mouseLeft中的任何点足够接近,则调用pts时,会将其从向量中删除,否则将向后推新的点。

函数draw绘制存储在pts中的点以及连接这些点的线:

void draw(Mat &frame)
{
  vector<Point> new_pts = pts; //avoid modifying the elements we are drawing
  int sizeVector = pts.size();
  if (sizeVector==1)
  {
      try
      {
        circle( frame, pts.at(0), radius, Scalar( 0, 0, 0), FILLED, 8,0);
      }
        catch (const out_of_range& e)
        {
            cout<<"Exception with command "<<endl;
            cout<<"circle( frame, pts.at(0), radius, Scalar( 0, 0, 0), FILLED, 8,0)"<<endl;
        }
  }
  else
  {
    int ii=0;
    while(ii<(sizeVector-1) && sizeVector>0)
    {
        try
        {
          circle( frame, pts.at(ii),   radius, Scalar( 0, 0, 0), FILLED, 8,0);
          circle( frame, pts.at(ii+1), radius, Scalar( 0, 0, 0), FILLED, 8,0);
          line(frame, pts.at(ii), pts.at(ii+1), Scalar( 0, 0, 0 ), 4, 8 );
        }
        catch (const out_of_range& e)
        {
          cout<<"Exception with commands: "<<endl;
          cout<<"\tcircle( frame, pts.at(ii),   radius, Scalar( 0, 0, 0), FILLED, 8,0)"<<endl;
          cout<<"\tcircle( frame, pts.at(ii+1), radius, Scalar( 0, 0, 0), FILLED, 8,0);"<<endl;
          cout<<"\tline(frame, pts.at(ii), pts.at(ii+1), Scalar( 0, 0, 0 ), 4, 8 );"<<endl;
        }

        if(pts.size()<=0) break;
        ii++;

        sizeVector= pts.size();
    }
  }
}

如果不是用SDL_Window* win = SDL_CreateWindow("Camera", 100, 100, frame.cols, frame.rows, SDL_WINDOW_SHOWN | SDL_WINDOW_RESIZABLE);创建窗口,而是将其尺寸变倍,例如:SDL_Window* win = SDL_CreateWindow("Camera", 100, 100, 2*frame.cols, 2*frame.rows, SDL_WINDOW_SHOWN | SDL_WINDOW_RESIZABLE);

,则会发生相同的问题

请注意,发布的代码是对原始代码的重新改编,以使其保持简单易懂。如果有任何遗漏或不清楚的地方,请告诉我,以便我可以正确地编辑问题。

1 个答案:

答案 0 :(得分:0)

问题如下:从SDL捕获的鼠标坐标基本上是正确的:当我将窗口放大时,坐标甚至可以具有较大的值。

问题在于,这些坐标是通过openCV绘制在从凸轮获取的帧上的,该帧始终具有相同的尺寸(基本上是凸轮的分辨率)。

在框架上绘制点之前,我必须在从凸轮获取的图像的尺寸内重新调整鼠标单击的坐标。

我找到了一个解决方案,方法是在定义窗口之后,将窗口的初始尺寸基本上存储在两个变量中:

SDL_Window* win = SDL_CreateWindow("Camera", 100, 100, frame.cols, frame.rows,
    SDL_WINDOW_SHOWN | SDL_WINDOW_RESIZABLE);
int oldWidth = frame.cols, oldHeight= frame.rows;
int width, height;
float scaleX=1, scaleY=1;

然后在已经存在的切换案例中添加以下事件:

            case SDL_WINDOWEVENT:
            {
                if (genericEvent.window.event==SDL_WINDOWEVENT_RESIZED)
                {
                    //oldWidth = width;
                    //oldHeight = height;
                    SDL_GetWindowSize(win, &width, &height);
                    scaleX =  (float)(width)/ float(oldWidth);
                    scaleY = (float)(height) / (float)(oldHeight);
                }
                break;
            }

并将`mouseCallback修改为

int mouseCallback(SDL_MouseButtonEvent ev, float scaleX, float scaleY, Mat frame)
{
    if(ev.button == SDL_BUTTON_LEFT)
    {
        cout<<scaleX<<" "<<scaleY<<endl;
        int scaled_x =  static_cast<int> ((float)(ev.x)/scaleX);
        int scaled_y = static_cast<int> ((float)(ev.y)/ scaleY);
        std::cout<<"scaled x: "<<scaled_x<<", scaled y: "<<scaled_y<<endl;
        mouseLeft( scaled_x,scaled_y);
    }
}