從圖像
回答
裁剪很容易,所有你需要做的是應用一個適當的面具。訣竅是創建這樣一個面具。
假設A
是你的形象,試試這個:
%# Create an ellipse shaped mask
c = fix(size(A)/2); %# Ellipse center point (y, x)
r_sq = [76, 100] .^ 2; %# Ellipse radii squared (y-axis, x-axis)
[X, Y] = meshgrid(1:size(A, 2), 1:size(A, 1));
ellipse_mask = (r_sq(2) * (X - c(2)) .^ 2 + ...
r_sq(1) * (Y - c(1)) .^ 2 <= prod(r_sq));
%# Apply the mask to the image
A_cropped = bsxfun(@times, A, uint8(ellipse_mask));
裁切的圖像將被存儲在A_cropped
。 玩中心的座標和半徑的值,直到你得到所需的結果。
編輯:我擴展了RGB圖像的解決方案(如果矩陣A
是3-D)。
我試過你的代碼。它給最後一行提供了錯誤:A_cropped(ellipse_mask)= A; ???在賦值A(:) = B中,A和B 中的元素數量必須相同。 – user671805
你說得對。我修好了它。 –
您的代碼現在可以使用。但現在我只是得到一個完全不透明的紅色橢圓,背景爲黑色。 – user671805
這是我用來將面裁剪成橢圓形的方法。它使背景透明。
[FileName,PathName] = uigetfile({'*.jpg;*.tif;*.png;*.gif','All Image Files'},'Please Select an Image');
image = imread([PathName FileName]);
imshow(image) %needed to use imellipse
user_defined_ellipse = imellipse(gca, []); % creates user defined ellipse object.
wait(user_defined_ellipse);% You need to click twice to continue.
MASK = double(user_defined_ellipse.createMask());
new_image_name = [PathName 'Cropped_Image_' FileName];
new_image_name = new_image_name(1:strfind(new_image_name,'.')-1); %removing the .jpg, .tiff, etc
new_image_name = [new_image_name '.png']; % making the image .png so it can be transparent
imwrite(image, new_image_name,'png','Alpha',MASK);
msg = msgbox(['The image was written to ' new_image_name],'New Image Path');
waitfor(msg);
- 1. 圖像分類:從圖像
- 2. Android圖像從URL到圖像視圖
- 3. 從圖像
- 4. 從圖像
- 5. UITabBarItem從圖像
- 6. 從圖像
- 7. 從圖像
- 8. 從uitabbaritem圖像
- 9. 從圖像
- 10. UIBezierPath從圖像
- 11. HBITMAP從圖像
- 12. 從圖像
- 13. 從圖像javacv
- 14. 從圖像
- 15. 我從圖像
- 16. 從圖像URL
- 17. 從圖像
- 18. 從圖像
- 19. 從圖像
- 20. 從圖像
- 21. 圖像從PHP
- 22. 從圖像
- 23. 從圖像URL下載圖像到圖像視圖
- 24. 將圖像從解析圖像加載到圖像視圖
- 25. 從圖像id中獲取圖像android
- 26. SWT:從圖像中獲取子圖像
- 27. 從圖像數組中播放圖像
- 28. 從2張圖像創建圖像
- 29. 從圖像組中選擇圖像
- 30. 圖像從鏈接圖像中顯示?
請詳細說明,舉例,展示圖片等...... –
您是否想手動或自動找到臉? –
橢圓是如何給出的?是用'imellipse'插入的嗎?你會知道它的幾何形狀(位置+主軸和副軸)嗎?你可以在生成的'imellipse'對象上使用'createMask'方法。 –