?? imsplit.m
字號:
function objs = imsplit( img, lightback, do_rot, thresh )% Image Split, (Adam Meadows: ameadows@cs.ucr.edu)% objs = imsplit( img, lightback, do_rot, thresh )% % IMG is the input image% LIGHTBACK is a flag specifying that the background is lighter than the foreground% DO_ROT is a flag to enable/disable rotation of the split images to "face"% same way% THRESH is a threshold to use when creating BW image (if 0, automatic)%% OBJS returns a cell array containing the individual images for each obj%%RGB = imread(filename);RGB = img;I = rgb2gray(RGB);if thresh == 0 thresh = graythresh(I);endbw = im2bw(I,thresh);%if the background is light, swap the bw imageif(lightback == 1) bw = swap(bw);end% remove all object containing fewer than 30 pixelsbw = bwareaopen(bw,30);% fill any holes, so that regionprops can be used to estimate% the area enclosed by each of the boundariesbw = imfill(bw,'holes');%find background colorbackground = imbackground(img, lightback);r = background(1,1,1);g = background(1,1,2);b = background(1,1,3);%[B,L] = bwboundaries(bw,'noholes');bwl = bwlabel(bw);bbs = regionprops(bwl, 'BoundingBox');os = regionprops(bwl, 'Orientation');cent = regionprops(bwl, 'Centroid');dims = size(img);for k = 1:length(bbs) %crop it (leaving a 5 pixel border so that the shape can be extracted) newBB = bbs(k).BoundingBox; newBB = [newBB(1)-5, newBB(2)-5, newBB(3)+10, newBB(4)+10]; %make sure it's just this object myBW = bwselect(bw, cent(k).Centroid(1), cent(k).Centroid(2)); myRGB = RGB; myRGB(:,:,1) = immultiply(RGB(:,:,1), myBW); myRGB(:,:,2) = immultiply(RGB(:,:,2), myBW); myRGB(:,:,3) = immultiply(RGB(:,:,3), myBW); cropped = imcrop(myRGB, newBB); %cropped = imcrop(RGB,newBB); if do_rot %rotate it so that major axis is parallel with image's major axis rotangle = 180 - os(k).Orientation; usey = 0; if(dims(1) > dims(2)) rotangle = rotangle + 90; usey = 1; end cropped = imrotate(cropped, rotangle); csize = size(cropped); for i=1:csize(1) for j=1:csize(2) if(cropped(i,j,:) == 0) cropped(i,j,:) = [ r g b ]; end; end; end; %now, re-crop it Ic = rgb2gray(cropped); %threshc = graythresh(Ic); bwc = im2bw(Ic,thresh); %if the background is light, swap the bw image if(lightback == 1) bwc = swap(bwc); end bwcl = bwlabel(bwc); bbsc = regionprops(bwcl, 'BoundingBox'); centc = regionprops(bwcl, 'Centroid'); newCent = centc(1).Centroid; newBBc = bbsc(1).BoundingBox; newBBc = [newBBc(1)-5, newBBc(2)-5, newBBc(3)+10, newBBc(4)+10]; cropped = imcrop(cropped, newBBc); %see if it's better the other way if usey if newCent(2)-newBBc(2) < newBBc(4)/2 %Y values upside down cropped = imrotate(cropped, 180); end else if newCent(1)-newBBc(1) > newBBc(3)/2 cropped = imrotate(cropped,180); end end else csize = size(cropped); for i=1:csize(1) for j=1:csize(2) if(cropped(i,j,:) == 0) cropped(i,j,:) = [ r g b ]; end; end; end; end %end of if do_rot objs{k} = cropped; end %of function
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -