0、Python包的导入
from skimage.measure import compare_ssim#~ import skimage as ssim # pip install scikit-imageimport argparseimport imutils# pip install imutilsimport cv2# pip install opencv-pythonimport uiautomator2 as u2import numpy as np
1、截图
![168bbef7073f2ef112083cf4fbdc8135.png](https://i-blog.csdnimg.cn/blog_migrate/6038d7ebb6348f877c70e20add32299c.jpeg)
手机截图展示
d = u2.connect('192.168.3.85')# 截图image=d.screenshot()# 截图# 保存截图image.save("./image/vivo.jpg", quality=95)
2、截取比对图片
![39ab60dc7e3213234371ff3a21f8b6bb.png](https://i-blog.csdnimg.cn/blog_migrate/2bfc33a8cc124500297f2b6dfaf7e37e.jpeg)
A
![a418a5cefc9247eee8644c369e24e171.png](https://i-blog.csdnimg.cn/blog_migrate/89bdff5812d7a313b35aded6460b75b8.jpeg)
B
前面已经通过Python完成了手机截图与存储
现在通过读取截图按照图片的像素点坐标与尺寸进行裁剪,分别命名为A、B
# 图片截取img = cv2.imread("./image/vivo.jpg")print(img.shape)x,y=48,1233cropped = img[y:y+492,x:x+492] # 裁剪坐标为[y0:y1, x0:x1]cv2.imwrite("./image/A.jpg", cropped)x,y=540,1233cropped = img[y:y+492,x:x+492] # 裁剪坐标为[y0:y1, x0:x1]cv2.imwrite("./image/B.jpg", cropped)
3、图片标记
![8a7b8a9493e867a2262a137a14e19009.png](https://i-blog.csdnimg.cn/blog_migrate/b95257204e29621852c1c0d226fd62fe.jpeg)
标记A
![d5c5a9ae1c7e4d7a2235329f1459902d.png](https://i-blog.csdnimg.cn/blog_migrate/97c6376c79d66ca98d3f906230742fde.jpeg)
标记B
imageA = cv2.imread("./image/A.jpg")imageB = cv2.imread("./image/B.jpg")grayA = cv2.cvtColor(imageA,cv2.COLOR_BGR2GRAY)grayB = cv2.cvtColor(imageB,cv2.COLOR_BGR2GRAY)# 计算两个灰度图像之间的结构相似度指数:(score,diff) = compare_ssim(grayA,grayB,full = True)diff = (diff *255).astype("uint8")print("SSIM:{}".format(score))# 找到不同点的轮廓以致于我们可以在被标识为“不同”的区域周围放置矩形:thresh = cv2.threshold(diff,0,255,cv2.THRESH_BINARY_INV | cv2.THRESH_OTSU)[1]# cnts = cv2.findContours(thresh.copy(),cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)# 新版cv2.findContours返回两个值,[0]号结果为轮廓cnts,hierarchy = cv2.findContours(thresh.copy(),cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)# print(cnts)# 找到一系列区域,在区域周围放置矩形:for c in cnts: (x,y,w,h) = cv2.boundingRect(c) print(c,(x,y),(x+w,y+h)) cv2.rectangle(imageA,(x,y),(x+w,y+h),(0,0,255),2) cv2.rectangle(imageB,(x,y),(x+w,y+h),(0,0,255),2) # 用cv2.imshow 展现最终对比之后的图片, cv2.imwrite 保存最终的结果图片# cv2.imshow("Modified",imageB)cv2.imwrite("./image/ModifiedB.png",imageB)# cv2.imshow("Modified",imageA)cv2.imwrite("./image/ModifiedA.png",imageA)
4、标记结果的拼接展示
![e3a410e785a2ec35368863929ed4c1be.png](https://i-blog.csdnimg.cn/blog_migrate/867eccf4decc93073de6add8eb3906b7.jpeg)
案例展示1
from PIL import Image#定义图片拼接函数def join(png1, png2, flag='horizontal'): """ :param png1: path :param png2: path :param flag: horizontal or vertical :return: """ img1, img2 = Image.open(png1), Image.open(png2) size1, size2 = img1.size, img2.size if flag == 'horizontal': joint = Image.new('RGB', (size1[0]+size2[0], size1[1])) loc1, loc2 = (0, 0), (size1[0], 0) joint.paste(img1, loc1) joint.paste(img2, loc2) joint.save('./image/horizontal.png') elif flag == 'vertical': joint = Image.new('RGB', (size1[0], size1[1]+size2[1])) loc1, loc2 = (0, 0), (0, size1[1]) joint.paste(img1, loc1) joint.paste(img2, loc2) joint.save('./image/vertical.png')join("./image/ModifiedA.png", "./image/ModifiedB.png")img = cv2.imread("./image/horizontal.png")cv2.imshow("img", img)cv2.waitKey(0)
![660f59e8306de2657ccb10502df9346e.png](https://i-blog.csdnimg.cn/blog_migrate/502dc1c8bc829159c8fdc469aab806de.jpeg)
案例展示2