Click here to Skip to main content
15,860,859 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
I am trying to compare two images to see the percentage difference in them.

This is my code…


    NSImage *_imageNSImage1 = [[NSImage alloc]initWithContentsOfFile:@"/Users/len/Pictures/Debut/Check These/Untitled 00000004.png"];
    NSSize _dimensions1 = [_imageNSImage1 size];
    
    NSImage *_imageNSImage2 = [[NSImage alloc]initWithContentsOfFile:@"/Users/len/Pictures/Debut/Check These/Untitled 00000005.png"];
    NSSize _dimensions2 = [_imageNSImage2 size];

    
    self.image1ImageWell.image = _imageNSImage1;
    self.image2ImageWell.image = _imageNSImage2;
    
    NSLog(@"Width from CIImage: %f",_dimensions1.width);
    NSLog(@"Height from CIImage: %f",_dimensions1.height);
    
    NSLog(@"Width from CIImage: %f",_dimensions2.width);
    NSLog(@"Height from CIImage: %f",_dimensions2.height);
   
        if (_dimensions1.width != _dimensions2.width)
        {
            NSLog(@"Images are of different sizes");
            return;
        }
        
        float diff = 0;

        for (int y = 0; y < _dimensions1.height; y++)
        {
            for (int x = 0; x < _dimensions1.width; x++)
               
                diff += fabs( GET_PIXEL(_imageNSImage1, x, y)[RED_C] - GET_PIXEL(_imageNSImage2, x, y)[RED_C] ) / 255.0;     // ’NSimage’ does not have a member named ‘width’
                diff += fabs( GET_PIXEL(_imageNSImage1, x, y)[GREEN_C] - GET_PIXEL(_imageNSImage2, x, y)[GREEN_C] ) / 255.0;     // ’NSimage’ does not have a member named ‘width’
                diff += fabs( GET_PIXEL(_imageNSImage1, x, y)[BLUE_C] - GET_PIXEL(_imageNSImage2, x, y)[BLUE_C] ) / 255.0;     // ’NSimage’ does not have a member named ‘width’

        }
    
        NSLog(@"diff: %f", 100 * diff / (_dimensions1.width * _dimensions1.height * 3));

The problem I am having is that ’NSimage’ does not have a member named ‘width’ in the last for loop as commented above.

How can I modify my code to have it working?

Thanks.

Lennox


What I have tried:

I have tried converting the images to bitmap images and then do a pixel by pixel comparison but I am having problem with GET_PIXEL, NSImage does not have that
Posted
Comments
Sergey Alexandrovich Kryukov 6-Jun-16 17:17pm    
You just need to define what is "difference". This is not as simple as it may seem; and pixel-by-pixel comparison may not reflect the intuitively understood "real" difference. Imagine two image just shifted a bit. Subjectively, they are near-identical, but pixel comparison may detect that they are totally different...
—SA
Member 11107475 6-Jun-16 17:43pm    
This is what I am trying to use...
https://rosettacode.org/wiki/Percentage_difference_between_images#C.2B.2B

I am currently using the equivalent of this in an app on Windows, compiled in Xojo, and am quite happy with it.

If I can get this converted to Objective C it would suit my needs...

GET_PIXEL(_imageNSImage2, x, y)[RED_C] ) / 255.0; // ’NSimage’ does not have a member named ‘width’
diff += fabs( GET_PIXEL(_imageNSImage1, x, y)[GREEN_C] - GET_PIXEL(_imageNSImage2, x, y)[GREEN_C] ) / 255.0; // ’NSimage’ does not have a member named ‘width’
diff += fabs( GET_PIXEL(_imageNSImage1, x, y)[BLUE_C] - GET_PIXEL(_imageNSImage2, x, y)[BLUE_C] ) / 255.0; // ’NSimage’ does not have a member named ‘width’

Thanks.

Lennox

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900