1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182
| from PIL import Image from PIL import ImageChops import signal import numpy as np import math import cv2 as cv from skimage.measure import compare_ssim import argparse import imutils
high=0 width=0
rawy=200 rawx=200
anchor_y=0
down_y1=0 down_y2=0 up_y1=0 up_y2=0
m=160 h=200
def picture_cut(): ''' 进行图片上下裁剪,并截取上部分中的一个样本 ''' global width global high img = Image.open("./raw.jpg") print(img.size) width = img.size[0] high = img.size[1] cropped_up = img.crop((0, 0, width, high/2)) cropped_down = img.crop((0,high/2,width,high)) matrix=np.array(cropped_down) img_down=Image.fromarray(matrix) cropped_down=img_down.transpose(Image.FLIP_TOP_BOTTOM) cropped_tem = img.crop((100,100,rawx,rawy)) cropped_up.save("./cut_up.jpg") cropped_down.save("./cut_down.jpg") cropped_tem.save("./tmp.jpg") def template_demo(): ''' 图片匹配,大图找小图 @target 原图 @tpl 小图 ''' global anchor_y tpl =cv.imread("./tmp.jpg") target = cv.imread("./cut_down.jpg") methods = [cv.TM_SQDIFF_NORMED, cv.TM_CCORR_NORMED, cv.TM_CCOEFF_NORMED] th, tw = tpl.shape[:2] for md in methods: print(md) result = cv.matchTemplate(target, tpl, md) min_val, max_val, min_loc, max_loc = cv.minMaxLoc(result) if md == cv.TM_SQDIFF_NORMED: tl = min_loc else: tl = max_loc br = (tl[0]+tw, tl[1]+th) anchor_y=tl[1]+th print("小图右下角在原图中的的坐标{}".format(br))
def final_cut(up_y1,up_y2,down_y1,down_y2): ''' 裁剪最终可以用来对比的图像 ''' img = Image.open("./raw.jpg") print("final") cropped_up = img.crop((0, up_y1, width, up_y2)) cropped_down = img.crop((0,down_y1,width,down_y2)) matrix=np.array(cropped_down) img_down=Image.fromarray(matrix) cropped_down=img_down.transpose(Image.FLIP_TOP_BOTTOM) cropped_up.save("./cut_final_up.jpg") cropped_down.save("./cut_final_down.jpg")
def compare_images(path_one, path_two, diff_save_location): """ 比较图片,如果有不同则生成展示不同的图片 @参数一: path_one: 第一张图片的路径 @参数二: path_two: 第二张图片的路径 @参数三: diff_save_location: 不同图的保存路径 """ image_one = Image.open(path_one) image_two = Image.open(path_two) try: diff = ImageChops.difference(image_one, image_two)
if diff.getbbox() is None: print("【+】We are the same!") else: diff.save(diff_save_location) except ValueError as e: text = ("表示图片大小和box对应的宽度不一致,参考API说明:Pastes another image into this image." "The box argument is either a 2-tuple giving the upper left corner, a 4-tuple defining the left, upper, " "right, and lower pixel coordinate, or None (same as (0, 0)). If a 4-tuple is given, the size of the pasted " "image must match the size of the region.使用2纬的box避免上述问题") print("【{0}】{1}".format(e,text))
def mark(): ''' 标注出图片的不同处 ''' imageA = cv.imread("./cut_final_down.jpg") imageB = cv.imread("./cut_final_up.jpg") grayA = cv.cvtColor(imageA,cv.COLOR_BGR2GRAY) grayB = cv.cvtColor(imageB,cv.COLOR_BGR2GRAY) (score,diff) = compare_ssim(grayA,grayB,full = True) diff = (diff *255).astype("uint8") print("SSIM:{}".format(score)) thresh = cv.threshold(diff,0,255,cv.THRESH_BINARY_INV | cv.THRESH_OTSU)[1] cnts = cv.findContours(thresh.copy(),cv.RETR_EXTERNAL,cv.CHAIN_APPROX_SIMPLE) cnts = imutils.grab_contours(cnts) for c in cnts: (x,y,w,h) = cv.boundingRect(c) cv.rectangle(imageA,(x,y),(x+w,y+h),(0,0,255),2) cv.rectangle(imageB,(x,y),(x+w,y+h),(0,0,255),2) cv.imshow("Modified",imageB) cv.imwrite("final.png",imageB) cv.waitKey(0) if __name__ == '__main__': picture_cut() template_demo() print("在图片窗口按任意键退出") up_y1=rawx-h up_y2=rawy+m print("high: {} anchor_y: {} ".format(high,anchor_y)) down_y1=high-anchor_y-m down_y2=high-anchor_y+h print("{} {} {} {}".format(up_y1,up_y2,down_y1,down_y2)) final_cut(up_y1,up_y2,down_y1,down_y2)
compare_images('./cut_final_up.jpg', './cut_final_down.jpg', 'res_diff.jpg')
print("最终对比图片为: res_diff.jpg final.png") mark()
|