Message Boards Message Boards

0
|
6560 Views
|
5 Replies
|
14 Total Likes
View groups...
Share
Share this post:

Crop a circle region from an image faster?

Posted 5 years ago

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?

POSTED BY: Shaohui Yin
5 Replies

Try

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

assuming im = Image[img].

POSTED BY: Szabolcs Horvát

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 BY: Henrik Schachner

Another example you can try:

POSTED BY: Shenghui Yang

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 BY: Henrik Schachner
Posted 5 years ago

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

POSTED BY: Shaohui Yin
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