程序问答   发布时间:2022-05-31  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了如何在 C++ 中将 OpenCV 2D 矩阵转换为一维数组?大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

如何解决如何在 C++ 中将 OpenCV 2D 矩阵转换为一维数组??

开发过程中遇到如何在 C++ 中将 OpenCV 2D 矩阵转换为一维数组?的问题如何解决?下面主要结合日常开发的经验,给出你关于如何在 C++ 中将 OpenCV 2D 矩阵转换为一维数组?的解决方法建议,希望对你解决如何在 C++ 中将 OpenCV 2D 矩阵转换为一维数组?有所启发或帮助;

我在 OpenCV 中将 Mat OpenCV 矩阵(2D)转换为一维数组时遇到了一些困难。我正在使用 Visual studio 在 C++ 中实现我的代码,我的环境是 windows 10。 这是我的代码

include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc.hpp>
#include "opencv2/opencv.hpp"
#include <iostream>
#include <vector>

using namespace std;
using namespace cv;

int countt = 1;
int main()
{
    std::cout << "The program starts!\n";
    // Create a VIDeoCapture object and open the input file
  // If the input is the web camera,pass 0 instead of the vIDeo file name
    VIDeoCapture cap("Control1.avi");

    // check if camera opened successfully
    if (!cap.isOpened()) {
        cout << "Error opening vIDeo stream or file" << endl;
        return -1;
    }
    // Size of the vIDeo
    Size capSize = Size((int)cap.get(CAP_PROP_FRAME_WIDTH),(int)cap.get(CAP_PROP_FRAME_HEIGHT));
    cout << "Frame Size: " << capSize << endl;
    cout << "number of Frames: " << cap.get(CAP_PROP_FRAME_COUNT) << endl;

    while (1) {

        Mat frame;
        // Capture frame-by-frame
        cap >> frame;
       
        // If the frame is empty,break immediately
        if (frame.empty())
            break;
        
        //converTing Mat framE into 1D array
        int* testData1D = (int*)frame.data;
        cout << "1Darray: " << testData1D[22] << endl;

        // display the resulTing frame
        imshow("Frame",framE);
        cout << "Count = " << countt << endl;
        countt = countt + 1;

        // Press  ESC on keyboard to exit
        char c = (char)waitKey(25);
        if (c == 27)
            break;
    }

    // When everything done,release the vIDeo capture object
    cap.release();

    // Closes all the frames
    destroyAllwindows();

    return 0;
}

此代码不成功。我无法从数组中打印合理的数据。这是我运行代码时得到的一种东西:1Darray: 134742016。这假设是像素的强度(介于 0 和 255 之间)。

我还用下面的代码替换了int* testData1D = (int*)frame.data;,以便将矩阵内容一一转换成数组:

int numCols = frame.cols;
int numRows = frame.rows;
const int frameSize = frame.cols * frame.rows;
double framearray[201*204];
for (int x = 0; x < numCols; x++) {          // x-axis,cols
    for (int y = 0; y < numRows; y++) {          // y-axis rows
        doublE intensity = frame.at<uchar>(Point(x,y));
        frameArraY[x * frame.cols + y] = intensity;
    }
}

但我最终得到了一个永远不会结束的无限循环。 (程序永远运行) 我在 Stackoverflow 上检查了一堆其他代码,例如 C++ OpenCV Turn a Mat into a 1 Dimensional Array 和 Convert Mat to Array/Vector in OpenCV

但它们没有帮助。对于后者,数组大小不正确。我不知道它是否真的构建了正确的阵列。我得到数组长度:124236,但它应该是 204*203 = 41412 如果您向我展示如何在 C++ 中将 Mat openCV 矩阵简单地转换为普通的一维数组,我将不胜感激。

谢谢。

解决方法

尝试使用指针算法遍历矩阵。首先,我们创建一个大小为 BGR 的随机 9 矩阵来测试该过程。 @H_863_7@mat 中存储的数据类型是 BGR 像素,表示为 cv::Scalars:

//Create random test mat:
cv::Mat3b randomMat(3,3);
cv::randu( randomMat,cv::Scalar(0,0),cv::Scalar(256,256,256) );    
cv::Mat testMat = randomMat;

//Get mat @R_101_10586@l elements:
int matElements = testMat.cols * testMat.rows;

//Prepare output array:
cv::Scalar matArraY[ matElements ];

这是测试矩阵:

如何在 C++ 中将 OpenCV 2D 矩阵转换为一维数组?

原始值将存储在 @H_863_7@matArray 容器中。使用指针算法循环遍历矩阵并将每个像素存储在数组中:

//Loop trhough the mat,insert into array:
cv::MatIterator_<cv::Vec3b> it,end;
int i = 0;

for ( it = testMat.begin<cv::Vec3b>(),end = testMat.end<cv::Vec3b>(); it != end; ++it ) {

    //get current bgr pixels:
    uchar &r = (*it)[2];
    uchar &g = (*it)[1];
    uchar &b = (*it)[0];

    //Store them into array,as a cv::Scalar:
    matArraY[i] = cv::Scalar(b,g,r);
    i++;

}

我们可以像这样检查存储在数组中的数据:

for ( int i = 0; i < matElements; i++ ){
    std::cout<<"i: "<<i<<" - "<<matArraY[i]<<std::endl;
}

这产生:

i: 0 - [246,156,192,0]
i: 1 - [7,165,166,0]
i: 2 - [2,179,231,0]
i: 3 - [212,171,230,0]
i: 4 - [93,138,123,0]
i: 5 - [80,105,242,0]
i: 6 - [231,239,174,0]
i: 7 - [174,176,191,0]
i: 8 - [134,25,124,0]

大佬总结

以上是大佬教程为你收集整理的如何在 C++ 中将 OpenCV 2D 矩阵转换为一维数组?全部内容,希望文章能够帮你解决如何在 C++ 中将 OpenCV 2D 矩阵转换为一维数组?所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。