1

Can anybody tell me what does cvmSet actually doing in the following code. Especially L0[y*5 + x] this portion. Also, is the array declared a 1D array or 2D array? I guess it is 1D array. I just want to understand that particular line. Any help will be appreciated.

I mean if the value of array is calculated as L0[y*5 + x],so what is the role of the elements in L0, then what is the point in initializing the array elements of 1D L0?

    float L0[]={
        -1,-1,-1,-1,-1,
        0, 0, 0, 0, 0,
        2, 2, 2, 2, 2,
        0, 0, 0, 0, 0,
        -1,-1,-1,-1,-1 };

    CvMat*  rgbMat = cvCreateMat(5, 5, CV_32FC1);

    for (int y = 0; y < 5; y++)
    {
        for (int x = 0; x < 5; x++)
            cvmSet(rgbMat, y, x, L0[y*5 + x]);
    }
user2481422
  • 868
  • 3
  • 17
  • 31

2 Answers2

2

L0 is declared as 1D array of 25 elements, but it is interpreted as 2D 5x5, common thing actually. L0[y*5 + x] is used just for that, to get element of array that corresponds to [x,y] in 2D.

Alex1985
  • 658
  • 3
  • 7
  • so this means that from float L0 to end of for first for loop means before IplImage* dst = cvCreateImage(cvSize(src->width,src->height),src->depth,3); it is just creating a 2D matrix, right? – user2481422 Jul 03 '13 at 10:41
  • Yes, rgbMat is 5x5 2D matrix, created based on L0 (which seems to be some filter) – Alex1985 Jul 03 '13 at 10:45
  • can I directly declare a 2D matrix instead of using mat? – user2481422 Jul 03 '13 at 10:47
  • so what is the role of the elements in L0, I mean if the value of array is calculated as L0[y*5 + x], then what is the point in initializing the array elements of 1D L0? – user2481422 Jul 03 '13 at 11:15
  • Please, vote up the answers you like and if any solves your question accept it. It's rude to ask so many questions and not give at least some credits. Thank you. – LovaBill Jul 03 '13 at 11:57
  • @William i tried it but it's requires 15 reputation for giving a vote and currently I have only 10 :( but once I have 15 I will surely vote for you – user2481422 Jul 04 '13 at 13:36
  • @William thanks :) I voted :) your answers are really helpful – user2481422 Jul 04 '13 at 13:53
1

The line cvmSet(image,x,y,value) is explained:

set the pixel x,y in image with value.

The value is derived for the array L0. E.g. for pixel (x,y)=(1,2):

value= L0[y*5 + x] = L0[2*5+1] = L0[11] = 2.

LovaBill
  • 5,107
  • 1
  • 24
  • 32
  • so what is the role of the elements in L0, I mean if the value of array is calculated as L0[y*5 + x], then what is the point in initializing the array elements of 1D L0? – user2481422 Jul 03 '13 at 11:16
  • It's a mapping. X,Y coordinates are mapped into L0. It's a non-technical question and you'll find an answer in the theory. – LovaBill Jul 03 '13 at 11:23
  • Check [this](http://stackoverflow.com/questions/17364987/knowing-the-value-of-an-mat-element-opencv/17365489#17365489) thread. – LovaBill Jul 04 '13 at 14:07