i4=im2double(J);
[L,num]=bwlabel(i4,8); % 區域標記
figure,imagesc(L);axis tight;axis equal; %標記顏色顯示
[r,c] = find(L == 1);%調用標記區域
MIN1 = min(c)-5; %計算橫最小坐標點
MIN2 = min(r)-5; %計算縱最小坐標點
MAX1 = max(c)+5; %計算橫最大坐標點
MAX2 = max(r)+5; %計算縱最大坐標點
%數值檢查,顯示初步框選區域
if(MIN1 > 0)
MMIN1=MIN1;
else
MMIN1 = 1;
end
if(MIN2 > 0)
MMIN2=MIN2;
else
MMIN2 = 1;
end
[mm nn] = size(i4);
if(MAX1 < nn)
MMAX1=MAX1;
else
MMAX1 = nn-1;
end
if(MAX2 < mm)
MMAX2=MAX2;
else
MMAX2 = mm-1;
end
cxk=i4(MMIN2:MMAX2,MMIN1:MMAX1); %將初步框選區域保存
%figure,imshow(1-cxk); %顯示初步框選區域
%邊緣檢測
[BW1,thresh1]=edge(cxk,'roberts'); %Roberts邊緣算子
[BW2,thresh2]=edge(cxk,'sobel'); %Sobel邊緣算子
[BW3,thresh3]=edge(cxk,'prewitt'); %Prewitt邊緣算子
subplot(2,2,1);
imshow(cxk); %顯示初步框選區域
subplot(2,2,2);
imshow(BW1); %顯示Roberts邊緣算子結果
subplot(2,2,3);
imshow(BW2); %顯示Sobel邊緣算子結果
subplot(2,2,4);
imshow(BW3); %顯示Prewitt邊緣算子結果
CXKK=im2double(BW1);
%投影
im=CXKK;
[m n]=size(im); % m n分別為圖像的行 列數
% 進行垂直投影
for y=1:n
V1(y)=sum(im(1:m,y));
end
% 進行水平投影
for x=1:m
V2(x)=sum(im(x,:));
end
%投影結果
Q1=im2bw(V1); %算長
i1=sum(Q1,2)-1;
Q2=im2bw(V2); %算寬
i2=sum(Q2,2)-1;
%計算四點坐標
[LL,num2]=bwlabel(BW1,8); % 區域標記
[rr,cc] = find(LL == 1);%調用標記區域
MINN1=min(rr)+MIN1; %橫最小
MINN2=min(cc)+MIN2+4; %縱最小
aaaaa(1,:)=[MINN1;MINN2]; %左上點坐標
bbbbb(1,:)=[MINN1+i1;MINN2]; %右上點坐標
ccccc(1,:)=[MINN1;MINN2+i2]; %左下點坐標
ddddd(1,:)=[MINN1+i1;MINN2+i2]; %右下點坐標
imshow(1-i4) % 顯示二值化圖像
hold on
plot([MINN1 MINN1+i1],[MINN2 MINN2],'m','LineWidth',2)
plot([MINN1+i1 MINN1+i1],[MINN2 MINN2+i2],'m','LineWidth',2)
plot([MINN1+i1 MINN1],[MINN2+i2 MINN2+i2],'m','LineWidth',2)
plot([MINN1 MINN1],[MINN2+i2 MINN2],'m','LineWidth',2)
plot([MINN1 MINN1+i1],[MINN2 MINN2],'mo','LineWidth',2)
plot([MINN1+i1 MINN1+i1],[MINN2 MINN2+i2],'mo','LineWidth',2)
plot([MINN1+i1 MINN1],[MINN2+i2 MINN2+i2],'mo','LineWidth',2)
plot([MINN1 MINN1],[MINN2+i2 MINN2],'mo','LineWidth',2)
V1=0;
V2=0;
Q1=0;
Q2=0;
i1=0;
i2=0;
%2222222222
[r,c] = find(L == 2);%調用標記區域
MIN1 = min(c)-5; %計算橫最小坐標點
MIN2 = min(r)-5; %計算縱最小坐標點
MAX1 = max(c)+5; %計算橫最大坐標點
MAX2 = max(r)+5; %計算縱最大坐標點
%數值檢查,顯示初步框選區域
if(MIN1 > 0)
MMIN1=MIN1;
else
MMIN1 = 1;
end
if(MIN2 > 0)
MMIN2=MIN2;
else
MMIN2 = 1;
end
[mm nn] = size(i4);
if(MAX1 < nn)
MMAX1=MAX1;
else
MMAX1 = nn-1;
end
if(MAX2 < mm)
MMAX2=MAX2;
else
MMAX2 = mm-1;
end
cxk=i4(MMIN2:MMAX2,MMIN1:MMAX1); %將初步框選區域保存
%figure,imshow(1-cxk); %顯示初步框選區域
%邊緣檢測
[BW1,thresh1]=edge(cxk,'roberts'); %Roberts邊緣算子
[BW2,thresh2]=edge(cxk,'sobel'); %Sobel邊緣算子
[BW3,thresh3]=edge(cxk,'prewitt'); %Prewitt邊緣算子
%subplot(2,2,1);
%imshow(cxk); %顯示初步框選區域
%subplot(2,2,2);
%imshow(BW1); %顯示Roberts邊緣算子結果
%subplot(2,2,3);
%imshow(BW2); %顯示Sobel邊緣算子結果
%subplot(2,2,4);
%imshow(BW3); %顯示Prewitt邊緣算子結果
CXKK=im2double(BW1);
%投影
im=CXKK;
[m n]=size(im); % m n分別為圖像的行 列數
% 進行垂直投影
for y=1:n
V1(y)=sum(im(1:m,y));
end
% 進行水平投影
for x=1:m
V2(x)=sum(im(x,:));
end
%投影結果
Q1=im2bw(V1); %算長
i1=sum(Q1,2);
Q2=im2bw(V2); %算寬
i2=sum(Q2,2);
%計算四點坐標
[LL,num2]=bwlabel(BW1,8); % 區域標記
[rr,cc] = find(LL == 1);%調用標記區域
MINN1=MIN1+min(rr)+3; %橫最小
MINN2=MIN2+min(cc); %縱最小
aaaaa(2,:)=[MINN1;MINN2]; %左上點坐標
bbbbb(2,:)=[MINN1+i1;MINN2]; %右上點坐標
ccccc(2,:)=[MINN1;MINN2+i2]; %左下點坐標
ddddd(2,:)=[MINN1+i1;MINN2+i2]; %右下點坐標
hold on
plot([MINN1 MINN1+i1],[MINN2 MINN2],'m','LineWidth',2)
plot([MINN1+i1 MINN1+i1],[MINN2 MINN2+i2],'m','LineWidth',2)
plot([MINN1+i1 MINN1],[MINN2+i2 MINN2+i2],'m','LineWidth',2)
plot([MINN1 MINN1],[MINN2+i2 MINN2],'m','LineWidth',2)
plot([MINN1 MINN1+i1],[MINN2 MINN2],'mo','LineWidth',2)
plot([MINN1+i1 MINN1+i1],[MINN2 MINN2+i2],'mo','LineWidth',2)
plot([MINN1+i1 MINN1],[MINN2+i2 MINN2+i2],'mo','LineWidth',2)
plot([MINN1 MINN1],[MINN2+i2 MINN2],'mo','LineWidth',2)
V1=0;
V2=0;
Q1=0;
Q2=0;
i1=0;
i2=0;
%3333333333333
[r,c] = find(L == 4);%調用標記區域
MIN1 = min(c)-5; %計算橫最小坐標點
MIN2 = min(r)-5; %計算縱最小坐標點
MAX1 = max(c)+5; %計算橫最大坐標點
MAX2 = max(r)+5; %計算縱最大坐標點
%數值檢查,顯示初步框選區域
if(MIN1 > 0)
MMIN1=MIN1;
else
MMIN1 = 1;
end
if(MIN2 > 0)
MMIN2=MIN2;
else
MMIN2 = 1;
end
[mm nn] = size(i4);
if(MAX1 < nn)
MMAX1=MAX1;
else
MMAX1 = nn-1;
end
if(MAX2 < mm)
MMAX2=MAX2;
else
MMAX2 = mm-1;
end
cxk=i4(MMIN2:MMAX2,MMIN1:MMAX1); %將初步框選區域保存
%figure,imshow(1-cxk); %顯示初步框選區域
%邊緣檢測
[BW1,thresh1]=edge(cxk,'roberts'); %Roberts邊緣算子
[BW2,thresh2]=edge(cxk,'sobel'); %Sobel邊緣算子
[BW3,thresh3]=edge(cxk,'prewitt'); %Prewitt邊緣算子
%subplot(2,2,1);
%imshow(cxk); %顯示初步框選區域
%subplot(2,2,2);
%imshow(BW1); %顯示Roberts邊緣算子結果
%subplot(2,2,3);
%imshow(BW2); %顯示Sobel邊緣算子結果
%subplot(2,2,4);
%imshow(BW3); %顯示Prewitt邊緣算子結果
CXKK=im2double(BW1);
%投影
im=CXKK;
[m n]=size(im); % m n分別為圖像的行 列數
% 進行垂直投影
for y=1:n
V1(y)=sum(im(1:m,y));
end
% 進行水平投影
for x=1:m
V2(x)=sum(im(x,:));
end
%投影結果
Q1=im2bw(V1); %算長
i1=sum(Q1,2)-1;
Q2=im2bw(V2); %算寬
i2=sum(Q2,2)-1;
%計算四點坐標
[LL,num2]=bwlabel(BW1,8); % 區域標記
[rr,cc] = find(LL == 1);%調用標記區域
MINN1=min(rr)+MIN1; %橫最小
MINN2=min(cc)+MIN2; %縱最小
aaaaa(3,:)=[MINN1;MINN2]; %左上點坐標
bbbbb(3,:)=[MINN1+i1;MINN2]; %右上點坐標
ccccc(3,:)=[MINN1;MINN2+i2]; %左下點坐標
ddddd(3,:)=[MINN1+i1;MINN2+i2]; %右下點坐標
hold on
plot([MINN1 MINN1+i1],[MINN2 MINN2],'m','LineWidth',2)
plot([MINN1+i1 MINN1+i1],[MINN2 MINN2+i2],'m','LineWidth',2)
plot([MINN1+i1 MINN1],[MINN2+i2 MINN2+i2],'m','LineWidth',2)
plot([MINN1 MINN1],[MINN2+i2 MINN2],'m','LineWidth',2)
plot([MINN1 MINN1+i1],[MINN2 MINN2],'mo','LineWidth',2)
plot([MINN1+i1 MINN1+i1],[MINN2 MINN2+i2],'mo','LineWidth',2)
plot([MINN1+i1 MINN1],[MINN2+i2 MINN2+i2],'mo','LineWidth',2)
plot([MINN1 MINN1],[MINN2+i2 MINN2],'mo','LineWidth',2)
V1=0;
V2=0;
Q1=0;
Q2=0;
i1=0;
i2=0;
%44444444
[r,c] = find(L == 3);%調用標記區域
MIN1 = min(c)-5; %計算橫最小坐標點
MIN2 = min(r)-5; %計算縱最小坐標點
MAX1 = max(c)+5; %計算橫最大坐標點
MAX2 = max(r)+5; %計算縱最大坐標點
%數值檢查,顯示初步框選區域
if(MIN1 > 0)
MMIN1=MIN1;
else
MMIN1 = 1;
end
if(MIN2 > 0)
MMIN2=MIN2;
else
MMIN2 = 1;
end
[mm nn] = size(i4);
if(MAX1 < nn)
MMAX1=MAX1;
else
MMAX1 = nn-1;
end
if(MAX2 < mm)
MMAX2=MAX2;
else
MMAX2 = mm-1;
end
cxk=i4(MMIN2:MMAX2,MMIN1:MMAX1); %將初步框選區域保存
% figure,imshow(1-cxk); %顯示初步框選區域
%邊緣檢測
[BW1,thresh1]=edge(cxk,'roberts'); %Roberts邊緣算子
[BW2,thresh2]=edge(cxk,'sobel'); %Sobel邊緣算子
[BW3,thresh3]=edge(cxk,'prewitt'); %Prewitt邊緣算子
%subplot(2,2,1);
%imshow(cxk); %顯示初步框選區域
%subplot(2,2,2);
%imshow(BW1); %顯示Roberts邊緣算子結果
%subplot(2,2,3);
%imshow(BW2); %顯示Sobel邊緣算子結果
%subplot(2,2,4);
%imshow(BW3); %顯示Prewitt邊緣算子結果
CXKK=im2double(BW1);
%投影
im=CXKK;
[m n]=size(im); % m n分別為圖像的行 列數
% 進行垂直投影
for y=1:n
V1(y)=sum(im(1:m,y));
end
% 進行水平投影
for x=1:m
V2(x)=sum(im(x,:));
end
%投影結果
Q1=im2bw(V1); %算長
i1=sum(Q1,2);
Q2=im2bw(V2); %算寬
i2=sum(Q2,2)-1;
%計算四點坐標
[LL,num2]=bwlabel(BW1,8); % 區域標記
[rr,cc] = find(LL == 1);%調用標記區域
MINN1=MMIN1+min(cc); %橫最小
MINN2=MMIN2+min(rr); %縱最小
aaaaa(4,:)=[MINN1;MINN2]; %左上點坐標
bbbbb(4,:)=[MINN1+i1;MINN2]; %右上點坐標
ccccc(4,:)=[MINN1;MINN2+i2]; %左下點坐標
ddddd(4,:)=[MINN1+i1;MINN2+i2]; %右下點坐標
hold on
plot([MINN1 MINN1+i1],[MINN2 MINN2],'m','LineWidth',2)
plot([MINN1+i1 MINN1+i1],[MINN2 MINN2+i2],'m','LineWidth',2)
plot([MINN1+i1 MINN1],[MINN2+i2 MINN2+i2],'m','LineWidth',2)
plot([MINN1 MINN1],[MINN2+i2 MINN2],'m','LineWidth',2)
plot([MINN1 MINN1+i1],[MINN2 MINN2],'mo','LineWidth',2)
plot([MINN1+i1 MINN1+i1],[MINN2 MINN2+i2],'mo','LineWidth',2)
plot([MINN1+i1 MINN1],[MINN2+i2 MINN2+i2],'mo','LineWidth',2)
plot([MINN1 MINN1],[MINN2+i2 MINN2],'mo','LineWidth',2)
V1=0;
V2=0;
Q1=0;
Q2=0;
i1=0;
i2=0;
%555555555555
[r,c] = find(L == 5);%調用標記區域
MIN1 = min(c)-5; %計算橫最小坐標點
MIN2 = min(r)-5; %計算縱最小坐標點
MAX1 = max(c)+5; %計算橫最大坐標點
MAX2 = max(r)+5; %計算縱最大坐標點
%數值檢查,顯示初步框選區域
if(MIN1 > 0)
MMIN1=MIN1;
else
MMIN1 = 1;
end
if(MIN2 > 0)
MMIN2=MIN2;
else
MMIN2 = 1;
end
[mm nn] = size(i4);
if(MAX1 < nn)
MMAX1=MAX1;
else
MMAX1 = nn-1;
end
if(MAX2 < mm)
MMAX2=MAX2;
else
MMAX2 = mm-1;
end
cxk=i4(MMIN2:MMAX2,MMIN1:MMAX1); %將初步框選區域保存
%figure,imshow(1-cxk); %顯示初步框選區域
%邊緣檢測
[BW1,thresh1]=edge(cxk,'roberts'); %Roberts邊緣算子
[BW2,thresh2]=edge(cxk,'sobel'); %Sobel邊緣算子
[BW3,thresh3]=edge(cxk,'prewitt'); %Prewitt邊緣算子
%subplot(2,2,1);
%imshow(cxk); %顯示初步框選區域
%subplot(2,2,2);
%imshow(BW1); %顯示Roberts邊緣算子結果
%subplot(2,2,3);
%imshow(BW2); %顯示Sobel邊緣算子結果
%subplot(2,2,4);
%imshow(BW3); %顯示Prewitt邊緣算子結果
CXKK=im2double(BW1);
%投影
im=CXKK;
[m n]=size(im); % m n分別為圖像的行 列數
% 進行垂直投影
for y=1:n
V1(y)=sum(im(1:m,y));
end
% 進行水平投影
for x=1:m
V2(x)=sum(im(x,:));
end
%投影結果
Q1=im2bw(V1); %算長
i1=sum(Q1,2)-1;
Q2=im2bw(V2); %算寬
i2=sum(Q2,2);
%計算四點坐標
[LL,num2]=bwlabel(BW1,8); % 區域標記
[rr,cc] = find(LL == 1);%調用標記區域
MINN1=min(cc)+MMIN1; %橫最小
MINN2=min(rr)+MMIN2; %縱最小
aaaaa(5,:)=[MINN1;MINN2]; %左上點坐標
bbbbb(5,:)=[MINN1+i1;MINN2]; %右上點坐標
ccccc(5,:)=[MINN1;MINN2+i2]; %左下點坐標
ddddd(5,:)=[MINN1+i1;MINN2+i2]; %右下點坐標
hold on
plot([MINN1 MINN1+i1],[MINN2 MINN2],'m','LineWidth',2)
plot([MINN1+i1 MINN1+i1],[MINN2 MINN2+i2],'m','LineWidth',2)
plot([MINN1+i1 MINN1],[MINN2+i2 MINN2+i2],'m','LineWidth',2)
plot([MINN1 MINN1],[MINN2+i2 MINN2],'m','LineWidth',2)
plot([MINN1 MINN1+i1],[MINN2 MINN2],'mo','LineWidth',2)
plot([MINN1+i1 MINN1+i1],[MINN2 MINN2+i2],'mo','LineWidth',2)
plot([MINN1+i1 MINN1],[MINN2+i2 MINN2+i2],'mo','LineWidth',2)
plot([MINN1 MINN1],[MINN2+i2 MINN2],'mo','LineWidth',2)
hold off
|