Frage

I'm writing an Objective-C algorithm that compares two images and outputs the differences.

Occasionally two identical images will be passed in. Is there a way to tell immediately from the resulting CGImageRef that it contains no data? (i.e. only transparent pixels).

The algorithm is running at > 20 fps so performance is a top priority.

War es hilfreich?

Lösung

You should go with CoreImage here. Have a look at the "CIArea*" filters.

See Core Image Filter reference here: http://developer.apple.com/library/mac/#documentation/GraphicsImaging/Reference/CoreImageFilterReference/Reference/reference.html

This will be A LOT faster than any of the previous approaches. Let us know if this works for you.

Andere Tipps

From a performance perspective, you should incorporate this check into your comparison-algorithm. The most expensive operation when working on images is most of the time loading a small bit of the image into cache. Once you got it there, there are plenty of ways of working on the data really fast (SIMD), but the problem is that you need to evict and reload the cache with new data all the time, and this is computationally expensive. Now, if you already have been through every pixel of both images once in your algorithm, it would make sense to at the same time compute the SAD while you still got the data in cache. So in pseudo-code:

int total_sad = 0
for y = 0; y < heigth; y++
  for x = 0; x < width; x+=16
    xmm0 = load_data (image0 + y * width + x)
    xmm1 = load_data (image1 + y * width + x)

    /* this stores the differences (your algorithm) */
    store_data (result_image + y * width + x, diff (xmm0, xmm1))
    /* this does the SAD at the same time */
    total_sad += sad (xmm0, xmm1)
if (total_sad == 0)
  print "the images are identical!"

Hope that helps.

Not sure about this but if you can have a sample image of completly blank image already exists then,

UIImage *image = [UIImage imageWithCGImage:imgRef]; //imgRef is your CGImageRef
if(blankImageData == nil)
{
    UIImage *blankImage = [UIImage imageNamed:@"BlankImage.png"]; 
    blankImageData = UIImagePNGRepresentation(blankImage); //blankImageData some global for cache
}

// Now comparison
imageData = UIImagePNGRepresentation(image);// Image from CGImageRef
if([imageData isEqualToData:blankImageData])
{
   // Your image is blank
}
else
{
   // There are some colourful pixel :)
}
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top