我真的很为难,任何帮助将不胜感激。我正在尝试翻译以下内容:
https://github.com/MicrocontrollersAndMore/OpenCV_KNN_Character_Recognition_Machine_Learning/blob/master/generate_data.cpp
最好用VB用C++ OpenCV编写到Emgu CV,但是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
在Try中。 。 。捕获块,在行上:
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调用仅接受类型为Single的Matrix(如果使用#C,则为同种类型,则为float),因此必须采用这种格式
我有个例子:
http://www.emgu.com/wiki/index.php/K_Nearest_Neighbors_in_CSharp
可以在C#和VB中使用,但是我不确定如何将其应用于实际图像而不是由随机数组成
-是的,我知道Emgu内置了用于字符识别的Tesseract,但我打算继续在Emgu中进行其他机器学习,并希望首先使这一工作成为一个相对简单的示例
任何帮助都会很棒。
最佳答案
我相信错误消息告诉您mtxTrainingImages和mtxTempReshaped的大小不同和/或具有不同的通道数。如果这两个是相同创建的,则不会有此问题。
关于vb.net - Emgu CV,无法将图像添加到矩阵以进行KNN训练,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/31041266/