42 lines
No EOL
1 KiB
Python
42 lines
No EOL
1 KiB
Python
import cv2
|
|
import numpy as np
|
|
from mobile_sam import sam_model_registry, SamAutomaticMaskGenerator, SamPredictor
|
|
import torch
|
|
|
|
model_type = "vit_t"
|
|
sam_checkpoint = "./models/mobile_sam.pt"
|
|
|
|
|
|
backSub = cv2.createBackgroundSubtractorMOG2(varThreshold=16 )
|
|
# Load the image
|
|
refimage = cv2.imread('clean2.jpeg')
|
|
image = cv2.imread('dirty2.jpeg')
|
|
|
|
res = cv2.absdiff(refimage, image)
|
|
|
|
res = res.astype(np.uint8)
|
|
|
|
percentage = (np.count_nonzero(res) * 100)/ res.size
|
|
|
|
print(percentage)
|
|
#cv2.imwrite('/var/www/html/images/blurred.jpg', blurred)
|
|
|
|
|
|
print("Converting to grey")
|
|
gray_refimage = cv2.cvtColor(refimage, cv2.COLOR_BGR2GRAY)
|
|
gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
|
|
|
|
|
|
blurred_refimage = cv2.GaussianBlur(gray_refimage, (9, 9), 2, 2)
|
|
blurred_image = cv2.GaussianBlur(gray_image, (9, 9), 2, 2)
|
|
|
|
#cv2.imwrite('/var/www/html/images/gray.jpg', gray)
|
|
maska = backSub.apply(blurred_refimage)
|
|
maskb = backSub.apply(blurred_image)
|
|
|
|
cv2.imshow("maskb",maskb)
|
|
|
|
cv2.waitKey(0)
|
|
|
|
# closing all open windows
|
|
cv2.destroyAllWindows() |