I have used Projection Profile
method on my binary images to get the deskew version. Everything is fine but the rotated image is having Black areas where the deskewing has been applied. How can I convert that area to white instead of black. Below is the code for Projection Profile.
def correct_skew(image, delta=1, limit=5):
"""
image : input
delta : sampling in the -limit,limit + delta range
limit : range of angles to explore
"""
# Function that returns the score of histogram for the given angle at which we check
def determine_score(arr, angle):
"""
arr : binarized image
angle : angle at which we calcuate the score
"""
data = inter.rotate(arr, angle, reshape=False, order=0)
histogram = np.sum(data, axis=1)
score = np.sum((histogram[1:] - histogram[:-1]) ** 2)
return histogram, score
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
thresh = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)[1]
scores = []
angles = np.arange(-limit, limit + delta, delta)
for angle in angles:
histogram, score = determine_score(thresh, angle)
scores.append(score)
best_angle = angles[scores.index(max(scores))]
(h, w) = image.shape[:2]
center = (w // 2, h // 2)
M = cv2.getRotationMatrix2D(center, best_angle, 1.0)
rotated = cv2.warpAffine(image, M, (w, h), flags=cv2.INTER_CUBIC)
return best_angle, rotated
This is the Image after Deskewing:
The cv2.warpaffine documentation states that the function takes an optional argument namely borderValue
. By default this value is (0, 0, 0)
, You can change this by calling your warpaffine routine as:
rotated = cv2.warpAffine(image, M, (w, h), flags=cv2.INTER_CUBIC, borderMode = cv2.BORDER_CONSTANT, borderValue=np.array([255, 255, 255]))