0

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

Warren Hill
  • 3,092

2 Answers2

0
for i=1:M
   for j=1:N
     if  i>=a&&i<b
     I(i,j)=i;
   end 
end
0
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
John Hughes
  • 93,729