imgops Module

imgops module provides a ImgOps class containing basic interaction methods with image file.

Register module

>>> import farabio.data.imgops.ImgOps as ImgOps
>>> img_path = './sample.jpg'
class farabio.data.imgops.ImgOps(imgpath)[source]

Creates instance of ImgOps class

Attributes
imgpathstr

image path of interest

fsizestr

file size in human readable format

imgnumpy.array

image

hint

height

wint

width

chint

channels

pmaxint | float

max pixel value

pminint | float

min pixel value

img_rnumpy.array

red channel image

img_gnumpy.array

green channel image

img_bnumpy.array

blue channel image

Methods

get_date(self)

Returns created date and hour info

print_imginfo(self)

Prints image information

slice_img(img, slices, orien)

Slices image into pieces

pad_img(self, droi, simg=None)

Pads image into predefined ROI size

approx_bcg(self, channel=’blue’)

Approximates background image

blend_img(self, ref_, overlap=0.2, ratio=0.5)

Blends two images taking the overage of overlap area

mask_img(self, bw)

Masks image

profile_img(self, pt1, pt2)

Plots image x/y profile between two coordinates

get_date()[source]

Get created date and hour info

Returns
str

Date and Hour, ex: 200820_191645

Examples

>>> ImgOps(img_path).get_date()
print_imginfo()[source]

Prints information pm image

Returns
info

Path, Shape, Intensity range and file size

Examples

>>> ImgOps(img_path).print_imginfo()
slice_img(slices, orien)[source]

Slices image into pieces

Parameters
imgnumpy.array

image

slicesint

number of slices

orienstr

orientation to cut. ‘x’: vertical, ‘y’: horizontal

Returns
img_sliceslist of numpy.array

image slices

img_slices_infolist of tuples

represents sliced image dimensions

Examples

>>> ImgOps(img_path).slice_img(slices=2,orien='x')
pad_img(droi, simg=None)[source]

Pads image into predefined ROI size

Parameters
imgnumpy.array

image

droituple

desired ROI shape. Ex: (1024, 1024) for gray, (1024, 1024, 3) for RGB

simgtuple

optional. custom definitions for object of interest

Returns
img_padnumpy.array

padded image

Examples

>>> ImgOps(img_path).pad_img((1024, 1024, 3))
approx_bcg(channel='blue')[source]

Approximates background image

Parameters
channelstr

colour channel

Returns
img_padnumpy.array

approximate background image

Examples

>>> ImgOps(img_path).approx_bcg(channel='blue')
blend_img(ref_, overlap=0.2, ratio=0.5)[source]

Blends two images taking the average of overlap area

Parameters
ref_numpy.array

image to blend with

overlapfloat

opt. ratio of overlap area

ratiofloat

opt. blending ratio

Returns
img_blendnumpy.array

blended image

Examples

>>> img_ref = ImgOps('./imgtoblend.png').img
>>> ImgOps(img_path).blend_img(img_ref, overlap=0.3, ratio=0.5)
static blend_imgs(img1, img2, overlap=0.2, ratio=0.5)[source]

This function blends two images taking the overage of overlap area

Parameters
img1numpy.array

First image to blend

img2numpy.array

Second image to blend

overlapfloat

optional. ratio of overlap area

ratiofloat

optional. blending ratio

Returns
img_blendnumpy.array

blended image

Examples

>>> ImgOps().blend_imgs("img1.jpg","img2.jpg",ratio=0.3)
mask_img(bw)[source]

Masks image

Parameters
bwnumpy.array

Binary mask

Returns
img_ovnumpy.array

Overlay image

Examples

>>> img_mask = ImgOps('./imgmask.png').img
>>> ImgOps(img_path).mask_img(img_mask)
static random_flip(img, y_random=False, x_random=False, return_param=False, copy=False)[source]

Randomly flip an image in vertical or horizontal direction.

Args:

img (~numpy.ndarray): An array that gets flipped. This is in CHW format. y_random (bool): Randomly flip in vertical direction. x_random (bool): Randomly flip in horizontal direction. return_param (bool): Returns information of flip. copy (bool): If False, a view of img will be returned.

Returns:

~numpy.ndarray or (~numpy.ndarray, dict): If return_param = False, returns an array out_img that is the result of flipping. If return_param = True, returns a tuple whose elements are out_img, param. param is a dictionary of intermediate parameters whose contents are listed below with key, value-type and the description of the value. * y_flip (bool): Whether the image was flipped in the vertical direction or not. * x_flip (bool): Whether the image was flipped in the horizontal direction or not.

static read_image(path, dtype=<class 'numpy.float32'>, color=True)[source]

Read an image from a file. This function reads an image from given file. The image is CHW format and the range of its value is \([0, 255]\). If color = True, the order of the channels is RGB.

Args:

path (str): A path of image file. dtype: The type of array. The default value is float32. color (bool): The option determines # channels. RGB if True, grayscale for False

Returns:

~numpy.ndarray: An image.

profile_img(pt1, pt2)[source]

Plots image profile (x and y)

Parameters
pt1tuple

coordinates of first point (row1, col1)

pt2tuple

coordinates of second point (row2, col2)

Returns
figmatplotlib figure

projection of histograms

Examples

>>> ImgOps(img_path).profile_img((50,50), (100,100))