问题
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.
来源:https://stackoverflow.com/questions/61842824/what-is-the-difference-between-cropping-an-image-and-applying-an-roi-region-of