當前位置:編程學習大全網 - 編程語言 - 緊急求助!用matlab編程從圖片中提取最中間200*200部分,保存,還能將提取的部分放回去。非常感謝!

緊急求助!用matlab編程從圖片中提取最中間200*200部分,保存,還能將提取的部分放回去。非常感謝!

OrgImageName = 'board.tif';

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;

中間廢話比較多,妳撿重要的看吧,希望能夠幫到妳。

  • 上一篇:小學語文課文閱讀方法
  • 下一篇:深度學習和AI有什麽關系,學習什麽內容呢?
  • copyright 2024編程學習大全網