图像分割,特征提取
[解决办法]
如果你的图都和这张类似的话,要切割还挺简单的
1 : 取threshold,产生binary image
2 : 找contours
3 : 挑选area最大的contour(I)
4 : 根据I取bounding rect
5 : 选取bounding rect所在的区间
struct maxContours
{
bool operator()(std::vector<cv::Point> const &a, std::vector<cv::Point> const &b) const
{
return cv::contourArea(a) < cv::contourArea(b);
}
};
try{
cv::Mat input = cv::imread("/Users/Qt/program/blogsCodes/pic/csdn_char_5.jpg", CV_LOAD_IMAGE_GRAYSCALE);
cv::Mat binary;
cv::threshold(input, binary, 0, 255, CV_THRESH_BINARY_INV + CV_THRESH_OTSU);
using Points = std::vector<cv::Point>;
std::vector<Points> contours;
cv::findContours(binary.clone(), contours, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_NONE);
auto it = std::max_element(std::begin(contours), std::end(contours), maxContours());
cv::Rect const mask_rect = cv::boundingRect(*it);
cv::imshow("", binary(mask_rect));
cv::waitKey();
}catch(std::exception const &ex)
{
std::cerr<<ex.what()<<std::endl;
}
Dim FPFN As String 'FullPathFileName
Dim i As Integer
Dim t As Integer
Dim n As Integer
Dim x As Integer
Dim x1 As Integer
Dim y As Integer
Dim w As Integer
Dim h As Integer
Dim b As Long
Dim a As Boolean
Dim nm(9) As String
Dim s As String
Dim r As String
Private Sub Form_Load()
FPFN = Command$
If Left(FPFN, 1) = Chr(34) Then FPFN = Mid(FPFN, 2)
If Right(FPFN, 1) = Chr(34) Then FPFN = Left(FPFN, Len(FPFN) - 1)
If Not FileExists(FPFN) Then
logtofile "[" + FPFN + "] file not find!"
End 'form
End If
nm(0) = _
"--OOO---" + _
"-OO-OO--" + _
"-O---O--" + _
"OO---OO-" + _
"OO---OO-" + _
"OO---OO-" + _
"OO---OO-" + _
"OO---OO-" + _
"OO---OO-" + _
"-O---O--" + _
"-OO-OO--" + _
"--OOO---"
nm(1) = _
"--OO----" + _
"OOOO----" + _
"--OO----" + _
"--OO----" + _
"--OO----" + _
"--OO----" + _
"--OO----" + _
"--OO----" + _
"--OO----" + _
"--OO----" + _
"--OO----" + _
"OOOOOO--"
nm(2) = _
"--OOOO--" + _
"-O--OOO-" + _
"O----OO-" + _
"-----OO-" + _
"-----OO-" + _
"-----O--" + _
"----OO--" + _
"----O---" + _
"---O----" + _
"--O----O" + _
"-OOOOOOO" + _
"OOOOOOO-"
nm(3) = _
"--OOOO--" + _
"-O--OOO-" + _
"O----OO-" + _
"-----OO-" + _
"----OO--" + _
"---OOO--" + _
"----OOO-" + _
"-----OO-" + _
"-----OO-" + _
"-----OO-" + _
"OO--OO--" + _
"OOOOO---"
nm(4) = _
"-----OO-" + _
"-----OO-" + _
"----OOO-" + _
"---O-OO-" + _
"--O--OO-" + _
"--O--OO-" + _
"-O---OO-" + _
"O----OO-" + _
"OOOOOOOO" + _
"-----OO-" + _
"-----OO-" + _
"-----OO-"
nm(5) = _
"--OOOO--" + _
"--OOOO--" + _
"-O------" + _
"-OOO----" + _
"OOOOO---" + _
"---OOO--" + _
"----OO--" + _
"-----O--" + _
"-----O--" + _
"-----O--" + _
"O---O---" + _
"OOOO----"
nm(6) = _
"-----OOO" + _
"---OOO--" + _
"--OO----" + _
"-OO-----" + _
"-O-OOO--" + _
"OOO--OO-" + _
"OO----OO" + _
"OO----OO" + _
"OO----OO" + _
"OO----OO" + _
"-OO--OO-" + _
"--OOOO--"
nm(7) = _
"-OOOOOOO" + _
"-OOOOOO-" + _
"O-----O-" + _
"-----O--" + _
"-----O--" + _
"-----O--" + _
"----O---" + _
"----O---" + _
"---O----" + _
"---O----" + _
"---O----" + _
"--O-----"
nm(8) = _
"--OOOO--" + _
"-OO---OO" + _
"OO----OO" + _
"OO----OO" + _
"-OOO-OO-" + _
"--OOO---" + _
"--OOOO--" + _
"-O---OO-" + _
"OO----OO" + _
"OO----OO" + _
"-OO--OO-" + _
"--OOOO--"
nm(9) = _
"--OOOO--" + _
"-OO--OO-" + _
"OO----OO" + _
"OO----OO" + _
"OO----OO" + _
"OO----OO" + _
"-OO---OO" + _
"--OOOOO-" + _
"-----OO-" + _
"----OO--" + _
"---OO---" + _
"OOO-----"
Timer1.Enabled = True
End Sub
Private Function recognize() As String
t = 3 '上面3行空白
w = 8 '每个数字宽8像素
h = 12 '每个数字高12像素
x1 = 0
r = "xxxx"
On Error GoTo RERR
Picture1.Picture = LoadPicture(FPFN)
For n = 0 To 3
Do
For y = 0 To h - 1
b = (Picture1.Point(x1, t + y)) And 255
If b < 127 Then Exit For
Next
If y <= h - 1 Then Exit Do
x1 = x1 + 1
If x1 >= 70 Then
Debug.Print "找不到下一个数字对应的x1"
recognize = r
Label1.Caption = r
Exit Function
End If
Loop
s = ""
For y = 0 To h - 1
For x = 0 To w - 1
'Debug.Print " "; Right("00000000" + Hex(Picture1.Point(n * w + x, t + y)), 8);
b = (Picture1.Point(x1 + x, t + y)) And 255
'Debug.Print " "; Right("00" + Hex(b), 2);
If b > 127 Then
Debug.Print "-";
s = s + "-"
Else
Debug.Print "O";
s = s + "O"
End If
'Picture1.PSet (x1 + x, t + y), RGB(255, 0, 0)
Next
Debug.Print
Next
Debug.Print
x1 = x1 + w
For i = 0 To 9
If nm(i) = s Then
Mid(r, n + 1, 1) = CStr(i)
End If
Next
Next
RERR:
recognize = r
Label1.Caption = r
Exit Function
End Function
Private Sub Timer1_Timer()
Dim f As Integer
On Error Resume Next
If LCase(Right(FPFN, 4)) = ".bmp" Then
f = FreeFile()
Open FPFN + ".txt" For Output As #f
Print #f, recognize()
Close #f
End
Else
logtofile "[" + FPFN + "] is not .bmp file!"
End If
End Sub
Public Function FileExists(filename) As Boolean
Dim msg As String
On Error GoTo CheckError
FileExists = (Dir(filename) <> "")
Exit Function
CheckError:
Const mnErrDiskNotReady = 71, mnErrDeviceUnavailable = 68
If (Err.Number = mnErrDiskNotReady) Then
msg = "将软盘插入驱动器 "
msg = msg + ",然后关好驱动器门。"
If MsgBox(msg, vbExclamation + vbOKCancel) = vbOK Then
Resume
Else
FileExists = False
Exit Function
End If
ElseIf Err.Number = mnErrDeviceUnavailable Then
msg = "找不到: "
msg = msg + filename
MsgBox msg, vbExclamation
FileExists = False
Exit Function
Else
FileExists = False
Exit Function
'msg = "出现 #" + str(Err.Number)
'msg = msg + " 意外错误: " + Err.Description
'MsgBox msg, vbCritical
'End'Form
End If
FileExists = False
Exit Function
End Function
Private Sub logtofile(s As String)
Dim f As Integer
On Error Resume Next
f = FreeFile()
Open App.Path + "\numeye1.log" For Append As #f
Print #f, Format(Now, "YYYY-MM-DD hh:mm:ss") + " " + s
Close #f
End Sub