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. Fastest way to scale binary images??

Fastest way to scale binary images??

Scheduled Pinned Locked Moved C / C++ / MFC
c++data-structurestutorialquestion
5 Posts 2 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.
  • P Offline
    P Offline
    pavanbabut
    wrote on last edited by
    #1

    Hi, I am looking for a fastest way to scale up/down a binary image. I may end up scaling images only in X direction. Is there any easiest/fastest way to do this in VC++?? For now my images are stored in an array (not a two-dimensional array, but just a single strip array(one row after another from the image). Since I am dealing with binary images, I hope I dont need to use interpolation. The data that I have is the original dimensions of the image and the final dimensions of the image. For example, my original image is 9x9 and my new dimensions are 15x9. thanks, -Pavan.

    C 1 Reply Last reply
    0
    • P pavanbabut

      Hi, I am looking for a fastest way to scale up/down a binary image. I may end up scaling images only in X direction. Is there any easiest/fastest way to do this in VC++?? For now my images are stored in an array (not a two-dimensional array, but just a single strip array(one row after another from the image). Since I am dealing with binary images, I hope I dont need to use interpolation. The data that I have is the original dimensions of the image and the final dimensions of the image. For example, my original image is 9x9 and my new dimensions are 15x9. thanks, -Pavan.

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

      if you're not limiting your output size to certain whole multiples of the input size, you're going to have to do some interpolation (if you want reasonable-looking output). but, bi-linear interpolation is pretty easy.

      image processing toolkits | batch image processing | blogging

      P 1 Reply Last reply
      0
      • C Chris Losinger

        if you're not limiting your output size to certain whole multiples of the input size, you're going to have to do some interpolation (if you want reasonable-looking output). but, bi-linear interpolation is pretty easy.

        image processing toolkits | batch image processing | blogging

        P Offline
        P Offline
        pavanbabut
        wrote on last edited by
        #3

        Any articles on bi-linear interpolation that is easy to understand... -Pavan

        C 1 Reply Last reply
        0
        • P pavanbabut

          Any articles on bi-linear interpolation that is easy to understand... -Pavan

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

          in 1-D, linear interpolation looks like this:

          // ratio of input size to output size
          factor1 = inputPoints / outputPoints

          // loop over output
          for (current=0 to outputPoints-1)
          {
          // center of our input pixel is here. it's probably fractional.
          inputPixelLocation = current * factor1

          // get the two pixels on either side of that center point
          input1 = pixel[floor(inputPixelLocation)]
          input2 = pixel[floor(inputPixelLocation) + 1]

          // the fractional part of this tells us how close we are to the left-most source pixel.
          factor2 = fractional_part_of(inputPixelLocation)

          // average of the two pixels, weighted by factor2
          output = input1 * factor2 + input2 * (1.0 - factor2)
          }

          in 2D, you pick four pixels: two from the current pixel row (as above) and another two from the row immediately beneath. and then you combine all four with the a two-factor weighted average.

          factor1X = inputPointsX / outputPointsX
          factor1Y = inputPointsY / outputPointsY

          // vertical loop
          for (currentY=0 to outputPointsY-1)
          {
          // find the vertical factor
          inputPixelLocationY = currentY * factor1Y

          // horizontal loop
          for (currentX=0 to outputPointsX-1)
          {
          // find horizontal factor
          inputPixelLocationX = currentX * factor1X

          floorX = floor(inputPixelLocationX)
          floorY = floor(inputPixelLocationY)
          
          // get four pixels: X,Y - X+1,Y - X,Y+1, X+1,Y+1
          input1 = pixel\[floorX \]\[floorY\]
          input2 = pixel\[floorX + 1\]\[floorY\]
          input3 = pixel\[floorX\]\[floorY + 1\]
          input4 = pixel\[floorX + 1\]\[floorY + 1\]
          
          // two factors
          factor2X = fractional\_part\_of(inputPixelLocationX) 
          factor2Y = fractional\_part\_of(inputPixelLocationY) 
          
          factor2X\_inverse = 1.0 - factor2X 
          factor2Y\_inverse = 1.0 - factor2Y 
          
          // average of the four pixels, weighted by the 2 factor2's
          outval = input1 \* factor2X \* factor2Y + 
                   input2 \* factor2X\_inverse \* factor2Y + 
                   input3 \* factor2X \* factor2Y\_inverse + 
                   input4 \* factor2X\_inverse \* factor2Y\_inverse
          
          output\[currentX\]\[currentY\] = outval
          

          }
          }

          that's a basic bi-lin algorithm. there's a lot of room there for optimization in general, and even more for monochrome data.

          image processing toolkits | batch image processing | blogging

          P 1 Reply Last reply
          0
          • C Chris Losinger

            in 1-D, linear interpolation looks like this:

            // ratio of input size to output size
            factor1 = inputPoints / outputPoints

            // loop over output
            for (current=0 to outputPoints-1)
            {
            // center of our input pixel is here. it's probably fractional.
            inputPixelLocation = current * factor1

            // get the two pixels on either side of that center point
            input1 = pixel[floor(inputPixelLocation)]
            input2 = pixel[floor(inputPixelLocation) + 1]

            // the fractional part of this tells us how close we are to the left-most source pixel.
            factor2 = fractional_part_of(inputPixelLocation)

            // average of the two pixels, weighted by factor2
            output = input1 * factor2 + input2 * (1.0 - factor2)
            }

            in 2D, you pick four pixels: two from the current pixel row (as above) and another two from the row immediately beneath. and then you combine all four with the a two-factor weighted average.

            factor1X = inputPointsX / outputPointsX
            factor1Y = inputPointsY / outputPointsY

            // vertical loop
            for (currentY=0 to outputPointsY-1)
            {
            // find the vertical factor
            inputPixelLocationY = currentY * factor1Y

            // horizontal loop
            for (currentX=0 to outputPointsX-1)
            {
            // find horizontal factor
            inputPixelLocationX = currentX * factor1X

            floorX = floor(inputPixelLocationX)
            floorY = floor(inputPixelLocationY)
            
            // get four pixels: X,Y - X+1,Y - X,Y+1, X+1,Y+1
            input1 = pixel\[floorX \]\[floorY\]
            input2 = pixel\[floorX + 1\]\[floorY\]
            input3 = pixel\[floorX\]\[floorY + 1\]
            input4 = pixel\[floorX + 1\]\[floorY + 1\]
            
            // two factors
            factor2X = fractional\_part\_of(inputPixelLocationX) 
            factor2Y = fractional\_part\_of(inputPixelLocationY) 
            
            factor2X\_inverse = 1.0 - factor2X 
            factor2Y\_inverse = 1.0 - factor2Y 
            
            // average of the four pixels, weighted by the 2 factor2's
            outval = input1 \* factor2X \* factor2Y + 
                     input2 \* factor2X\_inverse \* factor2Y + 
                     input3 \* factor2X \* factor2Y\_inverse + 
                     input4 \* factor2X\_inverse \* factor2Y\_inverse
            
            output\[currentX\]\[currentY\] = outval
            

            }
            }

            that's a basic bi-lin algorithm. there's a lot of room there for optimization in general, and even more for monochrome data.

            image processing toolkits | batch image processing | blogging

            P Offline
            P Offline
            pavanbabut
            wrote on last edited by
            #5

            Thanks for the info... will try to implement this as easy as I can.. -Pavan.

            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