1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172
| clc clear all; global c_im; c_im = imread('D:\'); c_im = rgb2gray(c_im); figure,imshow(c_im);
pre;
R_im_new = uint8(ans); imgB = im2double(R_im_new);
gaussH=fspecial('gaussian',[5 5],10); smoothB=imfilter(imgB,gaussH); imshow(smoothB);
[t1B,t2B] =DoubleOtsuThresh(smoothB);
T1B = t1B; T2B = t2B; J9=img2gray(smoothB,T1B,T2B); figure; imshow(J9,[]);title('平滑+双阈值分割');
J10 = imfill(J9); figure(13),imshow(J10,[]) J11 = J10 +J9 ; figure(14),imshow(J11,[]);
I=double(J11); [m,n]=size(I); R = zeros(m,n); G = zeros(m,n); B = zeros(m,n); for i=1:m for j=1:n if I(i,j) ==0 R(i,j)=255; G(i,j)=255; B(i,j)=255; end if I(i,j)==3 R(i,j)=255; G(i,j)=0; B(i,j)=0; end if I(i,j)==2 R(i,j)=255; G(i,j)=0; B(i,j)=0; end if I(i,j)==1 R(i,j)=255; G(i,j)=0; B(i,j)=0; end if I(i,j) == 4 R(i,j)=0; G(i,j)=255; B(i,j)=0; end end end rgbim = zeros(m,n,3); for i=1:m for j=1:n rgbim(i,j,1)=R(i,j); rgbim(i,j,2)=G(i,j); rgbim(i,j,3)=B(i,j); end end rgbim=rgbim/256; subplot(1,2,1);imshow(c_im); subplot(1,2,2); imshow(rgbim); hold on ;
[L,num]=bwlabel(J9,8);
stats_1 = regionprops(L,'Area','PixelList','Centroid','BoundingBox');
Cen_1 = cat(1,stats_1.Centroid); figure(8),imshow(c_im); hold on ; for i=1:length(stats_1) plot(Cen_1(i,1), Cen_1(i,2), 'r+'); end
Temp_1 = cat(1,stats_1.BoundingBox); for i=1:length(stats_1) temp = Temp_1(i,:); rectangle('position',temp,'edgecolor','r'); end
for i = 1:length(stats_1) plot(Temp_1(i,1),Temp_1(i,2),'g'); text(stats_1(i).BoundingBox(1)-10,stats_1(i).BoundingBox(2),num2str(i),'Color','y','FontSize',12); end hold on;
[G,num] = bwlabel(J9,8); global R_im_f;
EZ = cell(num,1); for c = 1:num G = bwlabel(R_im_f,8); for i = 1:m for j = 1:n if G(i,j) ~= c G(i,j) = 0; end end end EZ{c,1} = G .* (1/c); end
HL = cell(num,1); for c = 1:num HL{c,1} = J9.* EZ{c,1}; end
HL_L = cell(num,1); for c = 1:num p = 0; q = 0; for i = 1:m for j = 1:n if HL{c,1} (i,j) == 1 p = p +1 ; end if HL{c,1}(i,j) == 2 q = q+1; end end end HL_L{c,1} = p/(p+q); end for c= 1:num GS(c,1) = HL_L{c,1}; end
figure(6),plot(GS(1:num,1),'r+'); hold on;
plot(GS(31:61,1),'g+'),hold on;
plot(GS(1:30,1)); hold on; plot(GS(31:61,1))
figure(7),scatter(1:num,GS,'r+');
|