11/18/2018

Elastic image effect, python opencv example source code.

Elastic effect source code.
It can be useful when you want to augment image dataset.
and it is also good to make image effect.

Please refer code:

import numpy as np
import cv2
from scipy.ndimage.interpolation import map_coordinates
from scipy.ndimage.filters import gaussian_filter

def elastic(image, alpha, sigma, random_state=None):
"""Elastic deformation of images as described in [Simard2003]_.
.. [Simard2003] Simard, Steinkraus and Platt, "Best Practices for
Convolutional Neural Networks applied to Visual Document Analysis", in
Proc. of the International Conference on Document Analysis and
Recognition, 2003.
"""
if random_state is None:
random_state = np.random.RandomState(None)

#print(random_state)
shape = image.shape
dx = gaussian_filter((random_state.rand(*shape) * 2 - 1), sigma, mode="constant", cval=0) * alpha
dy = gaussian_filter((random_state.rand(*shape) * 2 - 1), sigma, mode="constant", cval=0) * alpha
dz = np.zeros_like(dx)

x, y, z = np.meshgrid(np.arange(shape[1]), np.arange(shape[0]), np.arange(shape[2]))

indices = np.reshape(y+dy, (-1, 1)), np.reshape(x+dx, (-1, 1)), np.reshape(z, (-1, 1))
distored_image = map_coordinates(image, indices, order=1, mode='nearest') #wrap,reflect, nearest

return distored_image.reshape(image.shape)



#main
#file read
o_img = cv2.imread('izone_oy.png')
elMat = elastic(o_img, alpha=5000, sigma=8, random_state=None)

cv2.namedWindow('origin',0)
cv2.imshow('origin', o_img)
cv2.namedWindow('elastic',0)
cv2.imshow('elastic', elMat)

cv2.waitKey(0)


GitHub url :



python OpenCV, draw grid example source code

make well divided linear coordinate
And make pair coordinate

Please see code for detail explanation.


import numpy as np
import cv2
import sys

rows = 500
cols = 500
newMat_3ch = np.zeros((rows, cols, 3), dtype = "uint8") #3channel

step = 20
x = np.linspace(start=0, stop=rows, num=step)
y = np.linspace(start=0, stop=cols, num=step)

v_xy = []
h_xy = []
for i in range(step):
v_xy.append( [int(x[i]), 0, int(x[i]), rows-1] )
h_xy.append( [0, int(y[i]), cols-1, int(y[i])] )

for i in range(step):
[x1, y1, x2, y2] = v_xy[i]
[x1_, y1_, x2_, y2_] = h_xy[i]

cv2.line(newMat_3ch, (x1,y1), (x2, y2), (0,0,255),1 )
cv2.line(newMat_3ch, (x1_,y1_), (x2_, y2_), (255,0,0),1 )
cv2.namedWindow('newMat_3ch',0)
cv2.imshow('newMat_3ch', newMat_3ch)
cv2.waitKey(0)




11/15/2018

data dump by pickle in python

Data dump by pickle

see below example:

data dump to file 
import pickle
f = open("any_file.dmp', "wb")
f.write(pickle.dumps(data))
f.close()

data load by dump
data = pickle.loads(open("any_file.dmp", "rb").read())




11/13/2018

_mask.so : undefined symbol: _Py_ZeroStruct


>git clone https://github.com/cocodataset/cocoapi.git
>cd cocoapi/PythonAPI
>sudo make
*** python 2.7
>python setup.py build_ext install
***python 3.x
>python3 setup.py build_ext install
>cp -r pycocotools <path_to_tensorflow>/models/research/


11/09/2018

How to mount extra hdd permanently

> sudo -I blkid
/dev/sdb1: UUID="0687f976-6efe-4ac5-88dd-8fd863f8b8bf" TYPE="ext4" PARTUUID="1569f982-01"/dev/sda1: LABEL="cloudimg-rootfs" UUID="8ae3d910-2d2a-492d-8667-d0fa24e4d357" TYPE="ext4" PARTUUID="05ae307a-01"/dev/sdc1: UUID="e8abbf34-2cb2-471f-bac6-2a5caec81ad8" TYPE="ext4" PARTUUID="4c4db30a-01"




check uuid that you want to mount
>sudo nano /etc/fstab
Add this string end of file, but uuid must be your value, and ext4 also should be same with blkid information

UUID=e8abbf34-2cb2-471f-bac6-2a5caec81ad8 /media/datadisk ext4 defults,nofail    0       2

Thank you.



11/04/2018

OpenCV python, SuperPixel example source code. (usage of createSuperpixelSEEDS)

input image



output image

 
 


source code

import cv2 as cv
import numpy as np
import sys
import random


#read image
img = cv.imread('izone_oy.png')
converted_img = cv.cvtColor(img, cv.COLOR_BGR2HSV)

height,width,channels = converted_img.shape
num_iterations = 6
prior = 2
double_step = False
num_superpixels = 200
num_levels = 4
num_histogram_bins = 5

seeds = cv.ximgproc.createSuperpixelSEEDS(width, height, channels, num_superpixels, num_levels, prior, num_histogram_bins)
color_img = np.zeros((height,width,3), np.uint8)
color_img[:] = (0, 0, 255)
seeds.iterate(converted_img, num_iterations)

# retrieve the segmentation result
labels = seeds.getLabels()

# labels output: use the last x bits to determine the color
num_label_bits = 2
labels &= (1<<num_label_bits)-1
labels *= 1<<(16-num_label_bits)

mask = seeds.getLabelContourMask(False)

# stitch foreground & background together
mask_inv = cv.bitwise_not(mask)
result_bg = cv.bitwise_and(img, img, mask=mask_inv)
result_fg = cv.bitwise_and(color_img, color_img, mask=mask)
result = cv.add(result_bg, result_fg)

cv.namedWindow('mask',0)
cv.namedWindow('result_bg',0)
cv.namedWindow('result_fg',0)
cv.namedWindow('result',0)

cv.imshow('mask',mask_inv)
cv.imshow('result_bg',result_bg)
cv.imshow('result_fg',result_fg)
cv.imshow('result',result)

cv.imwrite('mask.jpg',mask_inv)
cv.imwrite('result_bg.jpg',result_bg)
cv.imwrite('result_fg.jpg',result_fg)
cv.imwrite('result.jpg',result)

cv.waitKey(0)


reference
https://docs.opencv.org/3.0-beta/modules/ximgproc/doc/superpixels.html
https://github.com/opencv/opencv_contrib/blob/master/samples/python2/seeds.py




TypeError: Incorrect type of self (must be 'Feature2D' or its derivative), FastFeatureDetector

This is because of opencv version difference.
Some function is changed as XX_create().
Refer to below code. This is case of FastFeatureDetector.
Thank you.

fast = cv2.FastFeatureDetector_create() # <- FastFeatureDetector()
kp = fast.detect(blurGrayMat)