w3hello.com logo
Home PHP C# C++ Android Java Javascript Python IOS SQL HTML videos Categories
Read out RGB data from images with C on Raspberry PI

Your code is C++, but your error suggests you are trying to compile it as a C program because cstdio is the C++ version of C's stdio.h.

Your compile command:

gcc -shared -o mycfile.so -fPIC mycfile.c

Is trying to compile mycfile.c as C (not C++) which is causing your error. GCC detects the type of file based upon extension (.c for C, .cpp, .cxx, .cc , and .C for C++). Rename mycfile.c to mycfile.cpp, link the C++ runtime library stdc++, and run:

gcc -lstdc++ -shared -o mycfile.so -fPIC mycfile.cpp

I am unfamiliar with CImg, but if you're open to suggestions, you can give stb_image.h a try:

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

#include "stb_image.h"

double compare_pictures(const char* path1, const char* path2);

double compare_pictures(const char* path1, const char* path2)
{
    double totalDiff = 0.0;
    unsigned int x, y;

    int width1, height1, comps1;
    unsigned char * image1 = stbi_load(path1, &width1, &height1,
&comps1, 0);

    int width2, height2, comps2;
    unsigned char * image2 = stbi_load(path2, &width2, &height2,
&comps2, 0);

    if (image1 == NULL || image2 == NULL)
    {
        fprintf(stderr, "One of the images does not exist
");
        return -1;
    }

    if ((width1 != width2) || (height1 != height2))
    {
        fprintf(stderr, "width/height of the images must match!
");
        return -1;
    }
    else 
    {
        for (y = 0; y < height1; y++)
        {
            for (x = 0; x < width1; x++)
            {
                totalDIff += fabs((int)image1[(x + y*width1) * comps1 + 0]
- (int)image2[(x + y*width2) * comps2 + 0]) / 255.0;
                totalDiff += fabs((int)image1[(x + y*width1) * comps1 + 1]
- (int)image2[(x + y*width2) * comps2 + 1]) / 255.0;
                totalDiff += fabs((int)image1[(x + y*width1) * comps1 + 2]
- (int)image2[(x + y*width2) * comps2 + 2]) / 255.0;
            }
        }
        totalDiff = 100.0 * totalDiff / (double)(width1 * height1 * 3);
        printf("%lf
", totalDiff);
        return totalDiff;
    }
}




© Copyright 2018 w3hello.com Publishing Limited. All rights reserved.