Prelude to Medical Imaging

home > Research

Here is the repository for pre-processing of some medical imaging data.

 

1. Attention-Gated-Networks_auxiliary

Working on Attention-Gated-Networks from https://github.com/ozan-oktay/Attention-Gated-Networks, you might want to run the algorithm on the original datasets. One of them is PANCREAS CT-82 which can be found from https://wiki.cancerimagingarchive.net/display/Public/Pancreas-CT.

The .dcm files store 2D slices. This code combines (and normalize) the slices of each patient into 3D volume. Each patient is coded as PANCREAS_0001, PANCREAS_0002 etc.

2. Multiv

While working on medical images, for example in NIFTI formats, we might face memory problem. This is because a NIFTI volume might come in large sizes, for example 192x192x19 with many modalities. With large convolutional neural network, feeding the entire volume may result in out of memory error (at least my measly 4GB RAM does. Multi-view sampling is the way out of this. Using multi-view sampling, slices of the images (green rectangles) are sampled. The “multi” part of the multi-view can take the form of larger slice (red rectangles).

dualview2D_test.gif

Object Detection using Tensorflow: bee and butterfly Part II, faster

home>ML>Image Processing

Summary so far: Given images of butterflies and bees, mark the butterflies and the bees with white color RGB(255,255,255), create rotated copy of the images, annotate the position of these butterflies and bees with respect to the images in PASCAL VOC format, and finally put them into train and test folders.

Using python package kero version 4.3.0, we can increase the speed of image pre-processing using clone_to_annotate_faster() to do the processes described in the summary above. We will use this as the alternative to the tutorial from Part II of bees and butterflies object detection, and the front section of Part III.

Tips: do remember to activate the virtual environment if you have deactivated it. Virtual environment helps ensure that packages we download may not interfere with the system or other projects, especially when we need older version of some packages.

Create the following directory. I named it keropb, you can name it anything.

adhoc/myproject/images
+ train 
+ test
adhoc/keropb
+ butterflies_and_bees
  + Butterflies
    - butterflyimage1.png 
    - ...
  + Butterflies_canvas
    - butterflyimage1.png
  + Bees
    - beeimage1.png
    - ...
  + Bees_canvas
    - beeimage1.[ng
    - ...
+ do_clone_to_annotate.py
+ do_convert_to_PASCALVOC.py
+ do_move_ALL_to_train.py
+ do_move_a_fraction.py
+ adhoc_functions.py

Note: the image folders and the corresponding canvas folders can be downloaded here. Also, do not worry, the last 5 python files will be provided along the way.

We store all our butterflies images in the folder Butterflies and bee images in the folder Bees. The _canvas folders are exact replicas of the corresponding folders. You can copy-paste both Butterflies and Bees folders and rename them. In the canvas folders, however, we will mark out the butterflies and the bees. In a sense, we are teaching the algorithm which objects in the pictures are butterflies, and which are bees. To mark out a butterfly, use white (255,255,255) RGB to block out the butterfly. Ok, this is easy to do, just use the good ol’ Paint program and use white color to paint over the butterfly, or use eraser. See the example below. Note that the images have exactly the same names.

Tips: if the image contains white patches, they might be wrongly detected as a butterfly too. This is bad. In that case, paint this irrelevant white patches with other obvious color, such as black.

butterfly.jpg

Make sure kero is installed or upgrade it with

pip install --upgrade kero

Create and run the following script do_clone_to_annotate.py from adhoc/keropb i.e. in cmd.exe, cd into keropb and run the command

python do_clone_to_annotate.py

Tips: We have set check_missing_mode=False. It is good to set it to True first. This helps us check if each image in Butterflies have a corresponding image in Butterflies_canvas. Before processing, we want to identify missing images so that we can fix them before proceeding. If everything is fine, “ALL GREEN. No missing files.” will be printed. Then set it to False and run it again.

do_clone_to_annotate.py

import kero.ImageProcessing.photoBox as kip

this_folder = "butterflies_and_bees\\Butterflies" # "butterflies_and_bees\\Bees"
tag_folder =  "butterflies_and_bees\\Butterflies_canvas" # "butterflies_and_bees\\Bees_canvas"

gsw=kip.GreyScaleWorkShop()
rotate_angle_set = [30,60,90,120,150,180] # None
annotation_name = "butterfly" # "bee"
gsw.clone_to_annotate_faster(this_folder, tag_folder,1,annotation_name, 
	order_name="imgBUT", tag_name="imgBUT",
	check_missing_mode=False,
	skip_ground_truth=False,
	significant_fraction=0.01,
	rotate_angle_set=rotate_angle_set,
	thresh=250)

Redo the above for Bees folder as well changing the blue bolded values above correspondingly. See that Bees_LOG.txt and Butterflies_LOG.txt are created also, listing how the image files are renamed.

Create PASCAL VOC annotation format in xml

Now, still in keropb folder, create adhoc_functions.py as shown in Object Detection using Tensorflow: adhoc functions, adjust the approriate paths in do_convert_to_PASCALVOC.py and then run the following

python do_convert_to_PASCALVOC.py

do_convert_to_PASCALVOC.py

import adhoc_functions as af

for x in ["Butterflies","Bees"]:
	annot_foldername = "C:\\Users\\acer\\Desktop\\adhoc\\keropb\\butterflies_and_bees\\"+ x + "_ANNOT"
	annot_filetype = ".txt"
	img_foldername = "C:\\Users\\acer\\Desktop\\adhoc\\keropb\\butterflies_and_bees\\"+ x +"_CLONE"
	img_filetype = ".png"
	
	af.mass_convert_to_PASCAL_VOC_xml(annot_foldername,annot_filetype,
	annot_foldername,img_filetype)

By now, all the images required are available with annotations in PASCAL VOC, written in .xml files.

Train and test split

Now we split the images (with annotations) to training folder (90% of all the images) and test folder (the remaining 10%). We can further split out 10% out of the 90% for external testing if you prefer. Our tutorial will use 90% for training and 10% for validation, without splitting 10% out of the the 90%.

Run the following.

python do_move_ALL_to_train.py

do_move_ALL_to_train.py

import adhoc_functions, os

train_folder = "C:\\Users\\acer\\Desktop\\adhoc\\myproject\\images\\train"
test_folder = "C:\\Users\\acer\\Desktop\\adhoc\\myproject\\images\\test"
if not os.path.exists(train_folder):
	print("Creating train folder.")
	os.mkdir(train_folder)
if not os.path.exists(test_folder):
	print("Creating test folder.")
	os.mkdir(test_folder)

for i in ["Butterflies","Bees"]:
	folder_name_CLONE="C:\\Users\\acer\\Desktop\\adhoc\\keropb\\butterflies_and_bees\\"+i+"_CLONE"
	folder_name_ANNOT="C:\\Users\\acer\\Desktop\\adhoc\\keropb\\butterflies_and_bees\\"+i+"_ANNOT"
	rotate_angle_set=[30,60,90,120,150,180]
	folder_target = train_folder
	adhoc_functions.move_ALL_to_train(folder_name_CLONE,folder_name_ANNOT,rotate_angle_set, folder_target)

print("Done.")

The folder train will be populated. Now we get 10% into folder test. Run the following

python do_move_a_fraction.py

do_move_a_fraction.py

import adhoc_functions as af

src= "C:\\Users\\acer\\Desktop\\adhoc\\myproject\\images\\train"
tgt= "C:\\Users\\acer\\Desktop\\adhoc\\myproject\\images\\test"
af.move_some_percent(src,tgt)

Done! To proceed with the next step, which is model training, see the section “Conversion to tfrecords” (and skip “Train and test split” section) in Part III of bees and butterflies object detection.

kero Changelog

Version 0.6.3

NeuralNetwork.py. initiate_neural_network(). Add UniformRandom mode.

Version 0.6.2

NeuralNetwork.py: included NetworkUpdater class that performs model training.
utils.py: more functions are included.

Version 0.6.0

Included kero.utils.
Included NeuralNetwork.py in kero.multib.

Version 0.5.3

Included MANIFEST.in for sdist in Pypi upload.

Version 0.5.2

kero/multib/nDnet.py. Fixed bugs on

  1. SpecialGallery(). quiver3D_lattice3D, grid_shape=None assumes [N,1] shape.
  2. tuple_to_grid(). When Z is None, NoneType has no tolist(). problem will no longer occur.

Version 0.5.1

kero/multib/nDnet.py

quiver3D_lattice3D bug on squashed 3D plot fixed.

Version 0.5.0

kero/multib added.

Version 0.4.4

kero/ImageProcessing/photobBox.py

clone_to_annotate_faster() additional arguments 1. discard_edge and 2. edge_offset. Also fix bug on labeling object outside image or zero-width object.

Version 0.4.3

kero/ImageProcessing/photobBox.py
GreyScaleWorkshop:

  1. clone_to_annote () and Clone_to_annote_faster(). No longer need to set skip_ground_truth to False in order to use check_missing_mode = True
  2. scaled_box+positions() return None if there is no white patch RGB(255,255,255) found.
  3. multiple_scaled_box_positions() auto adjusts ground truth image rescaling if previous rescaling causes

tag_rename_clone: Added logging feature showing the change of names.

clone_to_annotate_faster()

home > kero > Documentation

Given a folder of images and a tag folder containing the same images, each of which is marked with white color, RGB=(255,255,255), this function creates

  1. a clone of the folder,
  2. a folder of its ground truth images based on the tag folder,
  3. a folder containing the images in the folder with bounding boxes based on the ground truth images along with text files containing
    image_label img-width img-height xmin ymin xmax ymax
    where (xmin,ymin) the top-left coordinates of the bounding box and (xmax,ymax) bottom-right bounding box.
def clone_to_annotate_faster(self,this_folder, tag_folder,starting_label,annotation_name,
        order_name="img",
        tag_name="imggt",
        check_missing_mode=False,
        rotate_angle_set=None,
        skip_ground_truth=False,
        significant_fraction=0.01,
        thresh=254,
        scale="Auto",
        discard_edge=True,
        edge_offset=2):
  return

 

this_folder String. The name of the folder with images.
tag_folder String. The name of the folder with images. If each image in the tag_folder has a corresponding image of the same name and type, then clone folder, clone tag folder, ground truth image folder and annotation folder will be created with their corresponding contents.
starting_label Integer. The sequence of numbers will start with this integer.
annotation_name String. The image label for the white region marked in the tag folder. This will be saved in annotation .txt files.

In example 1, this is “butterfly”. This means that we label the object marked with white a “butterfly”.

order_name String. The clone of this_folder will be relabelled with prefix specified by this string.

Default value =”img”

tag_name String. The clone of tag_folder will be relabelled with prefix specified by this string.

Default value =”imggt”

check_missing_mode Boolean. If True, cloning process of the folders are not performed. The file names of images in this_folder that do not have the corresponding images in the tag_folder will be printed.

Default value =False

rotate_angle_set List of float. Each float is a value in degree with which an image is rotated.

Default value =None

skip_ground_truth Boolean. Set to True if the ground truth images have been created in the manner spawn_ground_truth() spawns them. The function will continue with annotations.
significant_fraction Float. This argument takes in values between 0 and 1.0. It specifies the fraction of area relative to the area of whole matrix, above which a component will be considered as a component. Smaller than this value, the component will be treated as noise. This is an argument to the function get_connected_components().

Default value = .001

thresh Integer, from 0 to 255. This specifies the RGB values below which the ground truth color is treated converted to black, RGB=(0,0,0) and otherwise white, RGB=(255,255,255).

For example, if the value is set to 244, a pixel with (255,254,246) is converted to white since 244<255,254 and 246 while a pixel with (20,40,120) is converted to black.

Default value = 254

scale “Auto”, (Integer,Integer) or None. Annotation of images are computed from down-scaled images to improve the processing speed (unless scale=None). If set to “Auto”, the image will be downsized to (200,150) for processing.

This is an argument to the function multiple_scaled_box_positions().

Default value = “Auto”

Note: The actual image is not changed, only that the annotation positions are re-computed back from down-scaled images, giving potential loss of accuracy.

discard_edge Boolean. True:
While annotating objects in the image, objects whose bounding boxes appear at the edge will not be considered. More technically, if the coordinates of a bounding box are within edge_offset from image, then it will not be considered.  For example, if image width is 400 pixels, edge_offset=5, then a bounding box that lies in x position = 396 or 2 will be ignored. 

Default value = True

edge_offset Integer. Number of pixels from the edge of the image that is considered edge of the image. If discard_edge=True, any object whose bounding box is at least partly in the edge area will be ignored.

Default value = 2

Tips: If annotation fails for one reason or another after ground truth image generation is complete, then make sure to set skip_ground_truth=True before rerunning the function, so that we do not waste time re-spawning the ground truth images.

Tips: For images whose objects are very small, setting a small scale might be a bad choice, since the position of annotation boxes might loss a lot of accuracy during rescaling.

Example usage 1.

Download the example here and put them in the working directory under the folder /bb.

import kero.ImageProcessing.photoBox as kip

this_folder = "bb\\Butterflies"
tag_folder =  "bb\\Butterflies_canvas"

gsw=kip.GreyScaleWorkShop()
rotate_angle_set = [30,60,90,120,150,180] # None
annotation_name = "butterfly"
gsw.clone_to_annotate_faster(this_folder, tag_folder,1,annotation_name, 
	order_name="img",
        tag_name="img",
	check_missing_mode=False,
	rotate_angle_set=rotate_angle_set,
	skip_ground_truth=False,
	thresh=250)

This function will call spawn_ground_truth(), i.e. create ground truth image (figure 1 top-right) of the image (figure 1 top-left) based on the corresponding image from tag folder (figure 1 top-center), and furthermore create annotations for each images. Samples of images from the folders are shown below.

clone_faster

Figure 1.

A folder containing rotated figures (figure 2) will be created. Also, a txt file will be created for each rotated copy of the image. The bounding boxes are the thin green rectangles.

clone_faster_out

Figure 2.

Speed comparison

This function is alternative to clone_to_annotate(). It is way faster, though the precision of bounding boxes for rotated images will decline. Download the example here and put

import kero.ImageProcessing.photoBox as kip
import time

start = time.time()

this_folder = "bb\\Butterflies"
tag_folder =  "bb\\Butterflies_canvas"

gsw=kip.GreyScaleWorkShop()
rotate_angle_set = [0,30,60] # None
annotation_name = "butterfly"
gsw.clone_to_annotate(this_folder, tag_folder,1,annotation_name, 
	order_name="img",
        tag_name="img",
	check_missing_mode=False,
	skip_ground_truth=False,
	significant_fraction=0.01,
	rotate_angle_set=rotate_angle_set,
	thresh=250)

end = time.time()
elapsed = end - start
print(elapsed)

# 402.4 seconds (6 mins 40 seconds)

The following shows similar process using this faster function. It is generally as faster n times than the previous script, where n is the number of rotate_angle_set. This is because the bounding boxes of rotated images are derived from the original images, though this may lead to the loss of precision.

import kero.ImageProcessing.photoBox as kip
import time

start = time.time()

this_folder = "bb\\Butterflies"
tag_folder =  "bb\\Butterflies_canvas"

gsw=kip.GreyScaleWorkShop()
rotate_angle_set = [0,30,60] # None
annotation_name = "butterfly"
gsw.clone_to_annotate_faster(this_folder, tag_folder,1,annotation_name, 
	order_name="img",
        tag_name="img",
	check_missing_mode=False,
	skip_ground_truth=False,
	significant_fraction=0.01,
	rotate_angle_set=rotate_angle_set,
	thresh=250)

end = time.time()
elapsed = end - start
print(elapsed)

# 143.5 seconds ( 2 min 23.5 seconds)

kero version: 0.4.4 and above

rotate_rect()

home > kero > Documentation

Given a bounding box inside an image, this function returns the bounding box of the rotated image.

kero.ImageProcessing.photoBox.py

def rotate_rect(coord, theta, img_width, img_height):
  return [xmin0,ymin0,xmax0,ymax0]
coord List of integers, coord = [xmin,ymin,xmax,ymax] is the coordinates of the rectangles in pixel. [xmin, ymin] is the top-left corner of the image while [xmax, ymax] is the bottom-right corner.
theta Float. Angle of rotation in radians.
img_width Integer. Image width in number of pixels.
Img_height Integer. Image height in number of pixels.
return coord_out List of integers, coord_out = [xmin,ymin,xmax,ymax]  is the rotated coordinates.

Example usage 1

Both images boxrotate.png and boxrotate2.png can be downloaded here.  The results for both images are partially shown here. The original bounding box is in green. The images are then rotated, and the red bounding boxes are the new bounding boxes computed from the green boxes and the angles of rotation.

boxrot.png

import numpy as np
import kero.ImageProcessing.photoBox as kip
import cv2, os

filename_set= []
coord_set = []

filename = "boxrotate.png"
xmin = 42
ymin = 40
xmax = 145 # 200
ymax = 125 # 250
coord = [xmin,ymin,xmax,ymax]
filename_set.append(filename)
coord_set.append(coord)

filename = "boxrotate2.png"
coord = [66,58,244,193]
filename_set.append(filename)
coord_set.append(coord)

count = 1
foldercount = 1
for filename in filename_set:
	if not os.path.exists(''.join(("result",str(foldercount)))):
		os.mkdir(''.join(("result",str(foldercount))))
	coord = coord_set[foldercount-1]
	gg=cv2.imread(filename)

	img_height, img_width, _ = gg.shape

	cv2.rectangle(gg, (coord[0], coord[1]), (coord[2],coord[3]),(0,255,0),3)

	theta_set = np.linspace(10,350,20)
	theta_set = [x*np.pi/180 for x in theta_set]
	for theta in theta_set:
		gg2=cv2.imread(filename)
		rows,cols,_ = gg2.shape
		M = cv2.getRotationMatrix2D((cols/2,rows/2),theta*180/np.pi,1)
		dst = cv2.warpAffine(gg2,M,(cols,rows))
		[xmin0,ymin0,xmax0,ymax0] = kip.rotate_rect(coord, theta, img_width, img_height)
		cv2.rectangle(dst, (xmin0, ymin0), (xmax0,ymax0),(0,0,255),3)
		cv2.imwrite(''.join(("result",str(foldercount),"/rotate",str(count),".png")), dst)
		cv2.imwrite(''.join(("result",str(foldercount),"/orign.png")),gg)
		count = count+1
	foldercount = foldercount + 1

kero version: 0.4.2 and above

rotate_wrt_img_center()

home > kero > Documentation

kero.ImageProcessing.photoBox.py

def rotate_wrt_img_center(coord, theta, img_width, img_height):
  return coord_out

This function rotates a coordinate with respect to image center.

coord List of integers, coord = [x,y] is the coordinates of the image in pixels. [0,0] is the top-left corner of the image while [width, height] is the bottom-right corner.
theta Float. Angle of rotation in radians.
img_width Integer. Image width in number of pixels.
Img_height Integer. Image height in number of pixels.
return coord_out List of integers, coord_out = [x,y] is the rotated coordinates.

Example Usage 1

import numpy as np
import kero.ImageProcessing.photoBox as kip
import cv2

gg=cv2.imread("boxrotate.png")
img_height, img_width, _ = gg.shape
# print(img_height, img_width)
xmin = 100
ymin = 50
xmax = 110 # 200
ymax = 60# 250
cv2.rectangle(gg, (xmin, ymin), (xmax,ymax),(0,255,0),1)

theta_set = np.linspace(10,330,20)
theta_set = [x*np.pi/180 for x in theta_set]

for theta in theta_set:
	[xmin0, ymin0] = kip.rotate_wrt_img_center([xmin,ymin,xmax,ymax], theta, img_width, img_height)
	print(xmin0,ymin0)
	cv2.rectangle(gg, (xmin0, ymin0), (xmin0+10,ymin0+10),(0,0,255),1)

cv2.imshow("aa",gg)
cv2.waitKey(0)

The above demonstrates how the green rectangle object is rotated wrt origin, which is the center of image.

rotimg

kero version: 0.4.2 and above

transpose_list()

home > kero > Documentation

kero.DataHandler.Generic.py

def transpose_list(input_list):
  return out

This is the usual matrix transpose operation defined 2D matrix in the form of for list of list.

input_list List of list. 2D matrix. Note that each item in the input_list is a row in the matrix.
out List of list. 2D matrix. The matrix transpose of input list.

Example usage

import kero.DataHandler.Generic as kg

mat = [[1,2],[3,4]]
for x in mat:
	print(x)

print("\nTransposed:")
for x in kg.transpose_list(mat):
	print(x)

The output is:

[1, 2]
[3, 4]

Transposed:
[1, 3]
[2, 4]

kero version: 0.1 and above

clone_to_annotate()

home > kero > Documentation

Given a folder of images and a tag folder containing the same images, each of which is marked with white color, RGB=(255,255,255), this function creates

  1. a clone of the folder,
  2. a folder of its ground truth images based on the tag folder,
  3. a folder containing the images in the folder with bounding boxes based on the ground truth images along with text files containing
    image_label img-width img-height xmin ymin xmax ymax
    where (xmin,ymin) the top-left coordinates of the bounding box and (xmax,ymax) bottom-right bounding box.

Tips. Consider using clone_to_annotate_faster().

kero.ImageProcessing.photoBox.py

def clone_to_annotate(self,this_folder, tag_folder,starting_label,annotation_name,
        order_name="img",
        tag_name="imggt",
        check_missing_mode=False,
        rotate_angle_set=None,
        skip_ground_truth=False,
        significant_fraction=0.01,
        thresh=254,
        scale="Auto"):
  return

 

this_folder String. The name of the folder with images.
tag_folder String. The name of the folder with images. If each image in the tag_folder has a corresponding image of the same name and type, then clone folder, clone tag folder, ground truth image folder and annotation folder will be created with their corresponding contents.
starting_label Integer. The sequence of numbers will start with this integer.
annotation_name String. The image label for the white region marked in the tag folder. This will be saved in annotation .txt files.

In example 1, this is “butterfly”. This means that we label the object marked with white a “butterfly”.

order_name String. The clone of this_folder will be relabelled with prefix specified by this string.

Default value =”img”

tag_name String. The clone of tag_folder will be relabelled with prefix specified by this string.

Default value =”imggt”

check_missing_mode Boolean. If True, cloning process of the folders are not performed. The file names of images in this_folder that do not have the corresponding images in the tag_folder will be printed.

Default value =False

rotate_angle_set List of float. Each float is a value in degree with which an image is rotated.

Default value =None

skip_ground_truth Boolean. Set to True if the ground truth images have been created in the manner spawn_ground_truth() spawns them. The function will continue with annotations.
significant_fraction Float. This argument takes in values between 0 and 1.0. It specifies the fraction of area relative to the area of whole matrix, above which a component will be considered as a component. Smaller than this value, the component will be treated as noise. This is an argument to the function get_connected_components().

Default value = .001

thresh Integer, from 0 to 255. This specifies the RGB values below which the ground truth color is treated converted to black, RGB=(0,0,0) and otherwise white, RGB=(255,255,255).

For example, if the value is set to 244, a pixel with (255,254,246) is converted to white since 244<255,254 and 246 while a pixel with (20,40,120) is converted to black.

Default value = 254

scale “Auto”, (Integer,Integer) or None. Annotation of images are computed from down-scaled images to improve the processing speed (unless scale=None). If set to “Auto”, the image will be downsized to (200,150) for processing.

This is an argument to the function multiple_scaled_box_positions().

Default value = “Auto”

Note: The actual image is not changed, only that the annotation positions are re-computed back from down-scaled images, giving potential loss of accuracy.

Tips: If annotation fails for one reason or another after ground truth image generation is complete, then make sure to set skip_ground_truth=True before rerunning the function, so that we do not waste time re-spawning the ground truth images.

Tips: For images whose objects are very small, setting a small scale might be a bad choice, since the position of annotation boxes might loss a lot of accuracy during rescaling.

Example usage 1.

Download the example here and put them in the working directory under the folder /bb.

import kero.ImageProcessing.photoBox as kip

this_folder = "bb\\Butterflies"
tag_folder =  "bb\\Butterflies_canvas"

gsw=kip.GreyScaleWorkShop()
rotate_angle_set = [0,30,60,90,120,150,180] # None
annotation_name = "butterfly"
gsw.clone_to_annotate(this_folder, tag_folder,1,annotation_name, check_missing_mode=False,rotate_angle_set=rotate_angle_set)

This function will call spawn_ground_truth(), i.e. create ground truth image (figure 1 top-right) of the image (figure 1 top-left) based on the corresponding image from tag folder (figure 1 top-center), and furthermore create annotations for each images. Samples of images from the folders are shown below.

gtspawn

Figure 1.

A folder containing rotated figures (figure 2) will be created. Also, a txt file will be created for each rotated copy of the image. The bounding boxes are the thin green rectangles.

annot

Figure 2.

kero version: 0.4.2 and above

spawn_ground_truth()

home > kero > Documentation

Given a folder of images and a tag folder containing the same images, marked with white color, RGB=(255,255,255), this function creates a clone of the folder and a folder of its ground truth images based on the tag folder.

kero.ImageProcessing.photoBox.py

class GreyScaleWorkShop:
  def spawn_ground_truth(self,this_folder, tag_folder,starting_label,
        order_name="img",
        tag_name="img",
        check_missing_mode=False,
        rotate_angle_set=None,
        thresh=254)
    return

 

this_folder String. The name of the folder with images.
tag_folder String. The name of the folder with images. If each image in the tag_folder has a corresponding image of the same name and type, then the rotated image clone and ground truth images will be created and relabelled accordingly.
starting_label Integer. The sequence of numbers will start with this integer.
order_name String. The clone of this_folder will be relabelled with prefix specified by this string.

Default value =”img”

tag_name String. The clone of tag_folder will be relabelled with prefix specified by this string.

Default value =”img”

check_missing_mode Boolean. If True, cloning process of the folders are not performed. The file names of images in this_folder that do not have the corresponding images in the tag_folder will be printed.

Default value =False

rotate_angle_set List of float. Each float is a value in degree with which an image is rotated.

Default value =None

thresh Integer, from 0 to 255. This specifies the RGB values below which the ground truth color is treated converted to black, RGB=(0,0,0) and otherwise white, RGB=(255,255,255).

For example, if the value is set to 244, a pixel with (255,254,246) is converted to white since 244<255,254 and 246 while a pixel with (20,40,120) is converted to black.

Default value = 254

Example usage 1

Download the example here and put them in the working directory under the folder /bb.

import kero.ImageProcessing.photoBox as kip

this_folder = "bb\\Butterflies"
tag_folder =  "bb\\Butterflies_canvas"

gsw=kip.GreyScaleWorkShop()
rotate_angle_set = [0,30,60,90,120,150,180] # None
gsw.spawn_ground_truth(this_folder, tag_folder,1, check_missing_mode=False,rotate_angle_set=rotate_angle_set)

The _canvas folder (top-middle) marks out the butterfly with white colored marker. The ground truth is produced as shown in the top-right.

gtspawn.JPG

Given a folder of images and a tag folder containing the same images, marked with white color, RGB=(255,255,255), this function creates a clone of the folder and a folder of its ground truth images based on the tag folder.

kero.ImageProcessing.photoBox.py

class GreyScaleWorkShop:
  def spawn_ground_truth(self,this_folder, tag_folder,starting_label,
        order_name="img",
        tag_name="img",
        check_missing_mode=False,
        rotate_angle_set=None,
        thresh=254)
    return

 

this_folder String. The name of the folder with images.
tag_folder String. The name of the folder with images. If each image in the tag_folder has a corresponding image of the same name and type, then the rotated image clone and ground truth images will be created and relabelled accordingly.
starting_label Integer. The sequence of numbers will start with this integer.
order_name String. The clone of this_folder will be relabelled with prefix specified by this string.

Default value =”img”

tag_name String. The clone of tag_folder will be relabelled with prefix specified by this string.

Default value =”img”

check_missing_mode Boolean. If True, cloning process of the folders are not performed. The file names of images in this_folder that do not have the corresponding images in the tag_folder will be printed.

Default value =False

rotate_angle_set List of float. Each float is a value in degree with which an image is rotated.

Default value =None

thresh Integer, from 0 to 255. This specifies the RGB values below which the ground truth color is treated converted to black, RGB=(0,0,0) and otherwise white, RGB=(255,255,255).

For example, if the value is set to 244, a pixel with (255,254,246) is converted to white since 244<255,254 and 246 while a pixel with (20,40,120) is converted to black.

Default value = 254

Example usage 1

Download the example here and put them in the working directory under the folder /bb.

import kero.ImageProcessing.photoBox as kip

this_folder = "bb\\Butterflies"
tag_folder =  "bb\\Butterflies_canvas"

gsw=kip.GreyScaleWorkShop()
rotate_angle_set = [0,30,60,90,120,150,180] # None
gsw.spawn_ground_truth(this_folder, tag_folder,1, check_missing_mode=False,rotate_angle_set=rotate_angle_set)

The _canvas folder (top-middle) marks out the butterfly with white colored marker. The ground truth is produced as shown in the top-right.

gtspawn.JPG

kero version: 0.5.1 and above

tag_rename_clone()

home > kero > Documentation

kero.ImageProcessing.photoBox.py

def tag_rename_clone(this_folder, tag_folder,starting_label,
    order_name="img",tag_name="imggt",filetype=".png",
    clone_name="_CLONE",tag_clone_name="_TAG",
    dump_folder="this_dump",
    check_missing_mode=False,
    rotate_angle_set=None):
  return

Description. Cloning folder for image pre-processing. See example usage 1.

Rotation function. When the variable rotate_angle_set is set to a list of float [a1, a2,…] , then each image in the folder will be cloned, and then copies of the image rotated with angles a1, a2, … will be created in the clone folder and labelled along the sequence of numbers as well.

this_folder String. The name of the folder with images.
tag_folder String. The name of the folder with images. If each image in the tag_folder has a corresponding image of the same name and type, then the image will be cloned into a clone folder and tag clone folder and relabelled accordingly.
starting_label Integer. The sequence of numbers will start with this integer.
order_name String. The clone of this_folder will be relabelled with prefix specified by this string.

Default value =”img”

tag_name String. The clone of tag_folder will be relabelled with prefix specified by this string.

Default value =”imggt”

filetype String. Each image in both clone folders will have file type specified by this string.

Default value =”.png”

clone_name String. The clone folder of this_folder will be named this_folder+clone_name

Default value =”_CLONE”

tag_clone_name String. The clone folder of tag_folder will be named this_folder+tag_clone_name

Default value =”_GT”

dump_folder String. The name of dump folder. Dump folder will be filled with images from this_folder that do not have corresponding images with the same name and type in the tag_folder. If this folder is empty, it will be deleted at the end.

Default value =”this_dump”

check_missing_mode Boolean. If True, cloning process of the folders are not performed. The file names of images in this_folder that do not have the corresponding images in the tag_folder will be printed.

Default value =False

rotate_angle_set List of float. Each float is a value in degree with which an image is rotated.

Default value =None

 

Example usage 1

Given a folder of images and a tag folder, this function clones both folder and relabel the images in numerical sequence. Consider the folder as shown below (left) and its tag folder (right) which is identical to the folder except each corresponding butterfly object is marked white. Download the example here and put them in the working directory under the folder /bb.

import kero.ImageProcessing.photoBox as kip

this_folder = "bb\\Butterflies"
tag_folder =  "bb\\Butterflies_canvas"

kip.tag_rename_clone(this_folder, tag_folder, 1, check_missing_mode=False)

 

bbfolder.png

The cloned and relabelled folders are shown below.

bbfolder2.png

kero version: 0.4.3 and above