Image 在matlab中获取图像的中心像素
我想提取一个图像的大小为Image 在matlab中获取图像的中心像素,image,matlab,roi,Image,Matlab,Roi,我想提取一个图像的大小为(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 a
(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命令可以获得相同的结果?也许只需指定矩形的大小和图像中心?如果您有,您可以使用函数和一些数学:
[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, :);
如果您有,您可以使用函数和一些数学:
[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, :);
出于好奇,你提供的东西有什么问题?即使Matlab有一个内置的函数来做你想做的事情,它也可能和这个没有太大的不同。实际上什么都没有。但对我来说,这似乎是一个“简单”任务的代码。我只是想知道是否有可能用更少的代码行完成这项工作。出于好奇,您提供的有什么问题吗?即使Matlab有一个内置的函数来做你想做的事情,它也可能和这个没有太大的不同。实际上什么都没有。但对我来说,这似乎是一个“简单”任务的代码。我只是想知道是否有可能用更少的代码行完成这项工作。