gpt4 book ai didi

vb.net - Emgu CV,无法将图像添加到矩阵以进行 KNN 训练

转载 作者:行者123 更新时间:2023-12-02 16:39:04 25 4
gpt4 key购买 nike

我真的很难过这一点,任何帮助将不胜感激。我正在尝试翻译以下内容:

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

它是用 C++ OpenCV 编写的,到 Emgu CV,最好使用 VB,但 C# 也可以。我目前正在使用 Emgu CV 2.4.10(推迟到 >= 3.X 直到 Emgu 3.X 通过发布候选阶段)。

我遇到麻烦的地方是接近尾声,必须将训练图像添加到 OpenCV 矩阵中,然后才能将该矩阵传递给 KNN 调用进行训练。这是我到目前为止在按钮单击事件中打开带有培训编号的文件的内容:

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

在尝试。 . .捕获 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 中的其他机器学习,并希望首先让这个工作作为一个相对简单的例子

任何帮助都会很棒。

最佳答案

我相信错误消息告诉您 mtxTrainingImages 和 mtxTempReshape 是不同的大小和/或具有不同数量的 channel 。如果这两个创建相同,您将不会遇到此问题。

关于vb.net - Emgu CV,无法将图像添加到矩阵以进行 KNN 训练,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31041266/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com