我目前正在获取网页截图,以便识别旧CMS和新CMS之间的差异。为此,我使用Selenium将屏幕截图设为PNG图像:
driver.get_screenshot_as_file(filepath)
一旦我有两个截图,我尝试将它们与opencv进行比较:
imageA = cv2.imread("screenshota.png")
imageB = cv2.imread("screenshotb.png")
grayA = cv2.cvtColor(imageA, cv2.COLOR_BGR2GRAY)
grayB = cv2.cvtColor(imageB, cv2.COLOR_BGR2GRAY)
(score, diff) = compare_ssim(grayA, grayB, full=True)
然后我想在发现的差异周围画出矩形。
当我使用时:
thresh = cv2.threshold(diff, 0, 255, cv2.THRESH_BINARY_INV | cv2.THRESH_OTSU)[1]
我收到以下错误消息:
OpenCV Error: Assertion failed (src.type() == (((0) & ((1 << 3) - 1)) + (((1)-1) << 3))) in threshold
关闭自适应阈值:
thresh = cv2.threshold(diff, 0, 255, cv2.THRESH_BINARY_INV)[1]
我设法更进了一步。
但是,检测轮廓的代码失败了:
cnts = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
出现以下错误:
error: (-210) [Start]FindContours supports only CV_8UC1 images when mode != CV_RETR_FLOODFILL otherwise supports CV_32SC1 images only in function cvStartFindContours_Impl
我开始怀疑问题是否与使用的文件类型有关(png vs jpeg)。
完整,可重现的代码如下:
imageA = cv2.imread("screenshota.png")
imageB = cv2.imread("screenshotb.png")
grayA = cv2.cvtColor(imageA, cv2.COLOR_BGR2GRAY)
grayB = cv2.cvtColor(imageB, cv2.COLOR_BGR2GRAY)
(score, diff) = compare_ssim(grayA, grayB, full=True)
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)
对不起新手问题,但是有人能指出我正确的方向吗?