2

Hi i need to scale a gray scale image fast, so i tried vImage and the app is crashing, please help. In the below code srcimg.data/dstimg.data is a point to unsigned char image data(single channel only gray data).

                    vImage_Buffer src;
                    src.data=srcimg.data;
                    src.height=srcimg.cols;
                    src.width=srcimg.rows;
                    src.rowBytes=srcimg.cols;

                    vImage_Buffer dest;
                    dest.data=dstimg.data;
                    dest.height=dstimg.cols;
                    dest.width=dstimg.rows;
                    dest.rowBytes=dstimg.cols;
                    vImageScale_Planar8(&src, &dest, NULL, kvImageNoFlags);

Stephen Canon
  • 103,815
  • 19
  • 183
  • 269
pradeepa
  • 4,104
  • 5
  • 31
  • 41
  • srcimg and dstimg are cv::Mat objects from opencv, if i replace the above code with cv::resize(srcimg, dstimg, dstimg.size()); app will run as expected but slower. – pradeepa Jul 24 '12 at 11:28
  • Hi Davids answer fixed the problem, the app is no longer crashing but running slower than before, please let me know how to use vImage efficiently. – pradeepa Jul 24 '12 at 12:13

3 Answers3

2

For the resampling APIs in vImage/Geometry.h, we chose to use the vector unit to deliver better quality rather than more speed. This is because the vector units are usually pretty poor at doing scattered accesses in memory, which is largely what you are doing for something simple like linear or nearest neighbor resampling with non-unit stride. They didn't seem like they were going to be good at making nearest neighbor or linear filtering go fast. So, instead we went to Lanczos filtering, which looks at a larger region of contiguous pixels to figure out each result pixel. It looks awesome (I think) but it's more work to get the awesomeness.

Also, c'mon if all you want is linear or nearest neighbor filtering, then the GPUs have hardware for that!

It is true that in general APIs in vImage are intended to give you faster results than rolling your own.

1

cv::resize uses linear interpolation by default. vImageScale_Planar8 uses Lanczos resampling, which is more complex, but also gives significantly better quality. You're comparing apples and oranges.

Stephen Canon
  • 103,815
  • 19
  • 183
  • 269
0

First, just a comment: normally the height would be rows, and the width columns - its seems odd the way you are using it.

Did you malloc the memory for the destination image:

dstimg.data = malloc(dstimg.cols * dstimg.rows);

You set the deployment target to ios5 or newer?

I've used the Accelerate framework with no problems on iOS5.

David H
  • 40,852
  • 12
  • 92
  • 138
  • Thanks that fixed the problem src.height=srcimg.rows src.width=srcimg.cols; but it seems like it is slower than before. Am i using the vImage properly, it should have increased the speed right? – pradeepa Jul 24 '12 at 12:07
  • No, it does not guarantee speed increases in all cases. If you are making this call many times you can prealloc the buffer it will need (assuming images the same size). The speed is not dependent on the row or col values, but the number of pixels processed. Note that if you are reducing the size by a binary factor you can probably "cheat" and grab every other pixel, every other row, etc and make it much faster. – David H Jul 24 '12 at 12:56
  • If the vImage Buffer is being recreated every time, you'll end up with slower than optimal performance because you're not allowing the cache to warm up. – Cameron Lowell Palmer Mar 19 '15 at 13:28
  • @CameronLowellPalmer your comment belongs to the question, not my answer. Obviously if you allocate the maximum chunk of memory you'll need, and re-use it, the performance of the second and later calls will increase. That is sort of CS 101, no? – David H Mar 19 '15 at 14:05
  • First, definitely not CS 101. As I recall machine architecture is at least a Sophmore and probably more Junior year. The topic of SIMD, vector processing might not be covered to the point where they would know how to exploit it. My point was the speed-up, vImage as a magic box, doesn't provide enough detail to explain how exactly it derives speed-ups. Apple in this case just waves its hands and says, "we do." So my comment was more to pradeepa. OpenCV by-the-way reuses allocated memory when possible. – Cameron Lowell Palmer Mar 19 '15 at 18:20