Cv2 resize image4/2/2023 ![]() Inv_scale_factor_x = original_width/new_width Inv_scale_factor_y = original_height/new_height Original_height, original_width = image_matrix.shape Output_image = numpy.zeros((new_height, new_width), dtype=image_matrix.dtype) """Perform a pure-numpy linear-resampled resize of an image.""" import mathĭef resize_linear(image_matrix, new_height:int, new_width:int): ![]() I offer this under Apache or MIT license at the discretion of the user. ![]() If you're only rescaling smaller images, it should be fine. It does not make good use of Numpy and, thus, is not fast, especially for large images. It has three advantages over the above: (1) it will accept arbitrary resolutions, even non-power-of-two scaling factors (2) it uses pure Python+Numpy with no external libraries and (3) it interpolates all the pixels for an arguably 'nicer-looking' result. These solutions are all respectable, so I offer this only for completeness. It looks like the answers so far fall into one of a few categories: Stumbled back upon this after a few years. It's the fastest way to do this that I've found. This method uses the equivalent of max pooling. Small_image = large_image.reshape((1, output_size, bin_size, Small_image = large_image.reshape((3, output_size, bin_size,įor grayscale images just change the 3 to a 1 like this:Ĭhannels first ordering # large image is shape (1, 128, 128) Small_image = large_image.reshape((output_size, bin_size,Ĭhannels first ordering # large image is shape (3, 128, 128) The following examples downsample from 128x128 to 64圆4 (this can be easily changed).Ĭhannels last ordering # large image is shape (128, 128, 3) This method only works when the input dimensions are a multiple of the output dimensions. Like with most options, there is no "best" option in the sense that for every resize schema, there are scenarios where one strategy can be preferred over another.įor people coming here from Google looking for a fast way to downsample images in numpy arrays for use in Machine Learning applications, here's a super fast method (adapted from here ).
0 Comments
Leave a Reply.AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |