簡體   English   中英

在Matlab中獲取圖像的中心像素

[英]Get center pixels of image in matlab

我想提取圖像大小(p*hight,p*width,3)的矩形區域。 p是[0,1]之間的雙精度值。

以下代碼有效,但是我想知道是否有更好的方法來實現這一目標?

img = imread(ImageName);

% size parameter
p = 0.5;

% store image size
hight = size(img,1);
width = size(img,2);

% calculate the center of the image both in width and hight
% used as reference
centerHight = floor(hight/2);
centerWidth = floor(width/2);

% use half of the actual size of the rectangular region
halfHight = floor(p*hight/2);
halfWidth = floor(p*width/2);

% start index for hight and width
startHight = 1 + centerHight - halfHight;
startWidth = 1 + centerWidth - halfWidth;

% end index for hight and width
endHight = centerHight + halfHight;
endWidth = centerWidth + halfWidth;

% extract center pixels
CenterPixels = img(startHight:endHight,startWidth:endWidth,:);

是否有任何matlab命令可獲得相同的結果? 也許只指定矩形的大小和圖像中心?

如果您擁有圖像處理工具箱 ,則可以使用imcrop函數和一些數學運算:

[nl, nc, ~] = size(img);
CenterPixels = imcrop(img, [[nc nl] * (1 - p) / 2 [nc nl] * p]);

編輯:或者您可以這樣做:

[nl, nc, ~] = size(img);
CenterPixels  = img(nl*(1-p)/2:nl*(1+p)/2, nc*(1-p)/2:nc*(1+p)/2, :);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM