improc.blkptcs package

Submodules

improc.blkptcs.blocks module

improc.blkptcs.blocks.blks2imgs(blks, imgsShape, index=None, tofolder=None)[source]

Fight image blocks back to images.

Parameters:
  • blks ({array_like}) – A bH-bW-bC-bN numpy ndarray, where, bH, bW, bC, bN specify height, width, channels, number of images respectivelly. If blks Contains Gray Images and RGB Images, then bC = 3, and Gray Images is copied.
  • imgsShape ({tuple or list}) – Specify each image’s size.
  • index ({int, optional}) – The image that you want to fight back.
  • tofolder ({str (path), optional}) – Save specified images into files: image_0.png, image_i.png, … , image_N.png
Returns:

out – A list of image numpy ndarray(H-W-C).

Return type:

{ndarray or bool}

See also

imgs2ptcs(), imgs2blks.()

improc.blkptcs.blocks.imgs2blks(imgs, blksize=[8, 8, 3], padmode='symmetric')[source]

blks, imgsShape = imgs2blks(imgs, blksize) trys to split the imgs(an H-W-C -N numpy ndarray, or list of images filepathes) into image blocks(an bH-bW- bC-bN ndarray) orderly; imgsShape contains each image’s size in imgs for fighting back to images.

Trys to split some images into blocks orderly (1.channel-wisely, 2.colum- wisely, 3.row-wisely).

Parameters:
  • imgs ({array_like, or list of image pathes}.) – Images to be splited, a H-W-C-N numpy ndarray, or a list of image pathes with diffrent image shapes.
  • blksize ({int tuple, list or None, optional}) – Specifies the each block size (rows, cols, channel) that you want to split. If not given, blksize=[8, 8, 3].
  • padmode ({str or None, optional}) – Padding mode when an image can’t be split fully. ‘symmetric’ or ‘edge’ can be choosen, see np.pad for more option. The Channel will also be pad if it is not enough, e.g., for an H-W-1(gray) image, if you want to get blksize=[8,8,3].
Returns:

  • blks ({ndarray}) – A bH-bW-bC-bN numpy ndarray, (bH, bW, bC) == blksize.
  • imgsshape ({tuple}) – A tuple contains the shape of each image in imgs. Even if all images have the same size, imgsshape will still contain each image size like ((H, W, C), (H, W, C)).

See also

imgs2ptcs(), imgs2blks(), blks2imgs.()

Examples

>>> blks, imgsInfo = imgs2blks(imgspathes, [8, 8, 1], 'symmetric')
improc.blkptcs.blocks.padimgs(imgs, pad_width, mode)[source]
improc.blkptcs.blocks.pimgs2blks(pimgs, blksize)[source]

blks: bH-bW-bC-bN, img1_blk1_bc1, img1_blk1_bc2, img1_blk1_bc3, img1_blk2_bc1, img1_blk2_bc2, img1_blk2_bc3, …

improc.blkptcs.patches module

improc.blkptcs.patches.geocluptcs(patches, method='std', thresh=None, Rstar=None, sort='descent')[source]

Classifies patches into smooth blocks and rough blocks

Parameters:
  • patches ({array_like}) – Image patches, a pH-pW-pC-pN numpy ndarray.
  • thresh ({float or None, optional}) – Threshold for spliting smooth block and rough block.
  • Rstar ({float or None, optional}) – Specifies which method to used for evaluating each patch. Option: ‘std’(standard deviation), ‘var’(variance),
Returns:

  • selpat ({ndarray}) – A pH-pW-pC-pNx numpy ndarray.
  • idxsel ({ndarray}) – A pNx numpy array, indicates the position of selpat in patches.
  • selptcs_scores ({ndarray}) – A pNx numpy array, indicates the scores of selpat in patches, with measurements specfied by method.

See also

imgs2ptcs(), imgs2blks(), blks2imgs(), showblks.()

Examples

>>> patches = np.uint8(np.random.randint(0, 255, (8, 8, 3, 101)))
>>> # just compute scores:
>>> selpat, _, scores = geocluptcs(patches, method='std')
>>> # compute scores and sort by scores:
>>> selpat, _, scores = selptcs(patches, method='std', sort='descent')
>>> # compute scores and sort by scores, and just select scores > thresh
>>> selpat, idxsel, _ = selptcs(patches, method='std', thresh=15)
>>> # compute scores and sort by scores, and select scores > thresh, and
>>> # sort in descending order.
>>> selpat, idxsel, _ = selptcs(patches, method='std', thresh=15,
    sort='descent')
>>>
improc.blkptcs.patches.imgs2ptcs(imgs, ptcsize, numptcs, seed=None)[source]

Sample diffrent patches from imgs.

