This commit is contained in:
Matthew Frost 2023-11-08 22:17:08 +01:00
parent a3b6765ad2
commit a8a02b7765
14 changed files with 35 additions and 29 deletions

Binary file not shown.

Before

(image error) Size: 476 KiB

BIN
gray.jpg

Binary file not shown.

Before

(image error) Size: 406 KiB

BIN
images/blurred.jpg Normal file

Binary file not shown.

After

(image error) Size: 344 KiB

View file

Before

(image error) Size: 1,023 KiB

After

(image error) Size: 1,023 KiB

View file

Before

(image error) Size: 1.1 MiB

After

(image error) Size: 1.1 MiB

BIN
images/clean2.jpeg Normal file

Binary file not shown.

After

(image error) Size: 728 KiB

View file

Before

(image error) Size: 867 KiB

After

(image error) Size: 867 KiB

View file

Before

(image error) Size: 867 KiB

After

(image error) Size: 867 KiB

BIN
images/dirty1.jpeg Normal file

Binary file not shown.

After

(image error) Size: 747 KiB

BIN
images/dirty2.jpeg Normal file

Binary file not shown.

After

(image error) Size: 725 KiB

BIN
images/gray.jpg Normal file

Binary file not shown.

After

(image error) Size: 294 KiB

View file

Before

(image error) Size: 5.3 KiB

After

(image error) Size: 5.3 KiB

63
main.py
View file

@ -1,37 +1,42 @@
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
image = cv2.imread('dirty.jpeg')
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
refimage = cv2.imread('clean2.jpeg')
image = cv2.imread('dirty2.jpeg')
# Apply Gaussian blur to reduce noise
gray = cv2.GaussianBlur(gray, (9, 9), 2)
res = cv2.absdiff(refimage, image)
# Detect circles using the Hough Circles method
circles = cv2.HoughCircles(
gray,
cv2.HOUGH_GRADIENT,
dp=1, # Inverse ratio of accumulator resolution to image resolution
minDist=20, # Minimum distance between the centers of detected circles
param1=50, # Higher threshold for edge detection
param2=30, # Accumulator threshold for circle detection
minRadius=10, # Minimum radius
maxRadius=100 # Maximum radius
)
res = res.astype(np.uint8)
if circles is not None:
circles = np.uint16(np.around(circles))
for circle in circles[0, :]:
center = (circle[0], circle[1])
radius = circle[2]
# Draw the circle and its center
cv2.circle(image, center, radius, (0, 255, 0), 2)
cv2.circle(image, center, 2, (0, 0, 255), 3)
percentage = (np.count_nonzero(res) * 100)/ res.size
# Display the result
cv2.imshow('Hough Circles', image)
cv2.waitKey(0)
cv2.destroyAllWindows()
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()

1
models/mobile_sam.pt Normal file

File diff suppressed because one or more lines are too long