Ask Your Question
0

Deskew skews the untilted image

asked 2020-06-30 23:39:08 -0600

Malathi_72 gravatar image

updated 2020-07-01 03:24:45 -0600

supra56 gravatar image

Code:

    def correct_skew(image, delta=0.5, limit=5):
    def determine_score(arr, angle):
        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   
    scores = []
    angles = np.arange(-limit, limit + delta, delta)
    for angle in angles:
        histogram, score = determine_score(image, angle)
        scores.append(score)
    best_angle = angles[scores.index(max(scores))] 

    return best_angle
img = cv2.imread(images_path,cv2.IMREAD_GRAYSCALE)
thresh = cv2.threshold(img, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)[1] 
angle = correct_skew(img)
(h, w) = img.shape[:2]
center = (w // 2, h // 2)
M = cv2.getRotationMatrix2D(center, angle, 1.0)
rotated = cv2.warpAffine(img, M, (w, h), flags=cv2.INTER_CUBIC, \
              borderMode=cv2.BORDER_REPLICATE)
cv2.imwrite(rotimages_path,rotated)
edit retag flag offensive close merge delete

Comments

context ? explanation ? example image ?

berak gravatar imageberak ( 2020-07-01 01:11:06 -0600 )edit

@berak. No needed to do that context ? explanation ? example image. The indentation isn't accurately.

supra56 gravatar imagesupra56 ( 2020-07-01 07:38:45 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
0

answered 2020-07-01 07:27:01 -0600

supra56 gravatar image

updated 2020-07-01 07:42:15 -0600

I solved problem. It should be working. Your indentation isn't probably def correct_skew(image, delta=0.5, limit=5):, The correct way for indentation:

    def correct_skew(image, delta=0.5, limit=5):
        def determine_score(arr, angle):
            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   
        scores = []
        angles = np.arange(-limit, limit + delta, delta)
        for angle in angles:
            histogram, score = determine_score(image, angle)
            scores.append(score)
        best_angle = angles[scores.index(max(scores))] 

        return best_angle

img = cv2.imread(images_path,cv2.IMREAD_GRAYSCALE)
thresh = cv2.threshold(img, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)[1] 
angle = correct_skew(img)
(h, w) = img.shape[:2]
center = (w // 2, h // 2)
M = cv2.getRotationMatrix2D(center, angle, 1.0)
rotated = cv2.warpAffine(img, M, (w, h), flags=cv2.INTER_CUBIC, \
              borderMode=cv2.BORDER_REPLICATE)
cv2.imwrite(rotimages_path,rotated)
edit flag offensive delete link more

Comments

It is not a syntax error. It is working in my system, but for correct images, it adds skew. It is only on posting code alignment may be gone.

Malathi_72 gravatar imageMalathi_72 ( 2020-07-02 07:06:06 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2020-06-30 23:39:08 -0600

Seen: 568 times

Last updated: Jul 01 '20