我在 nim 中有一些代碼可以使用 Cairo ( https://github.com/nim-lang/cairo )創建圖片。我想使用 diffimg(https://github.com/SolitudeSF/diffimg,https://github.com/SolitudeSF/imageman)將該圖片與另一張圖片進行比較。
但是記憶體影像型別似乎沒有標準。有什么方法可以做到這一點,而不涉及先將影像保存到檔案中?
uj5u.com熱心網友回復:
可能最簡單的方法是令人驚訝地自己實作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 記憶體緩沖區中,然后復制迭代演算法的像素每個卡羅影像。或者也許將 cairo 緩沖區轉換為imageman緩沖區并讓diffimg實作發揮它的魔力。
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/434007.html
