【问题标题】:how to store values of x.y coordinates in different arrays如何将 x.y 坐标的值存储在不同的数组中
【发布时间】:2020-10-03 06:14:11
【问题描述】:

以下代码在鼠标单击时返回x,y 坐标的值。我想将 x 坐标存储在一个数组 a[10] 中,将 y 坐标存储在另一个数组 b[10] 中。

为此,我尝试使用 for 循环,但 x,y 坐标未显示在数组中。

如何将这些坐标存储在数组中?

我想在图像上单击鼠标 10 次,我想将所有 10 个坐标存储在数组中。在我的代码中,当我单击鼠标一次时,这个 x,y 坐标存储在数组中 10 次。

#include <iostream>
#include <opencv2/highgui.hpp>
#include <opencv2/imgproc.hpp>

using namespace cv;
using namespace std;
int i;int x;int y;
int a[10];int b[10];
void CallBackFunc(int event, int x, int y, int flags, void* userdata);

int main(int argc, char** argv)
{
     // Read image from file
     Mat img = imread("G:/qt-program/CA2.jpg");

     //Create a window
     namedWindow("My Window", 1);


     //set the callback function for any mouse event
   setMouseCallback("My Window", CallBackFunc, NULL);

     //show the image
     imshow("My Window", img);

     // Wait until user press some key
     waitKey(0);

     return 0;

}


void CallBackFunc(int event, int x, int y, int flags, void* userdata)
{

     if  ( event == EVENT_LBUTTONDOWN )
     {
          cout << "Left button of the mouse is clicked - position (" << x << ", " << y << ")" << endl;


            for ( i = 0; i < 10; ++i){

               a[i]=x;
               b[i]=y;

          }
     for ( i = 0; i < 10; ++i){
     cout << a[i] << endl;
     cout << b[i] << endl;
}

    }


}

【问题讨论】:

  • 您将当前 x 和 y 存储到数组中的所有单元格中,但您需要存储在下一个单元格中。您可以为数组中下一个单元格的索引创建全局变量。
  • 所以当鼠标点击时这个函数会被调用。单击时鼠标的位置将是一个点,比如说(x,y),您正在运行一个循环 10 次以将相同的 x 和 y 存储在您的数组中。
  • 我想在图像上单击鼠标 10 次,我想将所有 10 个坐标存储在数组中。在我的代码中,当我单击鼠标一次时,这个 x,y 坐标存储在数组中 10 次.

标签: c++ arrays image opencv


【解决方案1】:

您将当前 x 和 y 存储到数组中的所有单元格中,但您需要存储在下一个单元格中。您可以为数组中下一个单元格的索引创建全局变量,即执行类似这样的操作(伪代码)

int a[10]();
int b[10]();
int nextInd = 0;
...
CallBack(event, x, y, ...) {
  if (...) {
    if (nextInd >= 10) nextInd=0;

    a[nextInd] = x;
    b[nextInd++] = y;
  } 
}

【讨论】:

    【解决方案2】:

    评论很好地解释了您的问题。如果您需要确切的解决方案,只需将您的 CallBackFunc() 函数替换为:

    void CallBackFunc(int event, int x, int y, int flags, void* userdata)
    {
    
        if  ( event == EVENT_LBUTTONDOWN )
        {
            cout << "Left button of the mouse is clicked - position (" << x << ", " << y << ")" << endl;                
            a[i]=x;
            b[i]=y;
            i++;
    
            if(i==10)        
            for ( i = 0; i < 10; ++i){
                cout << a[i] << endl;
                cout << b[i] << endl;
            }        
        }    
    }
    

    【讨论】:

    • 鼠标左键被点击 - 位置 (45, 187) 鼠标左键被点击 - 位置 (103, 98) 鼠标左键被点击 - 位置 (228, 33)单击鼠标左键 - 位置 (397, 54) 单击鼠标左键 - 位置 (479, 117) 单击鼠标左键 - 位置 (523, 189) 45 187 103 98 228 33 397 54 479 117 523 189 上面的函数显示 a[i] 和 b[i] 像这样,但我需要 a[i] ={45,103,228,397,479,523} 和 b[i]={187,98,33,54,117,189} ,两者是否相同?
    • 当我将上述函数调用为 void 坐标(int a[6],int b[6]);在 main 函数中它不起作用但是当我像这样使用 a[i] ={45,103,228,397,479,523} 和 b[i]={187,98,33,54,117,189} 然后代码工作
    • 是一样的,只是我先写数组a,然后写数组b。 0 索引 a 和 b,1 索引 a 和 be 等等...
    • 当我将上述函数调用为 void 坐标(int a[6],int b[6]);在坐标(a,b)数据数据3(6)中的主函数中; for (int i=0; i
    • 坐标是否有可能没有像 a[i] ={45,103,228,397,479,523} 和 b[i]={187,98,33,54,117,189} 那样存储在数组中,只能这样显示?跨度>
    【解决方案3】:

    上面的函数显示 a[i] 和 b[i] 像这样- 单击鼠标左键 - 位置 (45, 187) 单击鼠标左键 - 位置 (103, 98) 单击鼠标左键 - 位置 (228, 33) 单击鼠标左键 - 位置 (397, 54) 单击鼠标左键 - 位置 (479, 117) 单击鼠标左键 - 位置 (523, 189) 45 187 103 98 228 33 397 54 479 117 523 189
    但我需要 a[i] ={45,103,228,397,479,523} 和 b[i]={187,98,33,54,117,189} ,两种模式是否相同?

    【讨论】:

      猜你喜欢
      • 2020-10-05
      • 2018-04-13
      • 1970-01-01
      • 2019-03-24
      • 2022-01-27
      • 1970-01-01
      • 2018-12-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多