yins
(Shaohui Yin)
1
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?
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
yins
(Shaohui Yin)
3
Thank you. It is forty times faster than my method.
shenghuiy
(Shenghui Yang)
4
Another example you can try:
szhorvat
(Szabolcs Horvát)
5
Try
im Image@DiskMatrix[ImageDimensions[im]/2, ImageDimensions[im]]
assuming im = Image[img].
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” …