matlabを使用して画像に枠を描き、保存します.

2535 ワード

clear all
close all
clc
for k=16 
p=k;
q=int2str(p);
path='E:\CellDetection\';
path1='E:\just_draw\';
I=imread(strcat(path,num2str(k),'.jpg'));
img=imread(strcat(path1,num2str(k),'.jpg'));%               ,           ,
for j=1:50
figure(1),imshow(I)
[I2,rect] = imcrop(I);         %          ;
X1=rect(1);%x         x;
X2=rect(2);%y         y;
w=rect(3); %w              w;
h=rect(4); %h              h;
X3=X1+w;   %   x  
X4=X2+h;   %   y  
x1=ceil(X1);x2=ceil(X2);x3=ceil(X3);x4=ceil(X4);%      ,         
%% 
%      :          
%img: input color image  
rgb = [255 0 0];    
  
[imgH,imgW,imgC] = size(img);%imgH:     ,  ;imgW:     ,  ;imgC:      

if size(img,3) == 3 %draw on color image   size(img,1):     ;size(img,2):     ;size(img,3):         
    for M=1:3    
            if( x1>=0 && x2>=0 && x3<=imgW && x4<=imgH ) 
                
                %top line      , img   ( x2 , x1   x3 , M )    rgb(1,M);
                img( x2,x1:x3,M ) = rgb(1,M);   
                img( x2+1,x1:x3,M ) = rgb(1,M);%          
                
                %bottom line      , img   ( x4 , x1   x3 , M )    rgb(1,M);  
                img( x4,x1:x3,M ) = rgb(1,M);  
                img( x4-1,x1:x3,M ) = rgb(1,M);  

%left line , img ( x2 x4 , x1 , M ) rgb(1,M); img( x2:x4, x1,M ) = rgb(1,M);%left line img( x2:x4, x1+1,M ) = rgb(1,M); %left line , img ( x2 x4 , x3 , M ) rgb(1,M); img( x2:x4, x3,M ) = rgb(1,M);%right line img( x2:x4, x3-1,M ) = rgb(1,M); end end endif size(img,3) ~= 3 % if( x1>=0 && x2>=0 && x3<=imgW && x4<=imgH ) %top line , img ( x2 , x1 x3 , M ) rgb(1,M); img( x2,x1:x3) = rgb(1,1); img( x2+1,x1:x3) = rgb(1,1);% %bottom line , img ( x4 , x1 x3 , M ) rgb(1,M); img( x4,x1:x3) = rgb(1,1); img( x4-1,x1:x3) = rgb(1,1); %left line , img ( x2 x4 , x1 , M ) rgb(1,M); img( x2:x4,x1) = rgb(1,1);%left line img( x2:x4,x1+1) = rgb(1,1); %left line , img ( x2 x4 , x3 ) rgb(1,M); img( x2:x4,x3) = rgb(1,1);%right line img( x2:x4,x3-1) = rgb(1,1); endendfigure(5),imshow(img); endend