필터 지우기
필터 지우기

How to make circular black frame in image?

조회 수: 6 (최근 30일)
andrew_cup
andrew_cup 2012년 11월 9일
Hi, I'm trying to make a circular black frame on an image in matlab. I have figured out how to make a rectangular frame, but I can't figure how to make a circular frame. Heres the code:
im = imread('michelle_pf.jpeg');
imshow(im)
[h,w,kanaler] = size(im);
im_k = im(1:min(h,w),1:min(h,w),:);
figure(2),clf
imshow(im_k)
[h,w,kanaler] = size(im_k);
im_g = rgb2gray(im_k);
im_d = im2double(im_g);
storleik_ramme = 50;
for i=1:h
for j=1:w
if i<storleik_ramme || j<storleik_ramme || j>w-storleik_ramme || i>h-storleik_ramme
im_d(i,j)=0.0;
end
end
end
figure(3),clf
imshow(im_d)

답변 (1개)

Image Analyst
Image Analyst 2012년 11월 9일
편집: Image Analyst 2012년 11월 9일
See my demo. It's not really so complicated. I basically just fancied up the short snippet in the FAQ. http://matlab.wikia.com/wiki/FAQ#How_do_I_create_a_circle.3F So, just copy, paste, and run.
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
imtool close all; % Close all imtool figures.
clear; % Erase all existing variables.
workspace; % Make sure the workspace panel is showing.
format longg;
format compact;
fontSize = 20;
% Read in a standard MATLAB color demo image.
folder = fullfile(matlabroot, '\toolbox\images\imdemos');
baseFileName = 'peppers.png';
% Get the full filename, with path prepended.
fullFileName = fullfile(folder, baseFileName);
if ~exist(fullFileName, 'file')
% Didn't find it there. Check the search path for it.
fullFileName = baseFileName; % No path this time.
if ~exist(fullFileName, 'file')
% Still didn't find it. Alert user.
errorMessage = sprintf('Error: %s does not exist.', fullFileName);
uiwait(warndlg(errorMessage));
return;
end
end
rgbImage = imread(fullFileName);
% Display the original image.
subplot(2, 2, 1);
imshow(rgbImage);
title('Original Color Image', 'FontSize', fontSize);
[rows columns numberOfColorChannels] = size(rgbImage);
% Enlarge figure to full screen.
set(gcf, 'units','normalized','outerposition',[0 0 1 1]);
% Create a logical image of a circle with specified
% diameter, center, and image size.
% First create the image.
[columnsInImage rowsInImage] = meshgrid(1:columns, 1:rows);
% Next create the circle in the center of the image.
centerX = columns/2;
centerY = rows/2;
radius = min([rows columns]) / 2;
circlePixels = (rowsInImage - centerY).^2 ...
+ (columnsInImage - centerX).^2 <= radius.^2;
% circlePixels is a 2D "logical" array.
% Now, display it.
subplot(2, 2, 2);
imshow(circlePixels, []);
colormap([0 0 0; 1 1 1]);
title('Binary image of a circle', 'FontSize', fontSize);
% Mask the image.
maskedRgbImage = bsxfun(@times, rgbImage, cast(circlePixels, class(rgbImage)));
% Now, display it.
subplot(2, 2, 3);
imshow(maskedRgbImage);
title('Image Masked by the Circle', 'FontSize', fontSize);
% Mask the image the other way.
maskedRgbImage = bsxfun(@times, rgbImage, cast(~circlePixels, class(rgbImage)));
% Now, display it.
subplot(2, 2, 4);
imshow(maskedRgbImage);
title('Image Masked by the Circle', 'FontSize', fontSize);

카테고리

Help CenterFile Exchange에서 Image Processing Toolbox에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by