nncore.image
IO
- nncore.image.io.imread(filename, flag='color', to_rgb=False)[source]
Read an image from a file.
- Parameters:
filename (str) – Path to the image file.
flag (str | int, optional) – Flags specifying the color type of the loaded image. Currently supported flags include
color
,grayscale
, andunchanged
. Default:color
.to_rgb (bool, optional) – Whether to convert channel order from
BGR
toRGB
. Default:False
.
- Returns:
The loaded image array.
- Return type:
np.ndarray
- nncore.image.io.imwrite(img, filename, overwrite=True, params=None)[source]
Write an image to a file.
- Parameters:
img (
np.ndarray
) – The image array to be written.filename (str) – Path to the image file.
params (list | None, optional) – Same as the
cv2.imwrite
interface. Default:None
.
- Returns:
Successful or not.
- Return type:
bool
Colorspace
- nncore.image.colorspace.bgr2gray(img, keep_dim=False)[source]
Convert a BGR image to a grayscale image.
- Parameters:
img (
np.ndarray
) – The input image.keep_dim (bool, optional) – Whether to keep the number of dimensions of the input image. Default:
False
.
- Returns:
The converted grayscale image.
- Return type:
np.ndarray
- nncore.image.colorspace.rgb2gray(img, keep_dim=False)[source]
Convert an RGB image to a grayscale image.
- Parameters:
img (
np.ndarray
) – The input image.keep_dim (bool, optional) – Whether to keep the number of dimensions of the input image. Default:
False
.
- Returns:
The converted grayscale image.
- Return type:
np.ndarray
- nncore.image.colorspace.gray2bgr(img)[source]
Convert a grayscale image to a BGR image.
- Parameters:
img (
np.ndarray
) – The input image.- Returns:
The converted BGR image.
- Return type:
np.ndarray
- nncore.image.colorspace.gray2rgb(img)[source]
Convert a grayscale image to an RGB image.
- Parameters:
img (
np.ndarray
) – The input image.- Returns:
The converted RGB image.
- Return type:
np.ndarray
- nncore.image.colorspace.bgr2rgb(img)
Convert a BGR image to an RGB image.
- Parameters:
img (
np.ndarray
) – The input image.- Returns:
The converted RGB image.
- Return type:
np.ndarray
- nncore.image.colorspace.rgb2bgr(img)
Convert an RGB image to a BGR image.
- Parameters:
img (
np.ndarray
) – The input image.- Returns:
The converted BGR image.
- Return type:
np.ndarray
- nncore.image.colorspace.bgr2hsv(img)
Convert a BGR image to an HSV image.
- Parameters:
img (
np.ndarray
) – The input image.- Returns:
The converted HSV image.
- Return type:
np.ndarray
- nncore.image.colorspace.hsv2bgr(img)
Convert an HSV image to a BGR image.
- Parameters:
img (
np.ndarray
) – The input image.- Returns:
The converted BGR image.
- Return type:
np.ndarray
- nncore.image.colorspace.bgr2hls(img)
Convert a BGR image to an HLS image.
- Parameters:
img (
np.ndarray
) – The input image.- Returns:
The converted HLS image.
- Return type:
np.ndarray
- nncore.image.colorspace.hls2bgr(img)
Convert an HLS image to a BGR image.
- Parameters:
img (
np.ndarray
) – The input image.- Returns:
The converted BGR image.
- Return type:
np.ndarray
Geometric
- nncore.image.geometric.imresize(img, size, interpolation='bilinear', return_scale=False)[source]
Resize an image to a given size.
- Parameters:
img (
np.ndarray
) – The input image.size (tuple[int]) – The target size in the form of
(width, height)
.interpolation (str | int, optional) – Interpolation method. Currently supported methods include
nearest
,bilinear
,bicubic
,area
, andlanczos
. Default:bilinear
.return_scale (bool, optional) – Whether to return
w_scale
andh_scale
. Default:False
.
- Returns:
The resized image (and scales).
- Return type:
np.ndarray
| tuple
- nncore.image.geometric.imresize_like(img, target, interpolation='bilinear', return_scale=False)[source]
Resize an image to the same size of a given image.
- Parameters:
img (
np.ndarray
) – The input image.target (
np.ndarray
) – The target image.interpolation (str | int, optional) – Interpolation method. Currently supported methods include
nearest
,bilinear
,bicubic
,area
, andlanczos
. Default:bilinear
.return_scale (bool, optional) – Whether to return
w_scale
andh_scale
. Default:False
.
- Returns:
The resized image (and scales).
- Return type:
np.ndarray
| tuple
- nncore.image.geometric.rescale_size(size, scale, return_scale=False)[source]
Compute the new size to be rescaled to.
- Parameters:
size (tuple[int]) – The original size in the form of
(width, height)
.scale (int | tuple[int]) – The scaling factor or the maximum size. If it is a number, the image will be rescaled by this factor. When it is a tuple containing 2 numbers, the image will be rescaled as large as possible within the scale. In this case,
-1
means infinity.return_scale (bool, optional) – Whether to return the scaling factor. Default:
False
.
- Returns:
The new size (and scaling factor).
- Return type:
np.ndarray
| tuple
- nncore.image.geometric.imrescale(img, scale, interpolation='bilinear', return_scale=False)[source]
Resize an image while keeping the aspect ratio.
- Parameters:
img (
np.ndarray
) – The input image.scale (int | tuple[int]) – The scaling factor or the maximum size. If it is a number, the image will be rescaled by this factor. When it is a tuple containing 2 numbers, the image will be rescaled as large as possible within the scale. In this case,
-1
means infinity.interpolation (str | int, optional) – Interpolation method. Currently supported methods include
nearest
,bilinear
,bicubic
,area
, andlanczos
. Default:bilinear
.return_scale (bool, optional) – Whether to return the scaling factor. Default:
False
.
- Returns:
The resized image (and scaling factor).
- Return type:
np.ndarray
| tuple
Normalize
- nncore.image.normalize.imnormalize(img, mean, std)[source]
Normalize an image with mean and std.
- Parameters:
img (
np.ndarray
) – The image to be normalized.mean (
np.ndarray
) – The mean to use.std (
np.ndarray
) – The standard deviation to use.
- Returns:
The normalized image.
- Return type:
np.ndarray
- nncore.image.normalize.imdenormalize(img, mean, std)[source]
Denormalize an image with mean and std.
- Parameters:
img (
np.ndarray
) – The image to be denormalized.mean (
np.ndarray
) – The mean to use.std (
np.ndarray
) – The standard deviation to use.
- Returns:
The denormalized image.
- Return type:
np.ndarray