圖像分割

function digitalDivide_Callback(hObject, eventdata, handles)
% hObject    handle to digitalDivide (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
global Img_noNoise
global Img_Divide
global t1
global n
global isnoNoise
global isDivide
isDivide = 0;
    Img_Divide = Img_noNoise;
    [rowBit, colBit] = find(Img_Divide == 1); %找到字符的最大範圍 rowBit與colBit都是與字符像素點個數相同的一維數組 
    imin=min(rowBit);
    imax=max(rowBit);
    jmin=min(colBit);
    jmax=max(colBit);
    %下面的代碼用於標識出每個字符的範圍
    flag = false;%flag 用作是否進入一個字符分割的標誌
    k = 1;
    for j = jmin : jmax + 1
        if (max(size(find(Img_Divide(imin : imax, j) == 1))) - 1) == 0 %在第j列中沒有找到像素爲1(白點, 代表字符)的點
            if flag == true;
                t1(1, k) = j - 1;  %t1的第一行偶數記錄分割數字的右邊界
                k = k + 1;
                flag = false;
            end     
            
        else%在第j列中存在像素爲1(白點,代表字符)的點
            if flag == false
                flag = true;
                t1(1, k) = j; %t1的第一行奇數記錄分割數字的左邊界
                k = k + 1;
            end
        end
    end
    n = max(size(t1)) / 2;%m爲待識別數字的個數
    for i = 1 : n   
        j = 2 * i;
        for k = imin : imax%由上到下尋找上邊界
             if (max(size(find(Img_noNoise(k, t1(1, j - 1) : t1(1, j)) == 1))) - 1) > 0% 在對應的列中找到了分割數字的上邊界
                 t1(2,j - 1) = k;  %t1的第二行奇數列分別記錄分割數字的上邊界
                 break;
             end
        end
    end

    for i = 1 : n   
        j = 2 * i;
        for k = imax : -1 : imin%由下到上尋找下邊界
             if (max(size(find(Img_noNoise(k, t1(1, j - 1) : t1(1, j)) == 1))) -1) > 0% 在對應的列中找到了分割數字的下邊界
                 t1(2, j) = k;  %t1的第二行偶數列分別記錄分割數字的下邊界
                 break;
             end
        end
    end

axes(handles.axes1);
imshow(Img_noNoise)
hold on 
for i = 1 : n
    j = 2 * i;
    plot([t1(1, j-1), t1(1, j)], [t1(2, j-1), t1(2, j-1)], 'red');
    plot([t1(1, j-1), t1(1, j)], [t1(2, j), t1(2, j)], 'red');
    plot([t1(1, j-1), t1(1, j-1)], [t1(2, j-1), t1(2, j)],'red');
    plot([t1(1, j), t1(1, j)], [t1(2, j-1), t1(2, j)], 'red');
end
hold off


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章