Click here to Skip to main content
15,886,067 members
Home / Discussions / C#
   

C#

 
QuestionHow to check that dsn which is created used for Sql Server or Access using C# Pin
Awadhendra123422-Feb-12 2:16
Awadhendra123422-Feb-12 2:16 
AnswerRe: How to check that dsn which is created used for Sql Server or Access using C# Pin
PIEBALDconsult22-Feb-12 2:56
mvePIEBALDconsult22-Feb-12 2:56 
GeneralRe: How to check that dsn which is created used for Sql Server or Access using C# Pin
Awadhendra123422-Feb-12 3:24
Awadhendra123422-Feb-12 3:24 
GeneralRe: How to check that dsn which is created used for Sql Server or Access using C# Pin
PIEBALDconsult22-Feb-12 4:00
mvePIEBALDconsult22-Feb-12 4:00 
QuestionDetect Marked Checkbox using OCR Pin
Zeyad Jalil22-Feb-12 2:12
professionalZeyad Jalil22-Feb-12 2:12 
AnswerRe: Detect Marked Checkbox using OCR PinPopular
enhzflep22-Feb-12 4:44
enhzflep22-Feb-12 4:44 
GeneralRe: Detect Marked Checkbox using OCR Pin
Zeyad Jalil22-Feb-12 20:29
professionalZeyad Jalil22-Feb-12 20:29 
GeneralRe: Detect Marked Checkbox using OCR Pin
enhzflep22-Feb-12 20:43
enhzflep22-Feb-12 20:43 
Here you go - here's the VB code to calculate the skew angle of an image.
I'd throw you the C# version, but I don't have one - I converted this code to c++

