Message Boards Message Boards

GROUPS:

Crop a circle region from an image faster?

Posted 1 month ago
284 Views
|
5 Replies
|
14 Total Likes
|

First, I used the Outer function to generate an image.

img = Abs[Cos[\[Pi]^2 Outer[Plus, Range[-1, 1, 0.002]^2, 2 Range[-1, 1, 0.002]^2]]]; // AbsoluteTiming
Image[img]

enter image description here Then, I set the region outside the unit disk equals 0. This step costs 4.6045 seconds.

{Nx, Ny} = Dimensions[img]
imgCircle = Table[If[(i - Nx/2)^2 + (j - Ny/2)^2 > (Nx/2)^2, f[i, j] = 0, f[i, j] = img[[i, j]]], {i, 1, Nx}, {j, 1, Ny}]; // AbsoluteTiming

enter image description here Is there a faster way to get the round sub-area of an image?

5 Replies

This most likely is the optimal solution! Somewhere in the fog I was aware of DiskMatrix, but one is always prone to "invent the wheel twice" ...

Try

im Image@DiskMatrix[ImageDimensions[im]/2, ImageDimensions[im]]

assuming im = Image[img].

Another example you can try:

Posted 1 month ago

Thank you. It is forty times faster than my method.

I did not do any timings, but maybe this is pointing into the right direction. I am applying a mask made out of a graphics:

img = Image@
   Abs[Cos[\[Pi]^2 Outer[Plus, Range[-1, 1, 0.002]^2, 
       2 Range[-1, 1, 0.002]^2]]];

dims = ImageDimensions[img];
mask = Image[
   Graphics[{White, Disk[{0, 0}, Min[dims]/2]}, Background -> Black, 
    PlotRangePadding -> None], ImageSize -> dims];

imgCrop = img mask
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract