OrgImage = imread(OrgImageName);
[RowSize ColSize ChanSize] = size(OrgImage);
CutRowSize = 200;
CutColSize = 200;
if (RowSize < CutRowSize)
error(message(strcat('Please choice the right image which row size is bigger than ',num2str(CutRowSize))));
end
if (ColSize < CutColSize)
error(message(strcat('Please choice the right image which column size is bigger than ',num2str(CutColSize))));
end
%計算起始行列,並剪切圖片
StartRow = floor(RowSize-CutRowSize)/2+1;StartCol = floor(ColSize-CutColSize)/2+1;
CutImage = OrgImage(StartRow:StartRow+CutRowSize-1,StartCol:StartCol+CutColSize-1,:);
%保存圖片
[pathstr, name, ext] = fileparts(OrgImageName);
CutImageName = strcat(name,'_Cut_',num2str(CutRowSize),'x',num2str(CutColSize),ext);
imwrite(CutImage,CutImageName,ext(2:end));
%讀回圖片
ReplaceImageName = CutImageName;
ReplaceImage = imread(ReplaceImageName);
[RpRowSize RpColSize RpChanSize] = size(ReplaceImage);
if RpRowSize ~= CutRowSize
error(message(strcat('Please choice the right image which row size is equal to ',num2str(CutRowSize))));
end
if (RpColSize ~= CutColSize)
error(message(strcat('Please choice the right image which column size is equal to ',num2str(CutColSize))));
end
if (RpChanSize ~= ChanSize)
error(message(strcat('Please choice the right image which channel size is equal to ',num2str(ChanSize))))
end
%將讀回圖片覆蓋原圖片中間的位置
OrgImage(StartRow:StartRow+CutRowSize-1,StartCol:StartCol+CutColSize-1,:) = ReplaceImage;
中間廢話比較多,妳撿重要的看吧,希望能夠幫到妳。