DoubleImage.py 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. # encoding: utf-8
  2. '''
  3. 用于两张图片比对检测
  4. '''
  5. import cv2 as cv
  6. import numpy as np
  7. import random
  8. # from matplotlib import pyplot as plt
  9. GRAY_LEVEL = 255
  10. class DoubleImage():
  11. def __init__(self):
  12. pass
  13. #裁剪图片
  14. def cutImage(self, img_addr):
  15. img = cv.imread(img_addr)
  16. img_arr = np.array(img)
  17. hight = img_arr.shape[0]
  18. wight = img_arr.shape[1]
  19. dest_img = img_arr[hight-280:hight-180,wight-280:wight-180]
  20. return dest_img
  21. '''
  22. 比较两张图片,如果根据threshold设定的容错值,有1%的点超过范围,则图片不一致。
  23. :param picPath1:第一个参数为为标准图地址,
  24. :param picPath2:第二个参数为测试图地址
  25. :param threshold: RGB颜色偏差容错范围
  26. '''
  27. def cmpPicTotal(self, picPath1, picPath2, threshold=20, expDotRate=0.01):
  28. srcImg1 = cv.imread(picPath1)
  29. srcImg2 = cv.imread(picPath2)
  30. return self.cmpImgTotal(srcImg1, srcImg2, threshold, expDotRate)
  31. '''
  32. 比较两张图片,如果根据threshold设定的容错值,有1%的点超过范围,则图片不一致。
  33. :param picPath1:第一个参数为为标准图对象,
  34. :param picPath2:第二个参数为测试图对象
  35. :param threshold: RGB颜色偏差容错范围
  36. '''
  37. def cmpImgTotal(self, srcImg1, srcImg2, threshold=20, expDotRate=0.01):
  38. shape = srcImg2.shape
  39. dstImg = srcImg1 - srcImg2
  40. cv.imwrite("D:/dstImg.png", dstImg)
  41. bArr = dstImg[:,:,0].ravel()
  42. gArr = dstImg[:,:,1].ravel()
  43. rArr = dstImg[:,:,2].ravel()
  44. dotSum = shape[0]*shape[1]+0.0
  45. bCount = np.where((bArr>threshold)&(bArr<=255))[0].__len__()
  46. gCount = np.where((gArr>threshold)&(gArr<=255))[0].__len__()
  47. rCount = np.where((rArr>threshold)&(rArr<=255))[0].__len__()
  48. print "cmpImgTotal BGR点异常个数比例:", bCount/dotSum,gCount/dotSum,rCount/dotSum
  49. if bCount/dotSum>expDotRate or gCount/dotSum > expDotRate or rCount/dotSum>expDotRate:
  50. return False
  51. else:
  52. return True
  53. '''
  54. 缺少衡量等级数字量化
  55. '''
  56. def cmpORB(self, im1, im2):
  57. orb = cv.ORB_create()
  58. kp = orb.detect(im1, None)
  59. kp, des1 = orb.compute(im1,kp)
  60. print "des1:",des1.__len__()
  61. # draw only keypoints location,not size and orientation
  62. # img1 = cv.drawKeypoints(im1, kp, None, color=(0, 255, 0), flags=0)
  63. # plt.imshow(img1), plt.show()
  64. orb = cv.ORB_create()
  65. kp2 = orb.detect(im2, None)
  66. kp2, des2 = orb.compute(im2, kp2)
  67. print "des2:",des2.__len__()
  68. # draw only keypoints location,not size and orientation
  69. # img2 = cv.drawKeypoints(im2, kp, None, color=(0, 255, 0), flags=0)
  70. # plt.imshow(img2), plt.show()
  71. if __name__ == '__main__':
  72. dImg = DoubleImage()
  73. # dImg.cmpPicTotal('flower.jpg', 'flower.jpg')
  74. im1 = cv.imread('std.jpg')
  75. im2 = cv.imread('test.jpg')
  76. # psnr = dImg.psnr(im1, im2)
  77. # print "im1,im2 psnr:", psnr
  78. # dImg.cmpHist(im1 , im2)
  79. dImg.cmpORB(im1,im2)