0

I'm using opencv.

Having a base image (640x640) I want to know if applying a rectangular white shape mask of (100x100), in another words, a ROI is more time consuming compared to cropping the image in the same rectangular shape (100x100).

What I consider as being a ROI

mask = np.zeros_like(original_image)
shape = np.array([[a,b,c,d]])
cv2.fillPoly(mask, shape, (255,255,255))
cv2.bitwise_and(original_image, mask)

The way of cropping

cropped = original_image[x:y, z:t]

I want to apply a function (e.g transform to grayscale) or apply a color filter on the image.

I think that doing so on the ROIed image will be more time consuming as there are many more pixels (the resulting image has the same dimensions as the original one (640x640)).

On the other hand, applying some function on the cropped image will take less time compared to the latter.

The question is: wouldn't the operation of cropping the image compensate the time needed to compute the rest of the 640x640 - 100x100 pixels?

Hope I was clear enough. Thanks in advance


Edit

I did use timeit as Belal Homaidan suggested:

print("CROP TIMING: ", timeit.timeit(test_crop, setup=setup, number=10000) * 1e6, "us")
print("ROI  TIMING: ", timeit.timeit(test_ROI, setup=setup, number=10000) * 1e6, "us")

Where setup was:

setup = """\
import cv2
import numpy as np
original_image = cv2.imread(r"C:\\Users\\path\\path\\test.png")
"""

Where test_crop was:

test_crop = """\
cropped = original_image[0:150, 0:150]
"""

And where test_ROI was:

test_ROI = """\
mask = np.zeros_like(original_image)
shape = np.array([[(0,0),(150,0),(150,150),(0,150)]])
cv2.fillPoly(mask, shape, (255,255,255))
final = cv2.bitwise_and(original_image, mask)
"""

The image was a BGR one with a size of 131 KB and a resolution of 384x208 pixels.

The result was:

CROP TIMING:  11560.400000007576 us
ROI  TIMING:  780580.8000000524 us

I think that doing a ROI on an image fits when you want a specific shape, and cropping would best fit when you need rectangular shapes.


Cătălina Sîrbu
  • 1,253
  • 9
  • 30
  • One difference you'll have, right off the bat, is that by processing a cropped image, you'll have less pixels to deal with. Depending on what you are doing, by operating on less pixels, you'll see a big impact in your processing chain. In regards to the actual cropping operation. I can only speak for the C++ implementation, but the operation is pretty fast. It is achieved using the same Mat information (no new data is created) and adjusting pointers at the beginning and end of the new size. – stateMachine May 16 '20 at 23:49
  • *"I want to know if **first** is more time consuming compared to **second**"* you can time it, and compare the time.[reference](https://docs.python.org/3/library/timeit.html) – Bilal May 17 '20 at 02:06
  • So basically it doesn't matter that many pixels are black, it matters their number. **I will ask another thing** : *It is possible to crop an image in another shape other than rectangles?* Since I can't find any usefull usage for the ROI I provided in the question. It looks like cropping is better. – Cătălina Sîrbu May 17 '20 at 06:34
  • 1
    @BelalHomaidan, the time is fluctuating. I don't have a constant result, and I believe this happends due to the interpreter or processor queue. There is some priority hierarchy... – Cătălina Sîrbu May 17 '20 at 10:16
  • 1
    @CătălinaSîrbu _So basically it doesn't matter that many pixels are black, it matters their number._ That’s correct. By processing fewer pixels, you pretty much are reducing the data that will go thru your operations. _It is possible to crop an image in another shape other than rectangles?_ No, however, you can create ROIs out of polygons, (where the pixel data will be enclosed by a polygon, and the rest will be masked) but the actual crop (the matrix shape containing your pixel data) will always be M x N. The former can be achieved by using _poly points_ to draw a polygonal mask. – stateMachine May 17 '20 at 21:37
  • 1
    @CătălinaSîrbu Check out this post: https://stackoverflow.com/questions/48301186/cropping-concave-polygon-from-image-using-opencv-python Maybe your optimal solution involves combining the 2 concepts. Share some details if you can. – stateMachine May 17 '20 at 21:38

0 Answers0