如何使用matlab进行圆形裁剪? [英] How to do circular crop using matlab?
问题描述
我通常使用 imcrop
来裁剪rectanguler图像,但我想创建圆形裁剪。怎么做?
I usually use imcrop
to crop rectanguler image but i wat to create circular crop. How to do it?
我知道还有另外一个关于此的问题,这里是链接:
I know there is another quetion about this, here the link:
[xx,yy] = ndgrid((1:imageSize(1))-ci(1),(1:imageSize(2))-ci(2));
mask = (xx.^2 + yy.^2)<ci(3)^2;
以及 imshow(掩码)
的结果是带有白色背景的矩形
and the result of imshow(mask)
is rectangle with white background
错误 roimaskcc
http://www.mathworks.com/matlabcentral/newsreader/view_thread/242489
[xx,yy]=ndgrid(1:size(X,1), 1:size(X,2));
CroppingMask= ( (xx-Xcenter).^2+(yy-Ycenter).^2<=Radius^2 );
X=X.*CroppingMask;
imshow(CroppingMask)
的结果是矩形,黑色背景,矩形中心有小白色圆形。当我运行第3行时,它显示错误。
The result of imshow(CroppingMask)
is a rectangle with black background with small white circular at the center of the rectangle. When I run line 3 it shows an error.
请帮助我,一步一步,因为我是初学者。
Please help me, step by step because I'm a beginner.
这是我的形象: https://www.dropbox.com/s/5plqzqgyb1ej6gh/patricia.jpg 。分辨率为480x640。
Here is my image: https://www.dropbox.com/s/5plqzqgyb1ej6gh/patricia.jpg. It resolution is 480x640.
推荐答案
解决方案(1)运行良好。以下是使用您的图片的完整工作示例。
Solution (1) works perfectly well. Here is a complete working example using your image.
I = imread('patricia.jpg');
imageSize = size(I);
ci = [250, 300, 100]; % center and radius of circle ([c_row, c_col, r])
[xx,yy] = ndgrid((1:imageSize(1))-ci(1),(1:imageSize(2))-ci(2));
mask = uint8((xx.^2 + yy.^2)<ci(3)^2);
croppedImage = uint8(zeros(size(I)));
croppedImage(:,:,1) = I(:,:,1).*mask;
croppedImage(:,:,2) = I(:,:,2).*mask;
croppedImage(:,:,3) = I(:,:,3).*mask;
imshow(croppedImage);
它产生以下图像。
我希望这能澄清事情。可能有一种更好的方法来重新组合裁剪的图像,但这是我能想到的最好的方法。
I hope this clarifies things. There is probably a better way of re-assembling the cropped image, but this was what I could come up with off the top of my head.
这篇关于如何使用matlab进行圆形裁剪?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!