简体   繁体   English

如何在matlab代码中裁剪图像中的矩形框

[英]How to crop rectangle box in image in matlab code

I want to crop a detected faces in my code. 我想在代码中裁剪出检测到的面孔。 Here is my code. 这是我的代码。

function DisplayDetections(im, dets)

imshow(im);

k = size(dets,1);

hold on;
for i=1:k
   rectangle('Position', dets(i,:),'LineWidth',2,'EdgeColor', 'r'); 

end
imcrop(rectangle);
hold off;

Their is syntax error in cropping. 它们是裁剪中的语法错误。 Can anybody help in cropping rectangle box detected in above box. 任何人都可以帮助裁剪在上面的框中检测到的矩形框。

That code only draws the rectangles in your image. 该代码仅在图像中绘制矩形。 If you actually want to crop out portions of the image with the defined rectangles, use imcrop . 如果您实际上想用定义的矩形裁剪出图像的某些部分,请使用imcrop

As such, you would do something like this to store all of your cropped rectangles. 这样,您将可以执行以下操作来存储所有裁剪后的矩形。 This is assuming that im and dets are already defined in your code from your function: 假设在函数中的代码中已经定义了imdets

k = size(dets,1);
cropped = cell(1,k);

for i=1:k
   cropped{k} = imcrop(im, dets(i,:));
end

cropped would be a cell array where each element will store a cropped image defined by each rectangle within your dets array. cropped将是一个单元格数组,其中每个元素将存储由dets数组中每个矩形定义的裁剪图像。 This is assuming that dets is a 2D array where there are 4 columns, and the number of rows determines how many rectangles you have. 这是假定dets是一个2列数组,其中有4列,并且行数决定了您有多少个矩形。 Each row of dets should be structured like: 每行dets结构应类似于:

[xmin ymin width height] 

xmin , ymin are the horizontal and vertical co-ordinate of the top-left corner of the rectangle, and width and height are the width and height of the rectangle. xminymin是矩形左上角的水平和垂直坐标, widthheight是矩形的宽度和高度。

If you want to access a cropped portion in the cell array, simply do: 如果要访问单元格数组中的裁剪部分,只需执行以下操作:

crp = cropped{k};

k would be the k th rectangle detected in your image. k将是图像中检测到的第k 矩形。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM