Python OpenCV 简单图像比较

项目需要做两张图像比较的差,感觉Python用起来很方便,手头正好有OpenCV。红绿代表插值正负,为了更好的可视化,差值均放大10倍显示。

  1. import cv2
  2. def clmap(v, k, upBound): #mul and clamp
  3. val = v * k
  4. if val > upBound:
  5. return upBound
  6. else:
  7. return val
  8. inImage_1 = 'B_Tri.bmp' #
  9. inImage_2 = 'B_RT.bmp' #
  10. dif_img = 'dif_' + inImage_1
  11. img_1 = cv2.imread(inImage_1) # read as color image
  12. img_2 = cv2.imread(inImage_2)
  13. dif = img_1.copy()
  14. show_dif = dif.copy() # dif image for show only
  15. width = img_1.shape[ 0] #get width
  16. height = img_1.shape[ 1] #get height
  17. for i in range(width):
  18. for j in range(height):
  19. #dif[i, j] = [128,0,0] # b g r
  20. #print img_1[i,j]-img_2[i,j]
  21. diff = int(img_1[i,j][ 0])-int(img_2[i,j][ 0])
  22. if diff < 0:
  23. show_dif[i,j] = [ 0,clmap(abs(diff), 10, 255), 0]
  24. elif diff > 0:
  25. show_dif[i, j]= [ 0, 0, clmap(abs(diff), 10, 255)]
  26. else:
  27. show_dif[i,j] = [ 0]
  28. dif[i,j] = [abs(diff)] * 3
  29. print dif_img
  30. print 'different data:'
  31. print 'max : ', dif.max()
  32. print 'min : ', dif.min()
  33. print 'mean : ', dif.mean()
  34. cv2.imwrite(dif_img, show_dif)
  35. cv2.imshow( '_dif',show_dif)
  36. cv2.waitKey( 0)
  37. cv2.destroyAllWindows()

image1

image2

dif

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

安果移不动

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值