#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2011
    Posts
    23
    Rep Power
    0

    Access to array (C++)


    Hi,
    I am just starting to learn C++ and I need to ask you. Is it possible to create array or object where I could create element under index starting not from zero but e.g. 25?

    Situation which I want to describe. I have image with white background. First row contains few pixels in black color. There are pixels (x coord) 23, 24 and 25 are black. There can be also some other black areas, but let's suppose 26 is white. According formula Iyn=[x1,x2] I want to get the lower value (x1) of the limit and the upper value (x2). So I want to make method to get ( in this particular case the limit Iyn=[23,25] ) .

    (Illustration of the black pixels in the image row 1-5, up to column 26; the grid illustrates the positions of the pixels; the pixels are the big black blots)




    I would like to do it like so:

    I plan to read image row by row (or column by column). For every row I would like to use for loop to analyse whether the previous pixel has same color (*) - if yes, then set a value in array. However to have efficient performance I need this direct access. So if I analyse row=1 and found that pixel with coord x=25 is same color like the previous pixel (x=24), later I wound need to access it directly by value 25. If I found that the next pixel does not contain same color so I would save the lower limit x1 into the array myarr[y][x1]=x2.

    I know that C++ makes arrays starting from index 0 and probably this is not possible to do by array, but maybe some kind of object? The reason why I want to do so is that later, when I would want to use the array to analyse the groups (black lines), I would need to compare pixel from previous row and group (e.g. first row and first black line) with the actual row (e.g. second row and first line in this row). In the case that they are same color, I would simply join the one group with the second group (So I would found that these two lines from different rows are common area, black shape or rectangle). When doing this comparison it would be much more faster to make condition testing access to certain element like myarr[1][23] ... if true, then ok, these lines are together. If this element does not exist, than it means, that there is different color...

    I tried to explain this as simple as possible, I hope you can understand what I try to do.

    * Notes - I can make method to compare with a fuzz, so the goal for comparison method would be to find similar color, which has certain fuzz. But for simplicity of this example, lets use just two colors.
  2. #2
  3. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,143
    Rep Power
    2222
    Yes. Create a class. It would contain a standard zero-base array. It would also overload the [] operator which would take the 25-to-whatever index that you're working in and translate it to the zero-base index to access the actual array. I believe that you should be able to find many examples floating about cyber-space.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2011
    Posts
    23
    Rep Power
    0

    Thumbs up


    Yet one more question regarding moving of data from one array to another subarray.

    Lets suppose I have one group of pixels (as a result) saved in array:

    Code:
            // 1 - y row    0 - line number
    resultsArr[1][groupNum][0][x1]={
                                   x2, // this is the upper limit of the black line
                                   byteArray // keeps color in BGR
                                   }
    And I want them to move the elements (delete and add)
    under

    Code:
    resultsArr[1][groupNum][0][0]
    to the following sub array:

    Code:
            // 1 - y row    0 - line number
    resultsArr[1][groupNum][0][1]={
                                  x2,
                                  byteArray
                                  }
    which way to copy and delete should I use?

    To copy should I use
    resultsArr[1][groupNum][0][1]=resultsArr[1][groupNum][1][0]

    or some sophisticated method to copy objects before I delete the original? Here I am not sure if should I use handler or not when I could want to copy the array.

    And the, should I delete the original like this

    resultsArr[1][groupNum][0][0]=null
    or this
    resultsArr[1][groupNum][0][0]=0
    or
    delete resultsArr[1][groupNum][0][0]
    ?

    As during declaration of multidimensional array, Do I must declare type for every level of array?

IMN logo majestic logo threadwatch logo seochat tools logo