我是OpenCV的新手。最近,我发现OpenCV函数从Mat转换为Array很麻烦。我使用OpenCV API中提供的.ptr和.at方法进行了研究,但是我无法获得正确的数据。我希望从Mat到Array直接转换(如果可用,如果没有,则转换为Vector)。我需要OpenCV函数,因为代码必须在Vivado HLS中进行高级综合。请帮忙。
答案 0 :(得分:75)
如果Mat mat
的内存是连续的(其所有数据都是连续的),您可以直接将其数据转换为一维数组:
std::vector<uchar> array(mat.rows*mat.cols);
if (mat.isContinuous())
array = mat.data;
否则,您必须逐行获取其数据,例如到2D数组:
uchar **array = new uchar*[mat.rows];
for (int i=0; i<mat.rows; ++i)
array[i] = new uchar[mat.cols];
for (int i=0; i<mat.rows; ++i)
array[i] = mat.ptr<uchar>(i);
更新:如果你使用的是std::vector
会更容易,你可以这样做:
std::vector<uchar> array;
if (mat.isContinuous()) {
// array.assign(mat.datastart, mat.dataend); // <- has problems for sub-matrix like mat = big_mat.row(i)
array.assign(mat.data, mat.data + mat.total());
} else {
for (int i = 0; i < mat.rows; ++i) {
array.insert(array.end(), mat.ptr<uchar>(i), mat.ptr<uchar>(i)+mat.cols);
}
}
p.s:对于其他类型的cv::Mat
,例如CV_32F
,你应该这样做:
std::vector<float> array;
if (mat.isContinuous()) {
// array.assign((float*)mat.datastart, (float*)mat.dataend); // <- has problems for sub-matrix like mat = big_mat.row(i)
array.assign((float*)mat.data, (float*)mat.data + mat.total());
} else {
for (int i = 0; i < mat.rows; ++i) {
array.insert(array.end(), mat.ptr<float>(i), mat.ptr<float>(i)+mat.cols);
}
}
答案 1 :(得分:11)
这是另一种可能的解决方案,假设矩阵有一列(您可以通过reshape将原始Mat重新整形为一列Mat):
Mat matrix= Mat::zeros(20, 1, CV_32FC1);
vector<float> vec;
matrix.col(0).copyTo(vec);
答案 2 :(得分:3)
您可以将其直接放入数组,而不是逐行获取图像。对于 CV_8U 类型的图像,您可以使用字节数组,对于其他类型,请检查here。
Mat img; // Should be CV_8U for using byte[]
int size = (int)img.total() * img.channels();
byte[] data = new byte[size];
img.get(0, 0, data); // Gets all pixels
答案 3 :(得分:3)
此处提供的示例均不适用于通用情况,即N维矩阵。任何使用&#34;行&#34;假定只有列和行,4维矩阵可能有更多。
下面是一些将非连续N维矩阵复制到连续内存流中的示例代码 - 然后将其转换回Cv :: Mat
#include <iostream>
#include <cstdint>
#include <cstring>
#include <opencv2/opencv.hpp>
int main(int argc, char**argv)
{
if ( argc != 2 )
{
std::cerr << "Usage: " << argv[0] << " <Image_Path>\n";
return -1;
}
cv::Mat origSource = cv::imread(argv[1],1);
if (!origSource.data) {
std::cerr << "Can't read image";
return -1;
}
// this will select a subsection of the original source image - WITHOUT copying the data
// (the header will point to a region of interest, adjusting data pointers and row step sizes)
cv::Mat sourceMat = origSource(cv::Range(origSource.size[0]/4,(3*origSource.size[0])/4),cv::Range(origSource.size[1]/4,(3*origSource.size[1])/4));
// correctly copy the contents of an N dimensional cv::Mat
// works just as fast as copying a 2D mat, but has much more difficult to read code :)
// see http://stackoverflow.com/questions/18882242/how-do-i-get-the-size-of-a-multi-dimensional-cvmat-mat-or-matnd
// copy this code in your own cvMat_To_Char_Array() function which really OpenCV should provide somehow...
// keep in mind that even Mat::clone() aligns each row at a 4 byte boundary, so uneven sized images always have stepgaps
size_t totalsize = sourceMat.step[sourceMat.dims-1];
const size_t rowsize = sourceMat.step[sourceMat.dims-1] * sourceMat.size[sourceMat.dims-1];
size_t coordinates[sourceMat.dims-1] = {0};
std::cout << "Image dimensions: ";
for (int t=0;t<sourceMat.dims;t++)
{
// calculate total size of multi dimensional matrix by multiplying dimensions
totalsize*=sourceMat.size[t];
std::cout << (t>0?" X ":"") << sourceMat.size[t];
}
// Allocate destination image buffer
uint8_t * imagebuffer = new uint8_t[totalsize];
size_t srcptr=0,dptr=0;
std::cout << std::endl;
std::cout << "One pixel in image has " << sourceMat.step[sourceMat.dims-1] << " bytes" <<std::endl;
std::cout << "Copying data in blocks of " << rowsize << " bytes" << std::endl ;
std::cout << "Total size is " << totalsize << " bytes" << std::endl;
while (dptr<totalsize) {
// we copy entire rows at once, so lowest iterator is always [dims-2]
// this is legal since OpenCV does not use 1 dimensional matrices internally (a 1D matrix is a 2d matrix with only 1 row)
std::memcpy(&imagebuffer[dptr],&(((uint8_t*)sourceMat.data)[srcptr]),rowsize);
// destination matrix has no gaps so rows follow each other directly
dptr += rowsize;
// src matrix can have gaps so we need to calculate the address of the start of the next row the hard way
// see *brief* text in opencv2/core/mat.hpp for address calculation
coordinates[sourceMat.dims-2]++;
srcptr = 0;
for (int t=sourceMat.dims-2;t>=0;t--) {
if (coordinates[t]>=sourceMat.size[t]) {
if (t==0) break;
coordinates[t]=0;
coordinates[t-1]++;
}
srcptr += sourceMat.step[t]*coordinates[t];
}
}
// this constructor assumes that imagebuffer is gap-less (if not, a complete array of step sizes must be given, too)
cv::Mat destination=cv::Mat(sourceMat.dims, sourceMat.size, sourceMat.type(), (void*)imagebuffer);
// and just to proof that sourceImage points to the same memory as origSource, we strike it through
cv::line(sourceMat,cv::Point(0,0),cv::Point(sourceMat.size[1],sourceMat.size[0]),CV_RGB(255,0,0),3);
cv::imshow("original image",origSource);
cv::imshow("partial image",sourceMat);
cv::imshow("copied image",destination);
while (cv::waitKey(60)!='q');
}
答案 4 :(得分:1)
byte * matToBytes(Mat image)
{
int size = image.total() * image.elemSize();
byte * bytes = new byte[size]; //delete[] later
std::memcpy(bytes,image.data,size * sizeof(byte));
}
答案 5 :(得分:1)
您可以使用迭代器:
Mat matrix = ...;
std::vector<float> vec(matrix.begin<float>(), matrix.end<float>());
答案 6 :(得分:0)
cv::Mat m;
m.create(10, 10, CV_32FC3);
float *array = (float *)malloc( 3*sizeof(float)*10*10 );
cv::MatConstIterator_<cv::Vec3f> it = m.begin<cv::Vec3f>();
for (unsigned i = 0; it != m.end<cv::Vec3f>(); it++ ) {
for ( unsigned j = 0; j < 3; j++ ) {
*(array + i ) = (*it)[j];
i++;
}
}
Now you have a float array. In case of 8 bit, simply change float to uchar and Vec3f to Vec3b and CV_32FC3 to CV_8UC3
答案 7 :(得分:0)
uchar * arr = image.isContinuous()? image.data: image.clone().data;
uint length = image.total()*image.channels();
cv::Mat flat = image.reshape(1, image.total()*image.channels());
std::vector<uchar> vec = image.isContinuous()? flat : flat.clone();
两者均可为任何普通cv::Mat
工作。
cv::Mat image;
image = cv::imread(argv[1], cv::IMREAD_UNCHANGED); // Read the file
cv::namedWindow("cvmat", cv::WINDOW_AUTOSIZE );// Create a window for display.
cv::imshow("cvmat", image ); // Show our image inside it.
// flatten the mat.
uint totalElements = image.total()*image.channels(); // Note: image.total() == rows*cols.
cv::Mat flat = image.reshape(1, totalElements); // 1xN mat of 1 channel, O(1) operation
if(!image.isContinuous()) {
flat = flat.clone(); // O(N),
}
// flat.data is your array pointer
auto * ptr = flat.data; // usually, its uchar*
// You have your array, its length is flat.total() [rows=1, cols=totalElements]
// Converting to vector
std::vector<uchar> vec(flat.data, flat.data + flat.total());
// Testing by reconstruction of cvMat
cv::Mat restored = cv::Mat(image.rows, image.cols, image.type(), ptr); // OR vec.data() instead of ptr
cv::namedWindow("reconstructed", cv::WINDOW_AUTOSIZE);
cv::imshow("reconstructed", restored);
cv::waitKey(0);
Mat
使用其构造函数之一创建,或者使用Mat
或类似方法复制到另一个clone()
中,则存储为连续的内存块。要转换为数组或vector
,我们需要它的第一个块的地址和数组/向量的长度。
Mat::data
是指向其内存的公共uchar指针。
但是此内存可能不是连续的。如其他答案所述,我们可以检查mat.data
是否指向连续内存或不使用mat.isContinous()
。除非您需要极高的效率,否则可以在O(N)时间中使用mat.clone()
获得连续的垫子版本。 (N =所有通道中的元素数)。但是,在处理由cv::imread()
读取的图像时,我们很少会遇到不连续的垫子。
问:row*cols*channels
应该正确吗?
答:并非总是如此。可以是rows*cols*x*y*channels
。
问:应该等于mat.total()吗?
答:适用于单通道垫。但不适用于多通道垫
由于OpenCV的文档不完善,因此数组/向量的长度有些棘手。我们有Mat::size
个公共成员,该成员仅存储单个Mat 没有渠道的尺寸。对于RGB图像,Mat.size = [rows,cols]而不是[rows,cols,channels]。 Mat.total()
返回垫子的单个通道中的元素总数,该元素等于mat.size
中值的乘积。对于RGB图像,total() = rows*cols
。因此,对于任何通用Mat,连续存储块的长度将为mat.total()*mat.channels()
。
除了数组/矢量,我们还需要原始Mat的mat.size
[array like]和mat.type()
[int]。然后,使用采用数据指针的构造函数之一,我们可以获得原始Mat。不需要可选的step参数,因为我们的数据指针指向连续内存。我使用此方法在nodejs和C ++之间将Mat作为Uint8Array传递。这样可以避免使用node-addon-api为cv :: Mat编写C ++绑定。
答案 8 :(得分:0)
如果你知道你的 img 是 3 通道,那么你可以试试这个代码
Vec3b* dados = new Vec3b[img.rows*img.cols];
for (int i = 0; i < img.rows; i++)
for(int j=0;j<img.cols; j++)
dados[3*i*img.cols+j] =img.at<Vec3b>(i,j);
如果你想检查 (i,j) vec3b 你可以写
std::cout << (Vec3b)img.at<Vec3b>(i,j) << std::endl;
std::cout << (Vec3b)dados[3*i*img.cols+j] << std::endl;