Crop a circle region from an image faster?

Posted 8 months ago
1180 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]  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  Is there a faster way to get the round sub-area of an image?
5 Replies
Sort By:
Posted 8 months ago
 Try im Image@DiskMatrix[ImageDimensions[im]/2, ImageDimensions[im]] assuming im = Image[img].
Posted 8 months ago
 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 
Posted 8 months ago
 Another example you can try:
Posted 8 months ago
 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" ...
Posted 8 months ago
 Thank you. It is forty times faster than my method.
Community posts can be styled and formatted using the Markdown syntax.