如何对 cairo 创建的图像和从文件加载的图像进行逐像素比较

发布于 2025-01-10 06:26:33 字数 459 浏览 0 评论 0原文

我在 nim 中有一些代码,使用 Cairo 创建图片(https://github.com/nim-lang /开罗)。我想使用 diffimg 将该图片与另一张图片进行比较(https://github.com/SolitudeSF/diffimghttps://github.com/SolitudeSF/imageman)。

但内存图像类型似乎没有一个标准。有没有什么方法可以做到这一点,而不需要先将图像保存到文件中?

I have some code in nim that creates a picture using Cairo (https://github.com/nim-lang/cairo). I would like to compare that picture to another using diffimg (https://github.com/SolitudeSF/diffimg, https://github.com/SolitudeSF/imageman).

But there does not seem to be a standard in memory image type. Is there any way to do this that does not involve saving the image to a file first?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

束缚m 2025-01-17 06:26:33

令人惊讶的是,最简单的方法可能是自己实现 diffimg 算法。查看 diffimg 的源代码显示比较算法是 大约20行代码

func absDiff[T: ColorComponent](a, b: T): T {.inline.} =
  if a > b:
    a - b
  else:
    b - a

func getDiffRatio*[T: Color](a, b: Image[T]): float =
  for p in 0..a.data.high:
    for c in 0..T.high:
      result += absDiff(a[p][c], b[p][c]).float
  result / (T.maxComponentValue.float * a.data.len.float * T.len.float)

func genDiffImage*[T: Color](a, b: Image[T]): Image[T] =
  result = initImage[T](a.w, a.h)
  for p in 0..result.data.high:
    for c in 0..T.high:
      result[p][c] = absDiff(a[p][c], b[p][c])

加载图像的实际麻烦留给了imageman,但总而言之似乎是减去像素分量两个图像之间的值并创建某种平均值/比率。由于 cairo 库似乎为图像生成了自己的、可能不兼容的内存布局,因此您很可能希望忽略 imageman 并将要与自己进行比较的图像加载到 cairo 内存缓冲区中,然后复制比较算法迭代每个 caro 图像的像素。或者可以将 cairo 缓冲区转换为 imageman 缓冲区,然后让 diffimg 实现发挥其魔力。

Probably the most easy way is surprisingly to implement yourself the diffimg algorithm. Looking at the source of diffimg shows the comparison algoritm is about 20 lines of code:

func absDiff[T: ColorComponent](a, b: T): T {.inline.} =
  if a > b:
    a - b
  else:
    b - a

func getDiffRatio*[T: Color](a, b: Image[T]): float =
  for p in 0..a.data.high:
    for c in 0..T.high:
      result += absDiff(a[p][c], b[p][c]).float
  result / (T.maxComponentValue.float * a.data.len.float * T.len.float)

func genDiffImage*[T: Color](a, b: Image[T]): Image[T] =
  result = initImage[T](a.w, a.h)
  for p in 0..result.data.high:
    for c in 0..T.high:
      result[p][c] = absDiff(a[p][c], b[p][c])

The actual trouble of loading the image is left to imageman, but all in all it seems to substract pixel component values between the two images and create some kind of average/ratio. Since the cairo library seems to generate its own, possibly incompatible, memory layout for images, most likely you want to ignore imageman and load the image you want to compare to yourself into a cairo memory buffer, then replicate the diffing algorithm iterating through the pixels of each caro image. Or maybe convert the cairo buffer to an imageman buffer and let the diffimg implementation do its magic.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文