6/10/2019

OpenCV -> PIL -> resize -> OpenCV

Some simple code for this processing

1. Read image by OpenCV
2. Conver from OpenCV to PIL image
3. some processing using PIL, ex)resize
4. Conver from PIL to OpenCV

Check this code.


import cv2
from PIL import Image
import numpy

#target resize
r_x = 100
r_y = 100


#read image using opencv
cv_img_o = cv2.imread('A.png')

#conver mat to pil
cv_img = cv2.cvtColor(cv_img_o, cv2.COLOR_BGR2RGB)
im_pil = Image.fromarray(cv_img)

#resize pil
im_pil = im_pil.resize((r_x,r_y), Image.ANTIALIAS)

#convert pil to mat
cv_img_r = numpy.array(im_pil)

# Convert RGB to BGR
cv_img_r = cv2.cvtColor(cv_img_r, cv2.COLOR_RGB2BGR)
#cv_img_r = cv_img_r[:, :, ::-1].copy()

cv2.namedWindow('origin',0)
cv2.imshow('origin', cv_img_o)

cv2.namedWindow('resize',0)
cv2.imshow('resize', cv_img_r)

cv2.waitKey(0)

4/22/2019

OpenCV Simple Background Subtraction Example code


Step #1 
Simple background subtraction




code here

.



Step #2
remove noise + binary

code here

.

Step #3 & #4
Draw contour

Remove small area and draw rect



core here:

.



Thank you
☕️

3/19/2019

Python hstack, vstack example code

import numpy as np

#hstack #1
a = np.array((1,2,3))
b = np.array((2,3,4))
print(np.hstack((a,b)))
>
 [1 2 3 2 3 4]

#hstack #2
a = np.array([[1],[2],[3]])
b = np.array([[2],[3],[4]])
print(np.hstack((a,b)))
>
 [[1 2]
 [2 3]
 [3 4]]

#vstack #1
a = np.array([1, 2, 3])
b = np.array([2, 3, 4])
print(np.vstack((a,b)))
>
 [[1 2 3]
 [2 3 4]]


#vstack #2
a = np.array([[1], [2], [3]])
b = np.array([[2], [3], [4]])
print(np.vstack((a,b)))
>
[[1]
 [2]
 [3]
 [2]
 [3]
 [4]]

3/07/2019

python number list, remove duplication and sort

coordiX = [0, 5016, 40, 5012, 40, 5012, 40, 5012, 40, 5012, 3169, 4970, 3169, 4970, 3169, 4970, 3169, 3537, 3586, 4355, 4395, 4970, 2632, 4616, 2632, 4616, 2632, 4616, 2632, 4616, 3651, 3659, 3651, 3659, 3651, 3659, 3651, 3659, 2630, 4614, 2630, 4614, 2630, 4614, 2630, 4614, 2632, 4616, 2632, 4616, 2632, 4616, 2632, 4616, 2630, 4614, 2630, 4614, 2630, 4614, 2630, 4614, 2632, 4614, 2632, 4614, 2632, 4614, 2632, 4614, 2630, 4614, 2630, 4614, 2630, 4614, 2630, 4614, 2630, 2640, 2630, 2640, 2630, 2640, 2630, 2640, 3652, 3660, 3652, 3660, 3652, 3660, 3652, 3660, 328, 4670, 328, 4670, 328, 4670, 328, 4670, 328, 4668, 328, 4668, 328, 4668, 328, 4668, 330, 4614, 330, 4614, 330, 2962, 330, 784, 808, 1206, 2694, 2962, 2692, 3868, 2692, 3404, 3428, 3868, 332, 866]

print("origin")
print(coordiX)
print("length:", len(coordiX))

print("remove duplication")
coordiX = list(set(coordiX))
print(coordiX)
print("length:", len(coordiX))

print("sort")
coordiX.sort()
print(coordiX)

output
origin
[0, 5016, 40, 5012, 40, 5012, 40, 5012, 40, 5012, 3169, 4970, 3169, 4970, 3169, 4970, 3169, 3537, 3586, 4355, 4395, 4970, 2632, 4616, 2632, 4616, 2632, 4616, 2632, 4616, 3651, 3659, 3651, 3659, 3651, 3659, 3651, 3659, 2630, 4614, 2630, 4614, 2630, 4614, 2630, 4614, 2632, 4616, 2632, 4616, 2632, 4616, 2632, 4616, 2630, 4614, 2630, 4614, 2630, 4614, 2630, 4614, 2632, 4614, 2632, 4614, 2632, 4614, 2632, 4614, 2630, 4614, 2630, 4614, 2630, 4614, 2630, 4614, 2630, 2640, 2630, 2640, 2630, 2640, 2630, 2640, 3652, 3660, 3652, 3660, 3652, 3660, 3652, 3660, 328, 4670, 328, 4670, 328, 4670, 328, 4670, 328, 4668, 328, 4668, 328, 4668, 328, 4668, 330, 4614, 330, 4614, 330, 2962, 330, 784, 808, 1206, 2694, 2962, 2692, 3868, 2692, 3404, 3428, 3868, 332, 866]
length: 130

remove duplication
[0, 3586, 4355, 2692, 4614, 2694, 4616, 784, 2962, 5012, 5016, 3868, 40, 808, 4395, 1206, 4668, 4670, 3651, 3652, 2630, 2632, 328, 330, 3659, 3660, 3404, 332, 2640, 3537, 3169, 866, 3428, 4970]
length: 34

sort
[0, 40, 328, 330, 332, 784, 808, 866, 1206, 2630, 2632, 2640, 2692, 2694, 2962, 3169, 3404, 3428, 3537, 3586, 3651, 3652, 3659, 3660, 3868, 4355, 4395, 4614, 4616, 4668, 4670, 4970, 5012, 5016]

python 2d array and rows and cols

#make array
W = 2
H = 3


list2d = []
for i in range(0,H):
w_list =[]
for j in range (0,W):
w_list.append((i,j))
list2d.append(w_list)


#print 2d array
print(list2d)


#get row and col
Height = Rows = len(list2d)
Width = Cols = len(list2d[0])


#check values
print( Rows, Cols)
print( Height, Width)

#print all elements
for i in range(0,Rows):
for j in range (0,Cols):
print(list2d[i][j])


output
[[(0, 0), (0, 1)], [(1, 0), (1, 1)], [(2, 0), (2, 1)]]
3 2
3 2
(0, 0)
(0, 1)
(1, 0)
(1, 1)
(2, 0)
(2, 1)