How to create an image from this function?

80 Views Asked by At

this is a newbie question. I would like to create an image that intensity obey to the following function g() :

$$ g(x,y) = \begin{cases} -10, & x \in [-50, -10) \\ x, & x \in [-10, 10) \\ 10, & x \in [10, 50] \end{cases} $$

for $y = 0$ to $100$

Here what I have done so far :

I = zeros(10,10)
I(7:10,:) = 10
I(0:4,:) = -10
imwrite(I,'g.jpeg')

But I don't know how to do : g(x,y)=x ?? any help is a welcome

2

There are 2 best solutions below

0
On BEST ANSWER
n = 101;
u = 51; % offset for indexing
v = zeros(1,n)
v(u + (-50:-10)) = -10
v(u + (-10:10)) = (-10):10
v(u + (10:50)) = 10
I = repmat(v, n, 1); % replicate v, n times vertically, once horizontally 
imshow((I+ 10) / 20); % matlab expects image values to range from 0 to 1!

Note that I've used 101 places in the vector $v$ so that my indexing and your formula match nicely. There are also no loops in this program, which will make it much more efficient than matlab code with loops (or at least that's a general principle to live by; in this case, the code's so short it's probably moot.)

Here's an alternative, more "matlab-esque" version

n = 101;
v = zeros(1,n)
v1 = -10 * ones(n, 40);
v2 = repmat(-10:10, n, 1);
v3 = 10 * ones(n, 40);
I = [v1, v2, v3];
imshow((I+ 10) / 20); % matlab expects image values to range from 0 to 1!
figure(gcf); % bring the figure to the foreground

And here's one last version, in a rather terser matlab style:

n = 101;
I = min(10, max(-10, repmat(-50:50, 101, 1)));
imshow((I+ 10) / 20); % matlab expects image values to range from 0 to 1!
figure(gcf); % bring the figure to the foreground
0
On
for i=1:M
   for j=1:N
     if  i>=a&&i<b
     I(i,j)=i;
   end 
end