Detecting the rotation of digits in an image
You can use Hough Transform to find dominant lines in the image and then based on rho &amp; theta parameters of the Hough transform, align your text.
First you need to remove unnecessary details from your image through closing as below:

binTextImage=TextImage&lt;30;
close=imclose(binTextImage,strel('disk',20));




Then extract edges, 
    edges=edge(close)>0;

Then apply hough transform (the normal form):

H=hough(edges);
imshow(H)



Then find the maximal point in the hough domain, 

[X Y]=find(max(H(:))==H)


The rho and theta in the position X,Y represents parameters of the dominant line. Use the theta parameter to find out how much the image is rotated. 

More details in MATLAB example in following links is given:
https://www.mathworks.com/help/vision/examples/text-rotation-correction.html