简体   繁体   English

如何在openCV中获得像素矩阵和重塑矩阵

[英]How to get a matrix of pixels and reshape matrix in openCV

I am implementing an image processing algorithm in C++ using openCV, in which the 1st step requires that the image be converted to a matrix. 我正在使用openCV在C ++中实现一个图像处理算法,其中第一步要求将图像转换为矩阵。 I know that when an image is loaded into openCV, it is already stored as a matrix. 我知道当图像加载到openCV中时,它已经存储为矩阵。 The image that I am using is of size 80 x 60, so I am assuming that the matrix it is stored in is of size 80 x 60. However, I would like to first be able to see this matrix and then be able to reshape it into a matrix with the same no. 我使用的图像大小为80 x 60,所以我假设它存储的矩阵大小为80 x 60.但是,我想首先能够看到这个矩阵然后能够重塑它变成了一个相同的矩阵。 of pixels but as one long column instead. 像素,但作为一个长列。 ie the 80 x 60 matrix would now become a 4800 x 1 matrix. 即80×60矩阵现在将成为4800×1矩阵。 I have tried researching textbooks and online but to no avail. 我曾尝试过研究教科书和在线但无济于事。 This is my code so far. 到目前为止这是我的代码。 In any case, it is not working because I cannot cannot convert from 'IplImage *' to 'CvMat * but how else I am supposed to assign my pixel values to the matrix after creating it? 在任何情况下,它都无法正常工作,因为我无法将'IplImage *'转换为'CvMat *但是我应该如何在创建后将像素值分配给矩阵? Please, I would greatly appreciate if someone could help me with this code. 如果有人可以帮我这个代码,我将不胜感激。

#include "cv.h"
#include "highgui.h"
#include "iostream"

using namespace std;
int main( int argc, char* argv ) {
IplImage* img0 = NULL;
CvMat* img0_mat = NULL ;
img0 = cvLoadImage("C:\\new\\walk mii.jpg");
if (!img0){
    return -1;}
img0_mat = cvCreateMat(80, 60, 1);
img0_mat = img0;
cout <<" matrix " << img0 << endl;

cvWaitKey(0);
return 0;
}

You could call Mat::reshape(int cn, int rows=0) : 你可以调用Mat::reshape(int cn, int rows=0)

The method makes a new matrix header for *this elements. 该方法为* this元素创建一个新的矩阵头。 The new matrix may have different size and/or different number of channels. 新矩阵可以具有不同的大小和/或不同数量的通道。 Any combination is possible, as long as: 任何组合都是可能的,只要:

1) No extra elements is included into the new matrix and no elements are excluded. 1)新矩阵中不包含额外元素,不排除任何元素。 Consequently, the product 因此,产品

2) rows*cols*channels() must stay the same after the transformation. 2)rows * cols * channels()必须在转换后保持不变。

No data is copied, ie this is O(1) operation. 没有数据被复制,即这是O(1)操作。 Consequently, if you change the number of rows, or the operation changes elements' row indices in some other way, the matrix must be continuous. 因此,如果更改行数,或者操作以其他方式更改元素的行索引,则矩阵必须是连续的。 See Mat::isContinuous() . 见Mat :: isContinuous()。

...it looks like you're using an older version of the library though so you want cvReshape . ...看起来你正在使用旧版本的库,所以你想要cvReshape Something like this should work: 这样的事情应该有效:

#include "cv.h" 
#include "highgui.h" 
#include "iostream" 
using namespace std; 

int main( int argc, char* argv ) 
{ 
    IplImage* img0 = NULL; 
    CvMat* img0_mat = NULL ; 
    img0 = cvLoadImage("C:\\new\\walk mii.jpg"); 
    img0_mat = cvCreateMat(80, 60, 1); 

    CvMat row_header, *row;
    row = cvReshape( img0_mat, &row_header, 0, 1 );

    cout << " matrix " << row->tostring() << endl; 

    cvWaitKey(0); 
    return 0;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM