在互联网上搜索了很长时间后,我找不到真正解决我的“问题”的方法。
我想做什么:
在 C 中比较 2 个图像(使用 Raspberry Pi 相机在 Python 脚本中创建)。 我已经在 Python 中尝试过这个,但它太慢了(每 2 张图像 +/- 1 分钟)。
所以我想在 C 中尝试一下。我从我的 Python 脚本中调用一个带有 ctypes 的 C 函数。 C 函数期望获得包含 2 个图像路径的 2 个字符串。 C 函数必须向 Python 脚本返回一个 double 变量(差异百分比)。
我尝试了什么:
我将图像存储为 .JPG,因此我搜索了一个可以处理 jpg 格式的 c 库。我在 stackoverflow 上找到了一篇建议 CImg 的帖子。我无法让它在我的 Raspberry Pi 上运行。说找不到进口。
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include "cimg/CImg.h"
using namespace cimg_library;
double compare_pictures(const char* path1, const char* path2);
// Compares two Pictures and returns the difference value
double compare_pictures(const char* path1, const char* path2)
{
CImg<unsigned char> image1(path1);
CImg<unsigned char> image2(path2);
double totalDiff = 0.0;
unsigned int x, y;
if (image1 == NULL || image2 == NULL)
{
fprintf(stderr, "One of the images does not exist\n");
return -1;
}
if ((image1.width() != image2.width()) || (image1.height() != image2.height()))
{
fprintf(stderr, "width/height of the images must match!\n");
return -1;
}
else
{
for (y = 0; y < image1.height; y++)
{
for (x = 0; x < image1.width; x++)
{
totalDiff += fabs((int)image1(x, y, 0, 0) - (int)image2(x, y, 0, 0)) / 255.0;
totalDiff += fabs((int)image1(x, y, 0, 1) - (int)image2(x, y, 0, 1)) / 255.0;
totalDiff += fabs((int)image1(x, y, 0, 2) - (int)image2(x, y, 0, 2)) / 255.0;
}
}
totalDiff = 100.0 * totalDiff / (double)(image1.width() * image1.height() * 3);
printf("%lf\n", totalDiff);
return totalDiff;
}
}
CImg.h:73:18: fatal error: cstdio: No such file or directory. compilation terminated.
经过一些尝试,我放弃了,回到互联网上寻找另一个图书馆。我找到了适用于 Raspberry Pi 和 C 的 libjpeg8-dev。尽管如此,这对我也没有太大帮助,因为我真的找不到很好的教程/文档来说明如何将它用于我的目的。
我只想比较 Raspberry Pi 相机创建的图像并快速计算差异百分比(最好在不到一秒内)
最佳答案
您的代码是 C++,但您的错误表明您正在尝试将其编译为 C 程序,因为 cstdio
是 C 的 stdio.h
的 C++ 版本。
你的编译命令:
gcc -shared -o mycfile.so -fPIC mycfile.c
正在尝试将 mycfile.c
编译为 C(而非 C++),这导致了您的错误。 GCC 根据扩展名检测文件类型(.c
for C, .cpp
, .cxx
, .cc
和 C++ 的 .C
)。将mycfile.c
重命名为mycfile.cpp
,链接C++运行时库stdc++
,运行:
gcc -lstdc++ -shared -o mycfile.so -fPIC mycfile.cpp
我不熟悉 CImg,但如果您愿意接受建议,您可以 给stb_image.h尝试一下:
#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\n");
return -1;
}
if ((width1 != width2) || (height1 != height2))
{
fprintf(stderr, "width/height of the images must match!\n");
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\n", totalDiff);
return totalDiff;
}
}
关于c - 在 Raspberry PI 上用 C 从图像中读取 RGB 数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26911620/