-
Notifications
You must be signed in to change notification settings - Fork 2
/
removeBlemish.py
115 lines (82 loc) · 3.5 KB
/
removeBlemish.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
import cv2,argparse
import numpy as np
# Default radius
radius = 15
def findBestSquare(img, center):
# Initializing the variables
minVal = 1000.0
nSquares = 3
startPosition = np.array(center,dtype=int) - nSquares*radius
newPosition = np.copy(startPosition)
widthSquare = 2*radius
#check all the squares in a nSquares x nSquares neighborhood around the required point
for i in range(nSquares):
for j in range(nSquares):
#take a square from the neighborhood of the center
newPosition[0] = startPosition[0] + i*widthSquare
newPosition[1] = startPosition[1] + j*widthSquare
#check to see if the square we want is out of the image if yes, proceed to next square
if (newPosition[0] < 0 or newPosition[1] < 0 or
newPosition[1] + widthSquare > img.shape[0] or
newPosition[0] + widthSquare > img.shape[1]):
continue
checkSquare = img[newPosition[1]:newPosition[1] + widthSquare, newPosition[0]:newPosition[0] + widthSquare, :]
#find a measure of roughness of the square block
meanSobelX = np.mean(np.abs(cv2.Sobel(checkSquare, cv2.CV_32F, 1, 0 )))
meanSobelY = np.mean(np.abs(cv2.Sobel(checkSquare, cv2.CV_32F, 0, 1 )))
#if it is smoother than previous ones update the best square
if (meanSobelX + meanSobelY) < minVal:
minVal = meanSobelX + meanSobelY
bestSquare = checkSquare
else:
continue
return bestSquare
def onMouse(event, x, y, flags, param):
global src, blemish, mask, center
# If left click
if event == cv2.EVENT_LBUTTONDOWN:
center = (x, y)
# check if the point lies on the boundary of the image
if(x - radius < 0 or x + radius > src.shape[1] or y - radius < 0 or y + radius > src.shape[0]):
return
# catch the blemish region
blemish = src[center[1] - radius : center[1] + radius, center[0] - radius : center[0] + radius, :]
# find the smoothest region around the marked point
smoothRegion = findBestSquare(src,center)
# Create a white mask of the same size as the smooth region
mask = np.zeros(smoothRegion.shape, smoothRegion.dtype)
cv2.circle(mask, (radius, radius), radius, (255, 255, 255), thickness=-1, lineType=cv2.LINE_AA)
# Perform Seamless Cloning
src = cv2.seamlessClone(smoothRegion, src, mask, center, cv2.NORMAL_CLONE)
cv2.imshow("Blemish Remover",src)
# added functionality for UNDO-ing the last modification
elif event == cv2.EVENT_RBUTTONDOWN:
# Revert back to the previous result
src = cv2.seamlessClone(blemish, src, mask, center, cv2.NORMAL_CLONE)
cv2.imshow("Blemish Remover",src)
if __name__ == '__main__' :
ap = argparse.ArgumentParser()
ap.add_argument("-f", "--filename", help="Path to the image")
ap.add_argument("-r", "--radius", help="radius of square of blemish")
args = vars(ap.parse_args())
filename = "blemish.png"
# load the image and setup the mouse callback function
global src
if(args["filename"]):
filename = args["filename"]
src = cv2.imread(filename)
if(args["radius"]):
radius = int(args["radius"])
print("Using a patch of radius {}".format(radius))
cv2.namedWindow("Blemish Remover")
cv2.setMouseCallback("Blemish Remover", onMouse)
while True:
# display the image and wait for a keypress
cv2.imshow("Blemish Remover", src)
key = cv2.waitKey(1) & 0xFF
if key == 27:
cv2.destroyAllWindows()
break
# Save the image if 's' is pressed on the keyboard
if key == ord("s"):
cv2.imwrite('clean_blemish.jpg',src)