Ask Your Question
0

Adding information in the Mat

asked 2015-04-21 02:02:53 -0500

zms gravatar image

Hello, I have a Mat data = [255,0,255,0,0,0] which is a 1X6. I would like to add another information in the Mat data=[255,0,255,0,0,0,50] an additional of another info 50 in the Mat for 1X7.

Is this feasible?

edit retag flag offensive close merge delete

Comments

Mat is NxM so, you cannot have a row of 6 values and another one of 7 values. maybe adding a 0 at the end of the first row, or think of some other way to "code it"

thdrksdfthmn gravatar imagethdrksdfthmn ( 2015-04-21 02:19:54 -0500 )edit

2 answers

Sort by ยป oldest newest most voted
1

answered 2015-04-26 23:47:14 -0500

zms gravatar image

updated 2015-04-27 02:24:01 -0500

thdrksdfthmn gravatar image

@StevenPuttemans, @thdrksdfthmn, thanks for the help and ideas.. Debugged and worked. Here is the code for anybody from the forum to refer. Let me know if there are issues from my code below.

Mat newdata = Mat::zeros(1,1025,CV_32F); //change accordingly the total of value
Mat data = frame_gray.reshape(1,1); // change the frame_gray  original mat to the 1X1 matriks
         data.convertTo(data, CV_32F); 
         data.setTo(1, data>1);

       for (int g = 0; g <1024;g++)
    {
        newdata.at<float>(0,g) = data.at<float>(0,g);
    }
        newdata.at<float>(0,1024)= whitepix;  // additional information need to be added at the last place
edit flag offensive delete link more

Comments

1

You can change that for loop into something like newdata(cv::Rect(0, 0, 1, 1024)) = data; or data.copyTo(newData(cv::Rect(0, 0, 1, 1024)));; and then you add/change the value of the latest element.

thdrksdfthmn gravatar imagethdrksdfthmn ( 2015-04-27 02:28:28 -0500 )edit
1

thanks for the alternative coding :)

zms gravatar imagezms ( 2015-04-27 02:35:15 -0500 )edit
3

answered 2015-04-21 02:31:31 -0500

updated 2015-04-22 02:52:29 -0500

If you want a dynamically growing matrix, than simply use the data structures provided for that, which is called a vector. Do this as follows.

// Initiate original value
// Using the C++11 standard
vector<int> data {255,0,255,0,0,0}; 
// Using older standards
int elements[5] = {255,0,255,0,0,0};
vector<int> data(&elements[0], &elements[0]+6);
// Now add an element --> same for both standards
data.push_back(50);

EDIT: If you want to add the information in the case you supplied, you can do it maybe a bit simpler. The only thing I am wondering is why you would convert integer data to floating point type, but probably thats for further processing. Also it kind of depends how versatile you want your code to be. A simple suggestion could be.

bool bSuccess = cap.read(frame_gray, CV_LOAD_IMAGE_GRAYSCALE); //you need to specify explicitly if you want to read in grayscale!
Mat data = frame_gray.reshape(1,1);
Mat result (1, 7, CV_8UC1); //Because I am guessing that data is of this type and that you know how much elements you will add
result.at<uchar>(1,1) = data.at<uchar>(1,1);
result.at<uchar>(1,2) = data.at<uchar>(1,2);
result.at<uchar>(1,3) = data.at<uchar>(1,3);
result.at<uchar>(1,4) = data.at<uchar>(1,4);
result.at<uchar>(1,5) = data.at<uchar>(1,5);
result.at<uchar>(1,6) = data.at<uchar>(1,6);
result.at<uchar>(1,7) = 50; //Add information to the end
result.convertTo(result, CV_32F);
edit flag offensive delete link more

Comments

Hi, if my Mat is already set like this..

bool bSuccess = cap.read(frame_gray);
Mat data = frame_gray.reshape(1,1);  // shape to 6X1
data.convertTo(data, CV_32F);

should I redo all the initialization like above to do this?

and after this I wan to add one more info

zms gravatar imagezms ( 2015-04-22 00:00:00 -0500 )edit

@StevenPuttemans - I'm changing to float due to the Neural Network real time classification process. It requires flot for the classification. Actually it is not a 1X7 matrix but 1X1024 instead. I want to add 2 more information as the attributes to the neural network data. I had tried the suggestion, but still do not success. Here is my code ..

zms gravatar imagezms ( 2015-04-24 02:38:23 -0500 )edit

frame_gray = frame_gray(myROI); cv::resize(frame_gray,frame_gray,size);//resize image imshow("scaledown",frame_gray);

    Mat data(1,1024,CV_32F);
    data = frame_gray.reshape(1,1);
    data.convertTo(data, CV_32F); 
    data.setTo(1, data>1);
    //cout<<data;

    Mat data1 (1,1024, CV_8UC1); 

    for (int g = 1; g <1025;g++)
    {
        data1.at<uchar>(1,g) = data.at<uchar>(1,g);

    }

    data1.at<uchar>(1,1025) = 50; //Add information to the end
    data1.convertTo(data1, CV_32F);
zms gravatar imagezms ( 2015-04-24 02:38:54 -0500 )edit
1

@zms this won't work. Either do the adding before you convert to float and use the uchar as step size of the at function OR otherwise do a step of float. Something like:

Mat data1 (1,1024, CV_32FC1); //You only got 1024 elements o_O shouldnt this number be higher?

for (int g = 1; g <1025;g++) // A matrix is accessed from element 0 at the start, not 1, you will now not use all your values.
{
    data1.at<float>(1,g) = data.at<float>(1,g);

}
StevenPuttemans gravatar imageStevenPuttemans ( 2015-04-24 03:12:47 -0500 )edit

@StevenPuttemans, I understand the first part, but on the second one..

for (int g = 1; g <1025;g++) // A matrix is accessed from element 0 at the start, not 1, you will now not use all your values.

I'm confuse about this. since the first example above start with 1 and not 0, that's why I'm quite confuse with the matrix first coordinate .. it should be 1X1 right?

result.at<uchar>(1,1) = data.at<uchar>(1,1); result.at<uchar>(1,2) = data.at<uchar>(1,2); result.at<uchar>(1,3) = data.at<uchar>(1,3);

zms gravatar imagezms ( 2015-04-24 03:40:39 -0500 )edit
1

Could you please put some attention at writing my username correct, doesnt seem that hard :D I have been fixing it now for the 4th time :) Hmm as to your problem, my mistake. It should start at [0,0]!

StevenPuttemans gravatar imageStevenPuttemans ( 2015-04-24 03:46:25 -0500 )edit

Question Tools

1 follower

Stats

Asked: 2015-04-21 02:02:53 -0500

Seen: 2,592 times

Last updated: Apr 27 '15