matlab圆形裁剪图片
时间: 2023-10-20 14:09:20 浏览: 178
您可以使用MATLAB中的imcrop函数来实现圆形裁剪图片。具体步骤如下:
1. 读取图片,例如使用imread函数。
2. 创建一个圆形掩膜,可以使用MATLAB中的meshgrid函数和欧几里得距离公式来实现。例如:
[x,y] = meshgrid(1:size(img,2),1:size(img,1));
center_x = round(size(img,2)/2); % 圆心横坐标
center_y = round(size(img,1)/2); % 圆心纵坐标
radius = 100; % 圆的半径
mask = (x-center_x).^2 + (y-center_y).^2 <= radius^2;
3. 将掩膜应用到原始图像上,可以使用MATLAB中的bsxfun函数来实现。例如:
img_crop = bsxfun(@times, img, cast(mask,class(img)));
完整代码示例:
img = imread('example.jpg');
[x,y] = meshgrid(1:size(img,2),1:size(img,1));
center_x = round(size(img,2)/2);
center_y = round(size(img,1)/2);
radius = 100;
mask = (x-center_x).^2 + (y-center_y).^2 <= radius^2;
img_crop = bsxfun(@times, img, cast(mask,class(img)));
阅读全文
相关推荐


















