I'm currently getting screenshots of web pages in order to identify differences between our old CMS and the new one. In order to do that I use Selenium to take the screenshot as PNG image:
driver.get_screenshot_as_file(filepath)
Once I have the two screenshots, I try to compare them with 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)
Then I want to draw rectangles around the differences found.
When I use:
thresh = cv2.threshold(diff, 0, 255, cv2.THRESH_BINARY_INV | cv2.THRESH_OTSU)[1]
I get the following error message:
OpenCV Error: Assertion failed (src.type() == (((0) & ((1 << 3) - 1)) + (((1)-1) << 3))) in threshold
Turning off the adaptive threshold:
thresh = cv2.threshold(diff, 0, 255, cv2.THRESH_BINARY_INV)[1]
I manage to get one step further.
But then, the code that detects contours fails:
cnts = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
with the below error:
error: (-210) [Start]FindContours supports only CV_8UC1 images when mode != CV_RETR_FLOODFILL otherwise supports CV_32SC1 images only in function cvStartFindContours_Impl
I'm starting to wonder if the problem lies with the type of file used (png vs jpeg).
The complete, reproducible code is as follow:
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)
Sorry for the newbie question, but can someone point me in the right direction?