You can then find an algorithm to rotate by -skewAngle. (I'll leave that to you)
I might add, I spent many 10s of hours on this project. For your own sanity, I suggest patience!

VB
Imports Microsoft.VisualBasic

Public Class deskew
    ' Representation of a line in the image.
    Public Class HougLine
        ' Count of points in the line.
        Public Count As Integer
        ' Index in Matrix.
        Public Index As Integer
        ' The line is represented as all x,y that solve y*cos(alpha)-x*sin(alpha)=d
        Public Alpha As Double
        Public d As Double
    End Class
    ' The Bitmap
    Dim cBmp As Bitmap
    ' The range of angles to search for lines
    Const stepsPerDeg = 5
    Const minAngle = -13
    Const maxAngle = 13
    Dim cAlphaStart As Double = minAngle
    Dim cAlphaStep As Double = 1 / stepsPerDeg
    Dim cSteps As Integer = (maxAngle - minAngle) * stepsPerDeg
    ' Precalculation of sin and cos.
    Dim cSinA() As Double
    Dim cCosA() As Double
    ' Range of d
    Dim cDMin As Double
    Dim cDStep As Double = 1
    Dim cDCount As Integer
    ' Count of points that fit in a line.
    Dim cHMatrix() As Integer

    Public getPixelCalls As Long

    Public Sub setAngleRange(ByVal minAngle As Long, ByVal maxAngle As Long)
        cAlphaStart = minAngle
        cSteps = (maxAngle - minAngle) / cAlphaStep
    End Sub

    ' Calculate the skew angle of the image cBmp.
    Public Function GetSkewAngle() As Double
        Dim hl() As deskew.HougLine
        Dim i As Integer
        Dim sum As Double
        Dim count As Integer

        ' Hough Transformation
        Calc()
        ' Top 20 of the detected lines in the image.
        hl = GetTop(20)
        ' Average angle of the lines
        For i = 0 To 19
            sum += hl(i).Alpha
            count += 1
        Next
        Return sum / count
    End Function

    ' Calculate the Count lines in the image with most points.
    Private Function GetTop(ByVal Count As Integer) As HougLine()
        Dim hl() As HougLine
        Dim i As Integer
        Dim j As Integer
        Dim tmp As HougLine
        Dim AlphaIndex As Integer
        Dim dIndex As Integer

        ReDim hl(Count)
        For i = 0 To Count - 1
            hl(i) = New HougLine
        Next
        For i = 0 To cHMatrix.Length - 1
            If cHMatrix(i) > hl(Count - 1).Count Then
                hl(Count - 1).Count = cHMatrix(i)
                hl(Count - 1).Index = i
                j = Count - 1
                While j > 0 AndAlso hl(j).Count > hl(j - 1).Count
                    tmp = hl(j)
                    hl(j) = hl(j - 1)
                    hl(j - 1) = tmp
                    j -= 1
                End While
            End If
        Next
        For i = 0 To Count - 1
            dIndex = hl(i).Index \ cSteps
            AlphaIndex = hl(i).Index - dIndex * cSteps
            hl(i).Alpha = GetAlpha(AlphaIndex)
            hl(i).d = dIndex + cDMin
        Next
        Return hl
    End Function
    Public Sub New(ByVal bmp As Bitmap)
        cBmp = bmp
    End Sub
    ' Hough Transforamtion:
    Private Sub Calc()
        Dim x As Integer
        Dim y As Integer
        Dim hMin As Integer = cBmp.Height / 4
        Dim hMax As Integer = cBmp.Height * 3 / 4

        Init()
        For y = hMin To hMax
            For x = 1 To cBmp.Width - 2
                ' Only lower edges are considered.
                If IsBlack(x, y) Then
                    If Not IsBlack(x, y + 1) Then
                        Calc(x, y)
                    End If
                End If
            Next
        Next
    End Sub
    ' Calculate all lines through the point (x,y).
    Private Sub Calc(ByVal x As Integer, ByVal y As Integer)
        Dim alpha As Integer
        Dim d As Double
        Dim dIndex As Integer
        Dim Index As Integer

        For alpha = 0 To cSteps - 1
            d = y * cCosA(alpha) - x * cSinA(alpha)
            dIndex = CalcDIndex(d)
            Index = dIndex * cSteps + alpha
            Try
                cHMatrix(Index) += 1
            Catch ex As Exception
                Debug.WriteLine(ex.ToString)
            End Try
        Next
    End Sub
    Private Function CalcDIndex(ByVal d As Double) As Double
        Return Convert.ToInt32(d - cDMin)
    End Function
    Private Function IsBlack(ByVal x As Integer, ByVal y As Integer) As Boolean
        Dim c As Color
        Dim luminance As Double
        getPixelCalls += 1
        c = cBmp.GetPixel(x, y)
        luminance = (c.R * 0.299) + (c.G * 0.587) + (c.B * 0.114)
        Return luminance < 140
    End Function
    Private Sub Init()
        Dim i As Integer
        Dim angle As Double
        getPixelCalls = 0
        ' Precalculation of sin and cos.
        ReDim cSinA(cSteps - 1)
        ReDim cCosA(cSteps - 1)
        For i = 0 To cSteps - 1
            angle = GetAlpha(i) * Math.PI / 180.0#
            cSinA(i) = Math.Sin(angle)
            cCosA(i) = Math.Cos(angle)
        Next
        ' Range of d:
        cDMin = -cBmp.Width
        cDCount = 2 * (cBmp.Width + cBmp.Height) / cDStep
        ReDim cHMatrix(cDCount * cSteps)
    End Sub

    Public Function GetAlpha(ByVal Index As Integer) As Double
        Return cAlphaStart + Index * cAlphaStep
    End Function
End Class

Questionsmtp unknown sender Pin
CS3421-Feb-12 23:23
CS3421-Feb-12 23:23 
AnswerRe: smtp unknown sender Pin
Richard MacCutchan22-Feb-12 3:14
mveRichard MacCutchan22-Feb-12 3:14 
Questionadd application to \\root\SecurityCenter2\antispywareproduct as antispyware Pin
abhinish21-Feb-12 23:12
abhinish21-Feb-12 23:12 
Questionhow can i scann image from scanner in C# Pin
A7mad_21-Feb-12 22:55
A7mad_21-Feb-12 22:55 
AnswerRe: how can i scann image from scanner in C# Pin
lukeer22-Feb-12 2:17
lukeer22-Feb-12 2:17 
Questiontrouble in connecting from C# to Postgre SQL. Pin
hellono121-Feb-12 20:46
hellono121-Feb-12 20:46 
AnswerRe: trouble in connecting from C# to Postgre SQL. Pin
BobJanova21-Feb-12 21:42
BobJanova21-Feb-12 21:42 
QuestionIntegration of .NET with retail billing machine Pin
tahernd21-Feb-12 19:14
tahernd21-Feb-12 19:14 
AnswerRe: Integration of .NET with retail billing machine Pin
Richard MacCutchan21-Feb-12 21:24
mveRichard MacCutchan21-Feb-12 21:24 
AnswerRe: Integration of .NET with retail billing machine Pin
Mycroft Holmes21-Feb-12 21:39
professionalMycroft Holmes21-Feb-12 21:39 
GeneralRe: Integration of .NET with retail billing machine Pin
tahernd22-Feb-12 0:26
tahernd22-Feb-12 0:26 
QuestionAny difference between Console App and Windows Service for HttpWebRequest..? [Solved] Pin
Paladin200021-Feb-12 6:38
Paladin200021-Feb-12 6:38 
AnswerRe: Any difference between Console App and Windows Service for HttpWebRequest..? Pin
jschell21-Feb-12 9:41
jschell21-Feb-12 9:41 
GeneralRe: Any difference between Console App and Windows Service for HttpWebRequest..? Pin
Paladin200021-Feb-12 10:10
Paladin200021-Feb-12 10:10 
AnswerRe: Any difference between Console App and Windows Service for HttpWebRequest..? Pin
Dave Kreskowiak21-Feb-12 15:08
mveDave Kreskowiak21-Feb-12 15:08 
GeneralRe: Any difference between Console App and Windows Service for HttpWebRequest..? Pin
Paladin200022-Feb-12 3:34
Paladin200022-Feb-12 3:34 
AnswerRe: Any difference between Console App and Windows Service for HttpWebRequest..? [Eureka!] Pin
Paladin200022-Feb-12 10:40
Paladin200022-Feb-12 10:40 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.