Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Question regarding std::Vector....

Question regarding std::Vector....

Scheduled Pinned Locked Moved C / C++ / MFC
graphicshelpquestion
6 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    Raza5680
    wrote on last edited by
    #1

    Hi, I am working with images and computing radon transform for an object. the function that computes the transform accepts vector as input:

    pixel p;
    vector segObj //segObj is the object segmneted from the image

    pixel is a user-defined struct defined as:

    struct pixel
    {
    float x, y; //x,y coordinates of the pixel
    };

    Currently, I am doing an element wise assignment to the vector:

    for(int ix=0; ix < element_count; ix++)
    {
    {
    f.x = xCoordArray[ix];
    f.y = yCoordArray[ix];
    segObj.push_back(f);
    }
    }

    //xCoordArray and yCoordArray are computed separately The for loop makes it slow when dealing with large images. Is there a way to assign xCoordArray and yCoordArray directly to

    vectorsegObj

    I am not expereinced with the use of vectors so any help would be appreciated Thanks

    A C 2 Replies Last reply
    0
    • R Raza5680

      Hi, I am working with images and computing radon transform for an object. the function that computes the transform accepts vector as input:

      pixel p;
      vector segObj //segObj is the object segmneted from the image

      pixel is a user-defined struct defined as:

      struct pixel
      {
      float x, y; //x,y coordinates of the pixel
      };

      Currently, I am doing an element wise assignment to the vector:

      for(int ix=0; ix < element_count; ix++)
      {
      {
      f.x = xCoordArray[ix];
      f.y = yCoordArray[ix];
      segObj.push_back(f);
      }
      }

      //xCoordArray and yCoordArray are computed separately The for loop makes it slow when dealing with large images. Is there a way to assign xCoordArray and yCoordArray directly to

      vectorsegObj

      I am not expereinced with the use of vectors so any help would be appreciated Thanks

      A Offline
      A Offline
      Andre Kraak
      wrote on last edited by
      #2

      You might try using reserve[^] before your loop. This will allocate the memory in one go for the vector and prevent the reallocation of memory every time you call push_back. So try adding

      segObj.reserve( element_count );

      before the for loop.

      0100000101101110011001000111001011101001

      R 1 Reply Last reply
      0
      • A Andre Kraak

        You might try using reserve[^] before your loop. This will allocate the memory in one go for the vector and prevent the reallocation of memory every time you call push_back. So try adding

        segObj.reserve( element_count );

        before the for loop.

        0100000101101110011001000111001011101001

        R Offline
        R Offline
        Raza5680
        wrote on last edited by
        #3

        There is a little improvement but for cases with more coordiante values ( > 2000) it is still slow. I am trying to get rid of the for loop, since at each index there a two values (pixel coordinates) using .assign is a bit tricky for me thanks

        _ 1 Reply Last reply
        0
        • R Raza5680

          There is a little improvement but for cases with more coordiante values ( > 2000) it is still slow. I am trying to get rid of the for loop, since at each index there a two values (pixel coordinates) using .assign is a bit tricky for me thanks

          _ Offline
          _ Offline
          _Superman_
          wrote on last edited by
          #4

          If speed is a concern to you, you must use xCoordArray and yCoordArray as is without using the vector.

          «_Superman_»  _I love work. It gives me something to do between weekends.

          _Microsoft MVP (Visual C++)

          Polymorphism in C

          R 1 Reply Last reply
          0
          • _ _Superman_

            If speed is a concern to you, you must use xCoordArray and yCoordArray as is without using the vector.

            «_Superman_»  _I love work. It gives me something to do between weekends.

            _Microsoft MVP (Visual C++)

            Polymorphism in C

            R Offline
            R Offline
            Raza5680
            wrote on last edited by
            #5

            Thanks for the reply. The issue is the function I am using accpets vector as input and re-writing it is not possible at the moment.

            1 Reply Last reply
            0
            • R Raza5680

              Hi, I am working with images and computing radon transform for an object. the function that computes the transform accepts vector as input:

              pixel p;
              vector segObj //segObj is the object segmneted from the image

              pixel is a user-defined struct defined as:

              struct pixel
              {
              float x, y; //x,y coordinates of the pixel
              };

              Currently, I am doing an element wise assignment to the vector:

              for(int ix=0; ix < element_count; ix++)
              {
              {
              f.x = xCoordArray[ix];
              f.y = yCoordArray[ix];
              segObj.push_back(f);
              }
              }

              //xCoordArray and yCoordArray are computed separately The for loop makes it slow when dealing with large images. Is there a way to assign xCoordArray and yCoordArray directly to

              vectorsegObj

              I am not expereinced with the use of vectors so any help would be appreciated Thanks

              C Offline
              C Offline
              Chris Losinger
              wrote on last edited by
              #6

              you could try:

              segObj.resize(element_count);
              for(int ix=0; ix < element_count; ix++)
              {
              segObj[ix].x = xCoordArray[ix];
              segObj[ix].y = yCoordArray[ix];
              }

              that eliminates the push_back, at least.

              image processing toolkits | batch image processing

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups