Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

After finishing a few tutorials, I thought I was ready to calculate the pixel values of a large (smaller numbers 20x20 used here) image and export to a standard format (.jpg) but my code doesn't work. I am clearly missing something important. I thought it might be best to see if someone would look at a small segment of the code and comment.

(* image calc *)
numrow = 20;
numcol = 20;

(* midpoint of image *)
midrow = N[numrow/2];
midcol = N[numcol/2];

(* azimuth angle of pixel from center point *) 
(* mixed symbolic and numeric results *)
patfun[i_,j_] := N[ArcTan[i-midrow,j-midcol]];

(* spoke-like image *)
pat = Array[patfun[i,j], 20, 20];
share|improve this question
1  
This is just the simple stuff... you haven't yet shown where you're going wrong and how you failed at exporting. –  rm -rf Oct 30 '13 at 1:41
1  
There is another tutorial you should read: common pitfalls –  m_goldberg Oct 30 '13 at 2:14

4 Answers 4

Several coding problems addressed. Take a look:

hNumrow = hNumcol = 10;
patfun[i_, j_] := If[i - hNumrow == 0, Pi/2 Sign[j - hNumcol], ArcTan[i - hNumrow, j - hNumcol]];
pat = Rescale@Array[patfun[#1, #2] &, 2 {hNumrow, hNumcol}];
Image@pat
Export["\\yourFile.jpg", Image@pat]

Mathematica graphics

share|improve this answer

Here is another way to do it, which keeps the auxiliary variables localized.

pixelData =
  With[{numrow = 200, numcol = 200}, 
    With[{midrow = numrow/2, midcol = numcol/2},
      Block[{patfun},
        patfun[midrow, midcol] = 0.; 
        patfun[i_, j_] := N[ArcTan[i - midrow, j - midcol]];
        Table[patfun[i, j], {i, 0, numrow}, {j, 0, numcol}]]]];
img = Image @ Rescale @ pixelData

image.png

share|improve this answer

Here's another way: Especially for listable built in functions like ArcTan using (packed) arrays for coordinates might be faster than calling ArcTan for every single pixel.

dim={20,20};
x=Array[#1&,dim,{{-1.,1.},{-1.,1.}}];
y=Array[#2&,dim,{{-1.,1.},{-1.,1.}}];
img=Image[Rescale[ArcTan[x,y]]]

enter image description here

Export["filename here",img];

(And to prevent the division by zero at x/y=0, if the dimensions aren't even, I usually just cheat and add a small number like 10^-10 or $MachineEpsilon to x or y.)

share|improve this answer

To supplement the other answers: by adding a semicolon at the end of every line, you sometimes don't spot the errors soon enough. For example, after:

pat = Array[patfun[i, j], {20, 20}]

you would have seen a list of

{{ArcTan[-10. + i, -10. + j][1, 1], ...

where the i and j are hanging around like uninvited guests.

After 'correcting' this line to:

pat = Array[patfun, {20, 20}]

you see an error message:

ArcTan::indet: Indeterminate expression ArcTan[0.,0.] encountered. >>

and, halfway down (if you've left the semicolon off):

{-1.5708, -1.5708, -1.5708, -1.5708, -1.5708, -1.5708, -1.5708, -1.5708,
 -1.5708, Indeterminate, 1.5708, 1.5708, 1.5708, 1.5708, 1.5708, 1.5708, 1.5708,
 1.5708, 1.5708, 1.5708},

and that Indeterminate will frustrate a subsequent attempt to apply the Image function to your data. You could, I suppose, do this:

Image[pat /. Indeterminate -> 0]

but I think the other solutions here - avoiding errors rather than cleaning up after them - are better.

share|improve this answer
1  
+1, but don't do this for images from a typical megapixel camera. I regularly loose hours of unsaved work by doing just that... –  nikie Oct 30 '13 at 9:24

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.