Drawing a square with a circle using a function.

조회 수: 9 (최근 30일)
Caitlin Schmidt
Caitlin Schmidt 2019년 7월 21일
답변: Jyothis Gireesh 2019년 8월 2일
How would I write a function with the requirements down below:
The function takes at least one input argument: the size of the square-shaped image in number of pixels. The default color for the square is black, and the default color for the circular disk is white.
If a second input argument is provided, it should be the color of the circular disk. If a third input argument is provided, it will be the color of the square.
I need some overall help on where to get started. I'm not sure how I should add in the white circle, as well as how to change the colors.
I have tried using a function(can no longer remember) that would allow me to insert a circle, but I do not have the proper toolbox for it. I have created an all black matrix of however many pixels are entered by the user. Adding the circle is where I am currently stuck. Any suggestions would be helpful.
This is what I have created so far:
function week7hw3(pixels,varargin)
mat=zeros(pixels,pixels);
n=nargin;
if n==1
image(mat)
elseif n==2
end
end
Any help is appreciated!
  댓글 수: 3
Caitlin Schmidt
Caitlin Schmidt 2019년 7월 21일
I have looked at the code and this is what I have come up with:
function week7hw3(pixels,varargin)
n=nargin;
mat=zeros(pixels,pixels,3);
hold on
[columnsInImage rowsInImage] = meshgrid(1:pixels, 1:pixels);
centerX = pixels/2;
centerY = pixels/2;
radius = pixels/2;
circlepixels=(rowsInImage-centerY).^2+(columnsInImage-centerX).^2 <=radius.^2;
hold off
if n==1
image(mat)
elseif n==2
circlepixels=colormap(varargin{1});
image(mat)
elseif n==3
mat=colormap(varargin{2});
circlepixels=colormap(varargin{1});
image(mat)
end
end
I'm only getting a black figure window, any ideas of where I have gone wrong?
Walter Roberson
Walter Roberson 2019년 7월 21일
Your mat should be uint8.
In the case of no extra arguments, you display mat, but mat is all zeros.
In the case of one extra argument, you activate a colormap according to the input, and then display mat, but mat is all zeros.
In the case of two extra arguments, you activate a colormap according to the input, and then display mat, but mat is all zeros and you ignore the first extra argument.
"If a second input argument is provided, it should be the color of the circular disk."
Okay, but in what form is it being passed in? Is it being passed in as the name of a colormap such as 'prism' ? Is it being passed in as the short name of a color, such as 'r' ? Is it being passed in as the name of one of the built-in colors, such as 'red' ? Is it being passed in as the name of a general color such as 'seafoam green' ? Is it being passed in as an rgb vector?

댓글을 달려면 로그인하십시오.

답변 (1개)

Jyothis Gireesh
Jyothis Gireesh 2019년 8월 2일
I am assuming that the background and foreground colors are given in the RGB format and the order of function arguments are as (Square size, color of circle, color of square).
To determine the circular region, it may be helpful to use the Euclidean distance between the indexes of the center pixel and other pixels and compare it with half the length of the side of the square.
The circle’s color can be modified by initializing a 3D matrix with the R, G and B components in each channel. Multiply the image with the mask to determine the region of interest.
A similar method can be used to create the background region also. Combine these to get the final image.
This code may help you.
function y = argtest(varargin)
x = varargin;
[X, Y] = meshgrid(linspace(-1, 1, x{1}));
R = hypot(X, Y);
y = 255*(R<1);
if nargin>1
fore_ground = x{2};
matrix_fore = cat(3,fore_ground(1)*ones(x{1}),fore_ground(2)*ones(x{1}),fore_ground(3)*ones(x{1}));
matrix_fore = matrix_fore.*repmat((R<1),[1 1 3]);
y = matrix_fore;
if nargin == 3
back_ground = x{3};
matrix_back = cat(3,back_ground(1)*ones(x{1}),back_ground(2)*ones(x{1}),back_ground(3)*ones(x{1}));
matrix_back = matrix_back.*repmat((R>=1),[1,1,3]);
y = matrix_fore + matrix_back;
end
end
end

카테고리

Help CenterFile Exchange에서 Modify Image Colors에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by