[英]How to convert a OpenCV 2D matrix into a 1D array in C++?
I have some difficulties converting a Mat OpenCV matrix (2D) into a 1D array in OpenCV.我在将 Mat OpenCV 矩阵(2D)转换为 OpenCV 中的一维数组时遇到了一些困难。 I am implementing my code in C++ with Visual Studio and my environment is Windows 10. This is my code我正在使用 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;
}
This code was unsuccessful.此代码不成功。 I am not able to print a reasonable data from the array.我无法从数组中打印出合理的数据。 This is a kind of thing I get when I run the code: 1Darray: 134742016
.这是我在运行代码时得到的一种情况: 1Darray: 134742016
。 This suppose to be the intensity of a pixel (something between 0 and 255).这假设是像素的强度(介于 0 和 255 之间)。
I also replaced int* testData1D = (int*)frame.data;
我还替换了int* testData1D = (int*)frame.data;
with the below code in order to convert the matrix content one by one into the array:使用以下代码将矩阵内容一一转换为数组:
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;
}
}
But I end up with an infinite for loop that never ends.但我最终得到了一个永远不会结束的无限 for 循环。 (The program runs for ever) I checked bunch of other codes on Stackoverflow such as c++ OpenCV Turn a Mat into a 1 Dimensional Array and Convert Mat to Array/Vector in OpenCV (程序永远运行)我在 Stackoverflow 上检查了一堆其他代码,例如c++ OpenCV 将垫子变成一维数组并在 Z5BD4C87976F48E6A53919D53E 中将垫子转换为数组/向量
but they are not helpful.但它们没有帮助。 For the latter, the array size is not correct.对于后者,数组大小不正确。 I don't know if it really build the right array.我不知道它是否真的构建了正确的数组。 I get array lenght: 124236, but it should be 204*203 = 41412 I would appreciate it if you show me how I can simply convert a Mat openCV matrix into a normal 1D array in C++.我得到数组长度:124236,但它应该是 204*203 = 41412 如果你告诉我如何简单地将 Mat openCV 矩阵转换为 C++ 中的普通一维数组,我将不胜感激。
Thanks.谢谢。
Try looping through the matrix using pointer arithmetic.尝试使用指针算法循环遍历矩阵。 First, we create a random BGR
matrix of size 9
, to test the procedure.首先,我们创建一个大小为9
的随机BGR
矩阵来测试该过程。 The data type stored in the mat
are BGR
pixels represented as cv::Scalar
s:存储在mat
中的数据类型是BGR
像素,表示为cv::Scalar
s:
//Create random test mat:
cv::Mat3b randomMat(3,3);
cv::randu( randomMat, cv::Scalar(0,0,0), cv::Scalar(256,256,256) );
cv::Mat testMat = randomMat;
//Get mat total elements:
int matElements = testMat.cols * testMat.rows;
//Prepare output array:
cv::Scalar matArray[ matElements ];
This is the test matrix:这是测试矩阵:
The raw values will be stored in the matArray
container.原始值将存储在matArray
容器中。 Loop through the matrix using pointer arithmetic and store each pixel in the array:使用指针算法循环遍历矩阵并将每个像素存储在数组中:
//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++;
}
We can check the data stored in the array like this:我们可以像这样检查存储在数组中的数据:
for ( int i = 0; i < matElements; i++ ){
std::cout<<"i: "<<i<<" - "<<matArray[i]<<std::endl;
}
This yields:这产生:
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]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.