3
$\begingroup$

I've been trying to write a program that could plot a unit circle into an image. The idea is using Arrayplot to plot the Part of pixels that fall into a disk area that radius less equal to 1.

img2 = N@Map[255 - Mean[#] &, 
ExampleData[{"TestImage", "Lena"}, "Data"], {2}];
{W, H} = Dimensions[img2];
i = Range[W]; j = Range[H];
xi = (2 i - W - 1)/W; yj = (2 j - H - 1)/H;  
radius = Sqrt[xi^2 + yj^2] ;(*These two lines are mapping each pixel to unit disk, just igrone the pixels fall out unit circle i.e. value less than -1 or greater than 1. *)
transimg2 = img2[[Round[Cases[radius, c_ /; -1 <= c <= 1]]]];
ArrayPlot[transimg2, Frame -> False]

Then got the error:

ArrayPlot::mat: Argument {{92.3333,92.3333,90.6667,92.6667,93.6667,98.,92.,93.3333,90.3333,95.,92.,95.,101.333,93.6667,95.6667,100.333,100.667,100.,94.6667,96.6667,102.,101.667,103.333,100.333,102.,99.3333,101.333,105.667,97.6667,101.333,99.,100.667,103.333,99.6667,101.667,95.,98.3333,90.,99.,89.6667,91.6667,93.,91.6667,87.,87.3333,83.6667,86.,88.,86.6667,87.3333,<<462>>},<<49>>,<<312>>} at position 1 is not a list of lists.

Please help to give some advice what may the problem in my code, thanks in advance!

$\endgroup$
3
  • $\begingroup$ Did you verify each line of code was correct before you went on to the next? That is the way to debug code like yours. $\endgroup$
    – m_goldberg
    Commented Jun 13, 2017 at 22:51
  • $\begingroup$ Are you trying to do something like: With[{img=ExampleData[{"TestImage","Lena"}]}, ImageMultiply[ img,Graphics[{White,Disk[]},Background->None,ImageSize->ImageDimensions[img]]] ]? $\endgroup$
    – Carl Woll
    Commented Jun 13, 2017 at 22:58
  • $\begingroup$ How about: ArrayPlot[DiskMatrix[100]] $\endgroup$
    – bill s
    Commented Jun 13, 2017 at 23:00

2 Answers 2

2
$\begingroup$

this does more or less what you appear to be trying to do:

img2 = N@Map[255 - Mean[#] &, 
    ExampleData[{"TestImage", "Lena"}, "Data"], {2}];
{W, H} = Dimensions[img2];
transimg2 = 
  Outer[If[Norm[{#1, #2} - {W, H}/2] < 100, img2[[#1, #2]], 0] &, 
   Range[W], Range[H]];
ArrayPlot[transimg2]

enter image description here

this is much faster to use matrix multiplication:

  ArrayPlot[DiskMatrix[100, {W, H}] img2]

same result

using only image functions:

ImageCrop@
   ColorNegate@
    ImageMultiply[ColorNegate@#, 
     Image@DiskMatrix[100, ImageDimensions@#]] &@
 ColorConvert[ExampleData[{"TestImage", "Lena"}], "GrayScale"]

(without the twice ColorNegate you end up with a black background )

$\endgroup$
2
  • $\begingroup$ Thanks very much, 100 is the just the half of original image, so that it should be altered to other value. Am i right? $\endgroup$
    – cj9435042
    Commented Jun 21, 2017 at 20:13
  • $\begingroup$ 100 is the radius of the circle in pixel coordinates, so use whatever you need. $\endgroup$
    – george2079
    Commented Jun 21, 2017 at 20:18
1
$\begingroup$

Expanding my comment into an answer. Perhaps:

With[{img = ColorConvert[ExampleData[{"TestImage","Lena"}], "Grayscale"]},
    ImageMultiply[
        img,
        Graphics[{White,Disk[]},Background->None]
    ]
]

enter image description here

or

With[{img = ImageCrop[ColorConvert[ExampleData[{"TestImage","Lena"}], "Grayscale"], 250]},
    ImageMultiply[
        img,
        Graphics[{White,Disk[]},Background->None]
    ]
]

enter image description here

$\endgroup$
3
  • $\begingroup$ Thanks for the input very much! I think the effect is similar to what I want, but I still want to let the program decides which pixels are belonging to the disk and which are not. So it could generate more precise result I think. $\endgroup$
    – cj9435042
    Commented Jun 21, 2017 at 20:16
  • $\begingroup$ It is simple to control the size of the disk, for example, using Graphics[{White, Disk[]}, PlotRange->2, Background->None] will make the disk half the size of the image. $\endgroup$
    – Carl Woll
    Commented Jun 21, 2017 at 20:26
  • $\begingroup$ Also note that my approach with a Graphics disk produces an antialiased disk, whereas using a DiskMatrix disk produces a non-antialiased disk. $\endgroup$
    – Carl Woll
    Commented Jun 21, 2017 at 20:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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