سؤال

I've been trying to learn about image resizing algorithms, such as nearest neighbor, bi-cubic and bi-linear interpolation algorithms. I've studied the math a bit, and I'm now looking at existing implementations to understand and appreciate how they work.

I started with this implementation of a Bi-Cubic Resize I found on Google code, which uses OpenCV, and provides test and example code. I used it as a starting point for my own implementation, which doesn't use OpenCV, but simply operates on a raw bitmap contained in an std::vector<unsigned char>:

std::vector<unsigned char> bicubicresize(const std::vector<unsigned char>& in, std::size_t src_width,
    std::size_t src_height, std::size_t dest_width, std::size_t dest_height)
{
    std::vector<unsigned char> out(dest_width * dest_height * 3);

    const float tx = float(src_width) / dest_width;
    const float ty = float(src_height) / dest_height;
    const int components = 3;
    const int bytes_per_row = src_width * components;

    const int components2 = components;
    const int bytes_per_row2 = dest_width * components;

    int a, b, c, d, index;
    unsigned char Ca, Cb, Cc;
    unsigned char C[5];
    unsigned char d0, d2, d3, a0, a1, a2, a3;

    for (int i = 0; i < dest_height; ++i)
    {
        for (int j = 0; j < dest_width; ++j)
        {
            const int x = int(tx * j);
            const int y = int(ty * i);
            const float dx = tx * j - x;
            const float dy = ty * i - y;

            index = y * bytes_per_row + x * components;
            a = y * bytes_per_row + (x + 1) * components;
            b = (y + 1) * bytes_per_row + x * components;
            c = (y + 1) * bytes_per_row + (x + 1) * components;

            for (int k = 0; k < 3; ++k)
            {
                for (int jj = 0; jj <= 3; ++jj)
                {
                    d0 = in[(y - 1 + jj) * bytes_per_row + (x - 1) * components + k] - in[(y - 1 + jj) * bytes_per_row + (x) * components + k];
                    d2 = in[(y - 1 + jj) * bytes_per_row + (x + 1) * components + k] - in[(y - 1 + jj) * bytes_per_row + (x) * components + k];
                    d3 = in[(y - 1 + jj) * bytes_per_row + (x + 2) * components + k] - in[(y - 1 + jj) * bytes_per_row + (x) * components + k];
                    a0 = in[(y - 1 + jj) * bytes_per_row + (x) * components + k];
                    a1 = -1.0 / 3 * d0 + d2 - 1.0 / 6 * d3;
                    a2 = 1.0 / 2 * d0 + 1.0 / 2 * d2;
                    a3 = -1.0 / 6 * d0 - 1.0 / 2 * d2 + 1.0 / 6 * d3;
                    C[jj] = a0 + a1 * dx + a2 * dx * dx + a3 * dx * dx * dx;

                    d0 = C[0] - C[1];
                    d2 = C[2] - C[1];
                    d3 = C[3] - C[1];
                    a0 = C[1];
                    a1 = -1.0 / 3 * d0 + d2 -1.0 / 6 * d3;
                    a2 = 1.0 / 2 * d0 + 1.0 / 2 * d2;
                    a3 = -1.0 / 6 * d0 - 1.0 / 2 * d2 + 1.0 / 6 * d3;
                    Cc = a0 + a1 * dy + a2 * dy * dy + a3* dy * dy * dy;
                    out[i * bytes_per_row2 + j * components2 + k] = Cc;
                }
            }
        }
    }

    return out;   
}



Now, as far as I can see, this implementation is fatally flawed, because of the line:

d0 = in[(y - 1 + jj) * bytes_per_row + (x - 1) * components + k] - in[(y - 1 + jj) * bytes_per_row + (x) * components + k];



It looks to me like this line will always access an out-of-bounds array index when y is 0. And y will always be 0 initially, because y is initialized with y = ty * i, and i is an iterator variable which starts at 0. So since y will always start at 0, the expression (y - 1 + jj) * bytes_per_row + (x - 1) * components + k (which is used to calculate an ARRAY INDEX) will always be negative. And... obviously, a negative array index is not valid.

Question: It looks to me like there's no possible way this code can work. Am I wrong somehow?

هل كانت مفيدة؟

المحلول

One way to do it is to define a GetPixel() function:

GetPixel(int x, int y, int channel)
{
   if (x >= 0 && x <= width-1)
      if (y >=0 && y <= height-1)
         return QueriedPixelFromImage(x,y,channel);
   return 0; // out of bounds
}

You will replace

d0 = in[(y - 1 + jj) * bytes_per_row + (x - 1) * components + k]

by

d0 = GetPixel(x-1,y-1+jj,k)

نصائح أخرى

I ran some test code to make sure I was not missing anything but as far as I can see you are indeed correct:

#include <iostream>

int main()
{

  int components = 3;

  const float tx = 200/50;
  const float ty = 200/50 ;
  int bytes_per_row = 1000 ;

  for (int i = 0; i < 5; ++i)
  {
    for (int j = 0; j < 5; ++j)
    {
         const int x = int(tx * j);
         const int y = int(ty * i);

        for (int k = 0; k < 3; ++k)
        {
            for (int jj = 0; jj <= 3; ++jj)
            {
               std::cout << "calc: " << (y - 1 + jj) * bytes_per_row + (x - 1) * components + k << std::endl ;
            }
        }
     }
   }
}

I took a look at the code on the site you provided and it does not look like a very big project. So maybe you should contact the project maintainers and see if they can provide some help. There is a contact email on the front page http://code.google.com/a/eclipselabs.org/p/bicubic-interpolation-image-processing/

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top