Emgu CV,无法将图像添加到Matrix进行KNN培训

时间:2015-06-25 04:33:56

标签: vb.net opencv matrix emgucv

我真的很难过这一点,任何帮助都会非常感激。我试图翻译以下内容:

https://github.com/MicrocontrollersAndMore/OpenCV_KNN_Character_Recognition_Machine_Learning/blob/master/generate_data.cpp

用C ++ OpenCV编写的

,最好用VB,但是C#也可以。我目前正在使用Emgu CV 2.4.10(暂停移至> = 3.X,直到Emgu 3.X通过发布候选阶段)。

我遇到麻烦的地方即将结束,在此矩阵被传递到KNN致电列车之前,必须将训练图像添加到OpenCV矩阵。这是我到目前为止在按钮单击事件中打开带有训练编号的文件:

Dim imgTrainingNumbers As Image(Of Bgr, Byte)
imgTrainingNumbers = New Image(Of Bgr, Byte)(ofdOpenFile.FileName)             'open image
'some error checking for verifying the image opened omitted here, its in the actual program


Dim imgGrayscale As Image(Of Gray, Byte)
Dim imgBlurred As Image(Of Gray, Byte)
Dim imgThresh As Image(Of Gray, Byte) = Nothing
Dim imgThreshCopy As Image(Of Gray, Byte)
Dim imgContours As Image(Of Gray, Byte)

Dim contours As Contour(Of Point)

Dim mtxClassificationInts As Matrix(Of Single) = New Matrix(Of Single)(NUMBER_OF_TRAINING_SAMPLES, 1)
Dim mtxTrainingImages As Matrix(Of Single) = New Matrix(Of Single)(RESIZED_IMAGE_WIDTH * RESIZED_IMAGE_HEIGHT * NUMBER_OF_TRAINING_SAMPLES, 1)

Dim intValidChars As New List(Of Integer)(New Integer() { 48, 49, 50, 51, 52, 53, 54, 55, 56, 57 })

imgGrayscale = imgTrainingNumbers.Convert(Of Gray, Byte)()             'convert to grayscale
imgBlurred = imgGrayscale.SmoothGaussian(5)

CvInvoke.cvShowImage("imgBlurred", imgBlurred)

imgThresh = imgBlurred.ThresholdAdaptive(New Gray(255), ADAPTIVE_THRESHOLD_TYPE.CV_ADAPTIVE_THRESH_GAUSSIAN_C, THRESH.CV_THRESH_BINARY_INV, 11, New Gray(2))

imgThreshCopy = imgThresh.Clone()

contours = imgThreshCopy.FindContours(CHAIN_APPROX_METHOD.CV_CHAIN_APPROX_SIMPLE, RETR_TYPE.CV_RETR_EXTERNAL)

imgContours = New Image(Of Gray, Byte)(imgThresh.Size())

CvInvoke.cvDrawContours(imgContours, contours, New MCvScalar(255), New MCvScalar(255), 100, 1, LINE_TYPE.CV_AA, New Point(0, 0))

CvInvoke.cvShowImage("imgThresh", imgThresh)
CvInvoke.cvShowImage("imgContours", imgContours)

While(Not contours Is Nothing)
    If (contours.Area > MIN_CONTOUR_AREA) Then
        Dim rect As Rectangle = contours.BoundingRectangle()            'get the bounding rect
        imgTrainingNumbers.Draw(rect, New Bgr(Color.Red), 2)            'draw red rect around the current char
        Dim imgROI As Image(Of Gray, Byte) = imgThresh.Copy(rect)

        Dim imgROIResized As Image(Of Gray, Byte) = imgROI.Resize(RESIZED_IMAGE_WIDTH, RESIZED_IMAGE_HEIGHT, INTER.CV_INTER_LINEAR)


        CvInvoke.cvShowImage("imgROI", imgROI)
        CvInvoke.cvShowImage("imgROIResized", imgROIResized)
        CvInvoke.cvShowImage("imgTrainingNumbers", imgTrainingNumbers)

        Dim intChar As Integer = CvInvoke.cvWaitKey(0)

        If (intChar = 27) Then
            'add code to exit program here if Esc is pressed
        ElseIf (intValidChars.Contains(intChar)) Then
            mtxClassificationInts.Add(intChar)      'append classification char to matrix of integers (we will convert later before writing to file)

            'now add the training image (some conversion is necessary first) . . .

            Dim mtxTemp As Matrix(Of Single) = New Matrix(Of Single)(imgROIResized.Size())
            Dim mtxTempReshaped As Matrix(Of Single) = New Matrix(Of Single)(imgROIResized.Size())

            CvInvoke.cvConvert(imgROIResized, mtxTemp)

            mtxTempReshaped = mtxTemp.Reshape(1, 1)

            Try
                mtxTrainingImages.Add(mtxTempReshaped)
            Catch ex As Exception
                txtInfo.Text = txtInfo.Text + ex.Message + vbCrLf
            End Try

        End If

    End If
    contours = contours.HNext
End While

Me.Text = "training complete !!"

'write mtxClassificationInts to file here
'write mtxTrainingImages to file here

'separate write and read into two separate programs when all this is working

'read mtxClassificationInts to file
'read mtxTrainingImages to file    

Dim kNearest As KNearest = New KNearest()                   'instantiate KNN object

kNearest.Train(mtxTrainingImages, mtxClassificationInts, Nothing, False, 1,False)       'call to train

'rest of program here when training is successful

在尝试中。 。 。 Catch block,就行了:

mtxTrainingImages.Add(mtxTempReshaped)

我收到以下错误:

OpenCV: The operation is neither 'array op array' (where arrays have the same size and the same number of channels), nor 'array op scalar', nor 'scalar op array'

我已经尝试过我可以找到的各种格式更改,但似乎无法在此行中遇到错误。

我应该提一些其他的事情:

- KNN对train的调用只接受Single类型的Matrix(如果使用#C则浮动,同样的事情),所以它必须采用这种格式

- 我得到了一个例子:

http://www.emgu.com/wiki/index.php/K_Nearest_Neighbors_in_CSharp

在C#和VB中工作,但我不确定如何将其应用于使用实际图像而不是随机编号

- 是的,我知道Emgu已经内置了Tesseract用于字符识别,但是我计划在Emgu中进行其他机器学习,并希望首先将其作为一个相对简单的示例

任何帮助都会很棒。

1 个答案:

答案 0 :(得分:0)

我相信错误消息告诉您mtxTrainingImages和mtxTempReshaped是不同的大小和/或具有不同数量的通道。如果这两个创建相同,则不会出现此问题。