Parameters:
  • imgs ({array_like, or list of image pathes}.) – Images to be sampled, a H-W-C-N numpy ndarray, or a list of image pathes with diffrent image shapes.
  • ptcsize ({int tuple, list or None, optional}) – Specifies the each patch size (rows, cols, channel) that you want to sampled. If not given, ptcsize=[8, 8, 1].
  • numptcs ({int or None, optional}) – The number of patches that you want to sample, if None, numptcs=100.
  • seed ({int or None, optional}) – Random seed, default None(differ each time.
Returns:

ptcs – A bH-bW-bC-bN numpy ndarray, (bH, bW, bC) == ptcsize.

Return type:

{ndarray}

See also

imgs2blks(), blks2imgs(), showblks.()

Examples

>>> import numpy as np
>>> from matplotlib import pyplot as plt
>>> from imgblk import imgs2ptcs, showblks
>>> ptcsize = [128, 128, 3]
>>> numptcs = 20
>>> imgspathes = ['/mnt/d/DataSets/oi/nsi/classical/BaboonRGB.bmp',
                 '/mnt/d/DataSets/oi/nsi/classical/LenaRGB.bmp',]
>>> ptcs = imgs2ptcs(imgspathes, ptcsize , numptcs)
>>> print(ptcs.dtype, ptcs.shape)
>>> # show
>>> showblks(ptcs, rcsize=(10,10))

See “test_imgs2ptcs.py”, “test_imgs2blks.py”, “test_blks2imgs.py”, “test_showblks.py” for more Examples.

improc.blkptcs.patches.imgsAB2ptcs(imgsA, imgsB, ptcsize, numptcs, seed=None)[source]

Sampling diffrent patches from imgsA imgsB.

Parameters:
  • imgsA/B ({array_like, or list of image pathes.}) – Images to be sampled, a H-W-C-N numpy ndarray, or a list of image pathes with diffrent image shapes.
  • ptcsize ({int tuple, list or None, optional}) – Specifies the each patch size (rows, cols, channel) that you want to sampled. If not given, ptcsize=[8, 8, 1].
  • numptcs ({int or None, optional}) – The number of patches that you want to sample, if None, numptcs=100.
  • seed ({int or None, optional}) – Random seed, default None(differ each time.
Returns:

  • ptcsA ({ndarray}) – A bH-bW-bC-bN numpy ndarray, (bH, bW, bC) == ptcsize.
  • ptcsB ({ndarray}) – A bH-bW-bC-bN numpy ndarray, (bH, bW, bC) == ptcsize.

See also

imgs2blks(), blks2imgs(), showblks.()

Examples

>>> import numpy as np
>>> from matplotlib import pyplot as plt
>>> from imgblk import imgs2ptcs, showblks
>>> ptcsize = [128, 128, 3]
>>> numptcs = 20
>>> imgspathesA = ['/mnt/d/DataSets/oi/nsi/classical/BaboonRGB.bmp']
    imgspathesB = ['/mnt/d/DataSets/oi/nsi/classical/LenaRGB.bmp']
>>> ptcsA, ptcsB = imgsAB2ptcs(imgspathesA, imgspathesB, ptcsize , numptcs)
>>> print(ptcsA.dtype, ptcsA.shape)
>>> # show
>>> showblks(ptcsA, rcsize=(10,10))

See “test_imgs2ptcs.py”, “test_imgs2blks.py”, “test_blks2imgs.py”, “test_showblks.py” for more Examples.

improc.blkptcs.patches.imgsABC2ptcs(imgsA, imgsB, imgsC, ptcsize, numptcs, seed=None)[source]

Sampling diffrent patches from imgsA imgsB imgsC.

Parameters:
  • imgsA/B/C ({array_like, or list of image pathes}.) – Images to be sampled, a H-W-C-N numpy ndarray, or a list of image pathes with diffrent image shapes.
  • ptcsize ({int tuple, list or None, optional}) – Specifies the each patch size (rows, cols, channel) that you want to sampled. If not given, ptcsize=[8, 8, 1].
  • numptcs ({int or None, optional}) – The number of patches that you want to sample, if None, numptcs=100.
  • seed ({int or None, optional}) – Random seed, default None(differ each time.
Returns:

  • ptcsA ({ndarray}) – A bH-bW-bC-bN numpy ndarray, (bH, bW, bC) == ptcsize.
  • ptcsB ({ndarray}) – A bH-bW-bC-bN numpy ndarray, (bH, bW, bC) == ptcsize.
  • ptcsC ({ndarray}) – A bH-bW-bC-bN numpy ndarray, (bH, bW, bC) == ptcsize.

See also

imgs2blks(), blks2imgs(), showblks.()

Examples

>>> import numpy as np
>>> from matplotlib import pyplot as plt
>>> from imgblk import imgs2ptcs, showblks
>>> ptcsize = [128, 128, 3]
>>> numptcs = 20
>>> imgspathesA = ['/mnt/d/DataSets/oi/nsi/classical/BaboonRGB.bmp']
    imgspathesB = ['/mnt/d/DataSets/oi/nsi/classical/LenaRGB.bmp']
>>> ptcsA, ptcsB = imgsAB2ptcs(imgspathesA, imgspathesB, ptcsize , numptcs)
>>> print(ptcsA.dtype, ptcsA.shape)
>>> # show
>>> showblks(ptcsA, rcsize=(10,10))

See “test_imgs2ptcs.py”, “test_imgs2blks.py”, “test_blks2imgs.py”, “test_showblks.py” for more Examples.

improc.blkptcs.patches.sampleimg(imgA, ptcsize, numSamples, imgB=None, imgC=None, seed=None)[source]
improc.blkptcs.patches.selptcs(patches, numsel=None, method=None, thresh=None, sort=None)[source]

Selects some patches based on std, var…

Parameters:
  • patches ({array_like}) – Image patches, a pH-pW-pC-pN numpy ndarray.
  • numsel ({int, float or None, optional}) – How many patches you want to select. If integer, then returns numsel patches; If float in [0,1], then numsel*100 percent of the patches will be returned. If not given, does not affect.
  • method ({str or None, optional}) – Specifies which method to used for evaluating each patch. Option: ‘std’(standard deviation), ‘var’(variance), the first numsel patch scores will be returned. If not given, does nothing.
  • thresh ({float, optional}) – When method are specified, using thresh as the threshold, and those patches whose scores are larger(>=) then thresh will be returned.
  • sort ({str or None, optional}) – Sorts the patches according to pathes scores in sort way(‘descent’, ‘ascent’). If not given, does not affect, i.e. origional order.
Returns:

  • selpat ({ndarray}) – A pH-pW-pC-pNx numpy ndarray.
  • idxsel ({ndarray}) – A pNx numpy array, indicates the position of selpat in patches.
  • selptcs_scores ({ndarray}) – A pNx numpy array, indicates the scores of selpat in patches, with measurements specfied by method.

See also

imgs2ptcs(), imgs2blks(), blks2imgs(), showblks.()

Examples

>>> patches = np.uint8(np.random.randint(0, 255, (8, 8, 3, 101)))
>>> # just compute scores:
>>> selpat, _, scores = selptcs(patches, method='std')
>>> # compute scores and sort by scores:
>>> selpat, _, scores = selptcs(patches, method='std', sort='descent')
>>> # compute scores and sort by scores, and just select scores > thresh
>>> selpat, idxsel, _ = selptcs(patches, method='std', thresh=15)
>>> # compute scores and sort by scores, and select scores > thresh, and
>>> # sort in descending order.
>>> selpat, idxsel, _ = selptcs(patches, method='std', thresh=15,
    sort='descent')
>>>

improc.blkptcs.utils module

improc.blkptcs.visual module

improc.blkptcs.visual.showblks(blks, rcsize=None, stride=None, plot=True, bgcolor='w', cmap=None, title=None, xlabel=None, ylabel=None)[source]

Trys to show image blocks in one image.

Parameters:
  • blks ({array_like}) – Blocks to be shown, a bH-bW-bC-bN numpy ndarray.
  • rcsize ({int tuple or None, optional}) – Specifies how many rows and cols of blocks that you want to show, e.g. (rows, cols). If not given, rcsize=(rows, clos) will be computed automaticly.
  • stride ({int tuple or None, optional}) – The step size (blank pixels nums) in row and col between two blocks. If not given, stride=(1,1).
  • plot ({bool, optional}) – True for ploting, False for silent and returns a H-W-C numpy ndarray for showing.
  • bgcolor ({char or None, optional}) – The background color, ‘w’ for white, ‘k’ for black. Default, ‘w’.
Returns:

out – A H-W-C numpy ndarray for showing.

Return type:

{ndarray or bool}

See also

imgs2ptcs(), imgs2blks(), blks2imgs.()

Examples

>>> blks = np.uint8(np.random.randint(0, 255, (8, 8, 3, 101)))
>>> showblks(blks, bgcolor='k')
improc.blkptcs.visual.showfilters(filters, fsize=None, rcsize=None, stride=None, plot=None, bgcolor='w', title=None, isscale=True)[source]

Trys to show weight filters in one image.

Parameters:
  • filters ({array_like}) – Weights to be shown, a fdim-by-numf numpy ndarray.
  • fsize ({int tuple list or None, optional}) – Specifies the height and width of one filter, e.g. (hf, wf, cf). If not given, fsize=(hf, wf, cf) will be computed automaticly, but may wrong.
  • rcsize ({int tuple list or None, optional}) – Specifies how many rows and cols of filters that you want to show, e.g. (rows, cols). If not given, rcsize=(rows, clos) will be computed automaticly.
  • stride ({int tuple or None, optional}) – The step size (blank pixels nums) in row and col between two filters. If not given, stride=(1,1).
  • plot ({bool or None, optional}) – True for ploting, False for silent and returns a H-W-C numpy ndarray for showing. If not given, plot = True.
  • bgcolor ({char or None, optional}) – The background color, ‘w’ for white, ‘k’ for black. Default, ‘w’.
  • title ({str or None}) – If plot, title is used to specify the title.
  • isscale ({bool or None, optional}) – If True, scale to [0, 255], else does nothing.
Returns:

out – A H-W-C numpy ndarray for showing.

Return type:

{ndarray or bool}

See also

imgs2ptcs(), imgs2blks(), blks2imgs.()

Examples

>>> filters = np.uint8(np.random.randint(0, 255, (192, 101)))
>>> showfilters(filters, bgcolor='k')

Module contents