In [1]:
%load_ext autoreload
%autoreload 2

In [2]:
import cv2
import numpy as np
from cv_utils import *
from tests.test_simple import *
from matplotlib import pyplot as plt

%matplotlib inline

In [7]:
image_data = helper_getimage("cake.jpg")

In [11]:
helper_writetoTEST_DATA("resources/cake_rgb.jpg", convert_to_rgb(image_data))

In [5]:
convert_to_rgb??

In [12]:
expected = helper_getimage("cake_rgb.jpg")
plt.imshow(expected)


Out[12]:
<matplotlib.image.AxesImage at 0x7fce8cf8c750>

In [13]:
image_data = helper_getimage("cake.jpg")
expected = helper_getimage("cake_rgb.jpg")
result = cv_utils.convert_to_rgb(image_data)

In [15]:
plt.imshow(image_data)
plt.show()
plt.imshow(expected)
plt.show()
plt.imshow(result)
plt.show()



In [21]:
image_data = helper_getimage("cake.jpg")
plt.imshow(image_data)
plt.show()
result = cv_utils.convert_to_hsv(image_data)
plt.imshow(result)
plt.show()



In [ ]:


In [28]:
image_data = helper_getimage("cake.jpg")
plt.imshow(image_data)
plt.show()

result = cv_utils.convert_to_gray(image_data)
plt.imshow(result, cmap="gray")
plt.show()



In [29]:
helper_writetoTEST_DATA("resources/cake_gray.jpg", result)

In [34]:
#image data is bgr, plt expects rgb, so plt draws a bluetone in red colors
image_data = helper_getimage("cake.jpg")
plt.imshow(image_data)
plt.show()

result = cv_utils.convert_bgr_to_bluetone(image_data)
plt.imshow(result)
plt.show()



In [35]:
#image data is bgr, plt expects rgb, so plt draws a bluetone in red colors
image_data = helper_getimage("cake.jpg")
plt.imshow(image_data)
plt.show()

result = cv_utils.convert_bgr_to_greentone(image_data)
plt.imshow(result)
plt.show()



In [37]:
#image data is bgr, plt expects rgb, so plt draws a redtone in blue colors
image_data = helper_getimage("cake.jpg")
plt.imshow(image_data)
plt.show()

result = cv_utils.convert_bgr_to_redtone(image_data)
plt.imshow(result)
plt.show()



In [96]:
data[:,:,2].shape


Out[96]:
(255, 255)

In [102]:
data = np.array([np.repeat(np.expand_dims(np.arange(0, 255, 1), 1), 255, 1)
,np.repeat(np.expand_dims(np.arange(0, 255, 1), 1), 255, 1).T
,np.ones((255,255))])
data = np.swapaxes(data, 0, 1)
data = np.swapaxes(data, 1, 2)
data = data.astype(np.uint8)

plt.imshow(cv_utils.convert_bgr_to_bluetone(data))
plt.show()
plt.imshow(cv_utils.convert_bgr_to_greentone(data))
plt.show()
plt.imshow(cv_utils.convert_bgr_to_redtone(data))
plt.show()



In [132]:
data.shape


Out[132]:
(255, 255, 3)

In [3]:
data = helper_dummychannels()

plt.imshow(cv_utils.convert_bgr_to_bluetone(data))
plt.show()
plt.imshow(cv_utils.convert_bgr_to_greentone(data))
plt.show()
plt.imshow(cv_utils.convert_bgr_to_redtone(data))
plt.show()



In [6]:
np.repeat(255, 2,0)


Out[6]:
array([255, 255])

In [7]:
np.repeat(255, 256, 0)


Out[7]:
array([255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
       255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
       255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
       255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
       255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
       255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
       255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
       255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
       255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
       255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
       255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
       255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
       255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
       255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
       255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
       255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
       255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
       255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
       255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
       255, 255, 255, 255, 255, 255, 255, 255, 255])

In [8]:
np.repeat(256, 256, 0)


Out[8]:
array([256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256,
       256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256,
       256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256,
       256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256,
       256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256,
       256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256,
       256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256,
       256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256,
       256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256,
       256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256,
       256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256,
       256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256,
       256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256,
       256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256,
       256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256,
       256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256,
       256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256,
       256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256,
       256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256,
       256, 256, 256, 256, 256, 256, 256, 256, 256])

In [9]:
np.all(np.equal(np.repeat(256, 256, 0), compute_histogram_3channel(data)[0]))


Out[9]:
True

In [10]:
np.repeat(256, 256, 0)


Out[10]:
array([256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256,
       256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256,
       256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256,
       256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256,
       256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256,
       256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256,
       256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256,
       256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256,
       256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256,
       256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256,
       256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256,
       256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256,
       256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256,
       256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256,
       256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256,
       256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256,
       256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256,
       256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256,
       256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256,
       256, 256, 256, 256, 256, 256, 256, 256, 256])

In [11]:
expected = np.zeros((256))
expected[-1] = 65536 # 256*256
expected


Out[11]:
array([     0.,      0.,      0.,      0.,      0.,      0.,      0.,
            0.,      0.,      0.,      0.,      0.,      0.,      0.,
            0.,      0.,      0.,      0.,      0.,      0.,      0.,
            0.,      0.,      0.,      0.,      0.,      0.,      0.,
            0.,      0.,      0.,      0.,      0.,      0.,      0.,
            0.,      0.,      0.,      0.,      0.,      0.,      0.,
            0.,      0.,      0.,      0.,      0.,      0.,      0.,
            0.,      0.,      0.,      0.,      0.,      0.,      0.,
            0.,      0.,      0.,      0.,      0.,      0.,      0.,
            0.,      0.,      0.,      0.,      0.,      0.,      0.,
            0.,      0.,      0.,      0.,      0.,      0.,      0.,
            0.,      0.,      0.,      0.,      0.,      0.,      0.,
            0.,      0.,      0.,      0.,      0.,      0.,      0.,
            0.,      0.,      0.,      0.,      0.,      0.,      0.,
            0.,      0.,      0.,      0.,      0.,      0.,      0.,
            0.,      0.,      0.,      0.,      0.,      0.,      0.,
            0.,      0.,      0.,      0.,      0.,      0.,      0.,
            0.,      0.,      0.,      0.,      0.,      0.,      0.,
            0.,      0.,      0.,      0.,      0.,      0.,      0.,
            0.,      0.,      0.,      0.,      0.,      0.,      0.,
            0.,      0.,      0.,      0.,      0.,      0.,      0.,
            0.,      0.,      0.,      0.,      0.,      0.,      0.,
            0.,      0.,      0.,      0.,      0.,      0.,      0.,
            0.,      0.,      0.,      0.,      0.,      0.,      0.,
            0.,      0.,      0.,      0.,      0.,      0.,      0.,
            0.,      0.,      0.,      0.,      0.,      0.,      0.,
            0.,      0.,      0.,      0.,      0.,      0.,      0.,
            0.,      0.,      0.,      0.,      0.,      0.,      0.,
            0.,      0.,      0.,      0.,      0.,      0.,      0.,
            0.,      0.,      0.,      0.,      0.,      0.,      0.,
            0.,      0.,      0.,      0.,      0.,      0.,      0.,
            0.,      0.,      0.,      0.,      0.,      0.,      0.,
            0.,      0.,      0.,      0.,      0.,      0.,      0.,
            0.,      0.,      0.,      0.,      0.,      0.,      0.,
            0.,      0.,      0.,      0.,      0.,      0.,      0.,
            0.,      0.,      0.,      0.,      0.,      0.,      0.,
            0.,      0.,      0.,  65536.])

In [12]:
compute_histogram_3channel(data)[2]


Out[12]:
array([     0.,      0.,      0.,      0.,      0.,      0.,      0.,
            0.,      0.,      0.,      0.,      0.,      0.,      0.,
            0.,      0.,      0.,      0.,      0.,      0.,      0.,
            0.,      0.,      0.,      0.,      0.,      0.,      0.,
            0.,      0.,      0.,      0.,      0.,      0.,      0.,
            0.,      0.,      0.,      0.,      0.,      0.,      0.,
            0.,      0.,      0.,      0.,      0.,      0.,      0.,
            0.,      0.,      0.,      0.,      0.,      0.,      0.,
            0.,      0.,      0.,      0.,      0.,      0.,      0.,
            0.,      0.,      0.,      0.,      0.,      0.,      0.,
            0.,      0.,      0.,      0.,      0.,      0.,      0.,
            0.,      0.,      0.,      0.,      0.,      0.,      0.,
            0.,      0.,      0.,      0.,      0.,      0.,      0.,
            0.,      0.,      0.,      0.,      0.,      0.,      0.,
            0.,      0.,      0.,      0.,      0.,      0.,      0.,
            0.,      0.,      0.,      0.,      0.,      0.,      0.,
            0.,      0.,      0.,      0.,      0.,      0.,      0.,
            0.,      0.,      0.,      0.,      0.,      0.,      0.,
            0.,      0.,      0.,      0.,      0.,      0.,      0.,
            0.,      0.,      0.,      0.,      0.,      0.,      0.,
            0.,      0.,      0.,      0.,      0.,      0.,      0.,
            0.,      0.,      0.,      0.,      0.,      0.,      0.,
            0.,      0.,      0.,      0.,      0.,      0.,      0.,
            0.,      0.,      0.,      0.,      0.,      0.,      0.,
            0.,      0.,      0.,      0.,      0.,      0.,      0.,
            0.,      0.,      0.,      0.,      0.,      0.,      0.,
            0.,      0.,      0.,      0.,      0.,      0.,      0.,
            0.,      0.,      0.,      0.,      0.,      0.,      0.,
            0.,      0.,      0.,      0.,      0.,      0.,      0.,
            0.,      0.,      0.,      0.,      0.,      0.,      0.,
            0.,      0.,      0.,      0.,      0.,      0.,      0.,
            0.,      0.,      0.,      0.,      0.,      0.,      0.,
            0.,      0.,      0.,      0.,      0.,      0.,      0.,
            0.,      0.,      0.,      0.,      0.,      0.,      0.,
            0.,      0.,      0.,      0.,      0.,      0.,      0.,
            0.,      0.,      0.,      0.,      0.,      0.,      0.,
            0.,      0.,      0.,  65536.], dtype=float32)

In [13]:
%%timeit
np.random.rand(10000,10000, 20)


1 loop, best of 3: 13.8 s per loop

In [14]:
%%timeit
np.random.rand(10000,10000, 20).ravel()


1 loop, best of 3: 13.7 s per loop

In [15]:
%%timeit
np.random.rand(10000,10000, 20).flatten()


1 loop, best of 3: 15.8 s per loop

In [23]:
np.bincount(data.ravel())


Out[23]:
array([  512,   512,   512,   512,   512,   512,   512,   512,   512,
         512,   512,   512,   512,   512,   512,   512,   512,   512,
         512,   512,   512,   512,   512,   512,   512,   512,   512,
         512,   512,   512,   512,   512,   512,   512,   512,   512,
         512,   512,   512,   512,   512,   512,   512,   512,   512,
         512,   512,   512,   512,   512,   512,   512,   512,   512,
         512,   512,   512,   512,   512,   512,   512,   512,   512,
         512,   512,   512,   512,   512,   512,   512,   512,   512,
         512,   512,   512,   512,   512,   512,   512,   512,   512,
         512,   512,   512,   512,   512,   512,   512,   512,   512,
         512,   512,   512,   512,   512,   512,   512,   512,   512,
         512,   512,   512,   512,   512,   512,   512,   512,   512,
         512,   512,   512,   512,   512,   512,   512,   512,   512,
         512,   512,   512,   512,   512,   512,   512,   512,   512,
         512,   512,   512,   512,   512,   512,   512,   512,   512,
         512,   512,   512,   512,   512,   512,   512,   512,   512,
         512,   512,   512,   512,   512,   512,   512,   512,   512,
         512,   512,   512,   512,   512,   512,   512,   512,   512,
         512,   512,   512,   512,   512,   512,   512,   512,   512,
         512,   512,   512,   512,   512,   512,   512,   512,   512,
         512,   512,   512,   512,   512,   512,   512,   512,   512,
         512,   512,   512,   512,   512,   512,   512,   512,   512,
         512,   512,   512,   512,   512,   512,   512,   512,   512,
         512,   512,   512,   512,   512,   512,   512,   512,   512,
         512,   512,   512,   512,   512,   512,   512,   512,   512,
         512,   512,   512,   512,   512,   512,   512,   512,   512,
         512,   512,   512,   512,   512,   512,   512,   512,   512,
         512,   512,   512,   512,   512,   512,   512,   512,   512,
         512,   512,   512, 66048])

In [20]:
expected = np.repeat(512, 256, 0)
expected[-1] = 66048 #(256*256)+512

In [30]:
np.all(np.equal(compute_histogram_1channel(data), expected))


Out[30]:
True

In [36]:
n_steps = 1
[x for x in range(n_steps)]


Out[36]:
[0]

In [46]:
n_steps = 1
init_arr = [-1]
init_arr= reduce(lambda arr, v: arr.append(v*v), range(n_steps), init_arr)
init_arr


Out[46]:
[-1, 0]

In [85]:
np.arange(3,0,-1)


Out[85]:
array([3, 2, 1])

In [106]:
min((23,44))


Out[106]:
23

In [99]:
np.linspace(1,3,3)


Out[99]:
array([ 1.,  2.,  3.])

In [110]:
v = 256
s = 1 + 1
map(lambda x: v * (0.5**(x-1)), np.linspace(1,s,s))


Out[110]:
[256.0, 128.0]

In [112]:
result = cv_utils.compute_downsamples(data,8)
shapes = map(lambda x: x.shape, result)
shapes


Out[112]:
[(256, 256, 3),
 (128, 128, 3),
 (64, 64, 3),
 (32, 32, 3),
 (16, 16, 3),
 (8, 8, 3),
 (4, 4, 3),
 (2, 2, 3),
 (1, 1, 3)]

In [111]:
expected = cv_utils.compute_downsamples(data,8)
print(len(expected))
for image in expected:
    plt.imshow(image)
    plt.show()


9

In [113]:
result = cv_utils.compute_upsamples(data,2)
shapes = map(lambda x: x.shape, result)
shapes


Out[113]:
[(256, 256, 3), (512, 512, 3), (1024, 1024, 3)]

In [118]:
expected = cv_utils.compute_upsamples(data,2)
print(len(expected))
for image in expected:
    plt.imshow(image)
    plt.show()


3

In [15]:
print(123)


123

In [51]:
cv_utils.coords_pixel_to_local((25.6,51.2), data)


Out[51]:
(0.10000000000000001, 0.20000000000000001)

In [52]:
cv_utils.coords_pixel_to_local((-1,51.2), data)


Out[52]:
(0.0, 0.20000000000000001)

In [53]:
cv_utils.coords_pixel_to_local((128,512), data)


Out[53]:
(0.5, 1.0)

In [70]:
cv_utils.coords_local_to_pixel((0.1, 0.2), data)


Out[70]:
(25, 51)

In [71]:
cv_utils.coords_local_to_pixel((0.1, 1.0), data)


Out[71]:
(25, 256)

In [72]:
cv_utils.coords_local_to_pixel((-1.0, 2.0), data)


Out[72]:
(0, 256)

In [84]:
np.sqrt((2**2)+(2**2))


Out[84]:
2.8284271247461903

In [93]:
np.subtract((1,2), (3,0))


Out[93]:
array([-2,  2])

In [89]:
np.linalg.norm([(1, 0),(3, 2)])


Out[89]:
3.7416573867739413

In [102]:
cv_utils.distance_betweencoords((1,1), (-1, -1))


Out[102]:
2.8284271247461903

In [146]:
plt.imshow(data)


Out[146]:
<matplotlib.image.AxesImage at 0x7f36734b74d0>

In [156]:
helper_writetoTEST_DATA("resources/dummy_data_rect_fill.jpg",cv_utils.draw_rectangle(data, (10,100), (128, 200), (255,127,0), -1))

In [157]:
helper_writetoTEST_DATA("resources/dummy_data_rect_line.jpg",cv_utils.draw_rectangle(data, (10,100), (128, 200), (255,127,0), 10))

In [158]:
dummy_data = helper_dummychannels()

result = cv_utils.draw_rectangle(dummy_data, (10,100), (128, 200), (255,127,0), -1)
plt.imshow(result)


Out[158]:
<matplotlib.image.AxesImage at 0x7f36730bf090>

In [216]:
res = cv_utils.draw_circle(cv_utils.draw_circle(data, (128,128), 64, (255,255,0), 10), (128,128), 50, (255,127,0), -1)

In [217]:
plt.imshow(res)


Out[217]:
<matplotlib.image.AxesImage at 0x7f3670d269d0>

In [218]:
helper_writetoTEST_DATA("resources/circle_and_arc.jpg", res)

In [220]:
plt.imshow(helper_getimage("circle_and_arc.jpg"))


Out[220]:
<matplotlib.image.AxesImage at 0x7f3670c623d0>

In [226]:
np.all(np.equal(result, helper_getimage("circle_and_arc.jpg")))


Out[226]:
True

In [224]:
dummy_data = helper_dummychannels()
#Lets draw a circle with no fill
result = cv_utils.draw_circle(dummy_data, (128,128), 64, (255,255,0), 10)

# Lets draw a circle inside of it
result = cv_utils.draw_circle(result, (128,128), 50, (255,127,0), -1)

plt.imshow(result)


Out[224]:
<matplotlib.image.AxesImage at 0x7f3670c14cd0>

In [235]:
helper_writetoTEST_DATA("resources/line.jpg", res)

In [234]:
res = cv_utils.draw_line(data, (0,0), (128,128), (255,220,0), 10)
plt.imshow(res)


Out[234]:
<matplotlib.image.AxesImage at 0x7f36706f9410>

In [67]:
helper_writetoTEST_DATA("resources/polygons_openclose.jpg", res)

In [80]:
np.all(np.equal(res,result))


Out[80]:
True

In [74]:
#these are same coordinates but with a different order
pts = np.array([
    [50,50],
    [100, 50],
    [150, 150],
    [50, 150],
    ], np.int32)

result = cv_utils.draw_polygon(data, pts.tolist(), (255, 255, 0),5,True)

pts = pts + 10

result = cv_utils.draw_polygon(result, pts.tolist(), (255, 127, 0),5,False)

plt.imshow(result)


Out[74]:
<matplotlib.image.AxesImage at 0x7f67f3995350>

In [5]:
all_zeros_rgb = np.zeros((512,512,3), np.uint8)

#these are same coordinates but with a different order
pts = np.array([
    [400,50], 
    [130,200],
    [145,500],
    [10,400]
    ], np.int32)

pts = pts.reshape((-1,1,2))

cv2.polylines(all_zeros_rgb, [pts], True, (255,127,0), 5)
plt.imshow(all_zeros_rgb)


Out[5]:
<matplotlib.image.AxesImage at 0x7f67f65a99d0>

In [104]:
res = cv_utils.translate_inpixels(data, 20,20)
plt.imshow(res)
plt.show()



In [107]:
#helper_writetoTEST_DATA("resources/translated_1.jpg", res)
#helper_writetoTEST_DATA("resources/translated_2.jpg", res)

In [106]:
res = cv_utils.translate_inpixels(data, -20,-20)
plt.imshow(res)
plt.show()



In [111]:
res = cv_utils.translate_inlocal(data, 0.5, 0.5)
plt.imshow(res)
plt.show()



In [114]:
#helper_writetoTEST_DATA("resources/translated_3.jpg", res)
#helper_writetoTEST_DATA("resources/translated_4.jpg", res)

In [113]:
res = cv_utils.translate_inlocal(data, -0.5, 0.5)
plt.imshow(res)
plt.show()



In [122]:
data = helper_getimage("cake.jpg")

In [132]:
helper_writetoTEST_DATA("resources/cake_crop.jpg", res)

In [130]:
res = cv_utils.crop_inpixels(data, (400,80), (650,490))
plt.imshow(data)
plt.show()
plt.imshow(res)
plt.show()



In [136]:
map(int, (1.2, 1.0))


Out[136]:
[1, 1]

In [147]:
res = cv_utils.crop_inlocal(data, (0.5,0.5), (1.0,1.0))
plt.imshow(data)
plt.show()
plt.imshow(res)
plt.show()



In [148]:
helper_writetoTEST_DATA("resources/cake_crop2.jpg", res)

In [139]:
height, width = data.shape[:2]

print(height, width)

topl_coord, bottomr_coord = (0.5,1.0), (0.5,1.0)

print([topl_coord[0] * width, topl_coord[1]*height])
print([bottomr_coord[0] * width, bottomr_coord[1]*height])


(612, 816)
[408.0, 612.0]
[408.0, 612.0]

In [149]:
data = helper_dummychannels()

In [153]:
plt.imshow(data)


Out[153]:
<matplotlib.image.AxesImage at 0x7f67f0ccff10>

In [161]:
res = cv_utils.rotate(data, 90, pivot_coords=None, scale=1.0)
plt.imshow(res)
plt.show()
#helper_writetoTEST_DATA("resources/rotate_90.jpg", res)
res = cv_utils.rotate(data, -45, pivot_coords=None, scale=1.0)
plt.imshow(res)
plt.show()
#helper_writetoTEST_DATA("resources/rotate_45.jpg", res)
res = cv_utils.rotate(data, 45, pivot_coords=(0,0), scale=1.0)
plt.imshow(res)
plt.show()
#helper_writetoTEST_DATA("resources/rotate_45_corner.jpg", res)
res = cv_utils.rotate(data, 45, pivot_coords=None, scale=0.5)
plt.imshow(res)
plt.show()
helper_writetoTEST_DATA("resources/rotate_45_scale.jpg", res)



In [165]:
plt.imshow(data)
plt.show()
res = cv_utils.flip(data, 0)
plt.imshow(res)
plt.show()
#helper_writetoTEST_DATA("resources/flip_horiz.jpg", res)
res = cv_utils.flip(data, 1)
plt.imshow(res)
plt.show()
#helper_writetoTEST_DATA("resources/flip_vert.jpg", res)



In [3]:
data = helper_dummychannels()
plt.imshow(data)
plt.show()



In [16]:


In [13]:
res = cv_utils.scale_inlocal(data, 0.5, 0.75)
plt.imshow(res)
plt.show()
#helper_writetoTEST_DATA("resources/scale1.jpg", res)
res = cv_utils.scale_inpixels(data, 300, 300)
plt.imshow(res)
plt.show()
#helper_writetoTEST_DATA("resources/scale2.jpg", res)



In [639]:
mask1 = np.zeros((256,256), np.uint8)
mask2 = np.zeros((256,256), np.uint8)

cv2.rectangle(mask1, (0, 0), (168, 168), 255, -2)
cv2.rectangle(mask2, (98, 98), (256, 256), 255, -2)

plt.imshow(mask1)
plt.show()
plt.imshow(mask2)
plt.show()



In [640]:
And = cv2.bitwise_and(mask1, mask2)
plt.imshow(And)


Out[640]:
<matplotlib.image.AxesImage at 0x7fec7fa66c10>

In [641]:
bitwiseOr = cv2.bitwise_or(mask1, mask2)
plt.imshow(bitwiseOr)


Out[641]:
<matplotlib.image.AxesImage at 0x7fed8a8cdf90>

In [642]:
bitwiseXor = cv2.bitwise_xor(mask1, mask2)
plt.imshow(bitwiseXor)


Out[642]:
<matplotlib.image.AxesImage at 0x7fed8103e210>

In [14]:
bitwiseNot_sq = cv2.bitwise_not(mask1)
plt.imshow(bitwiseNot_sq)


Out[14]:
<matplotlib.image.AxesImage at 0x7fb40d3f7690>

In [643]:
#helper_writetoTEST_DATA("resources/mask1.jpg", mask1)
#helper_writetoTEST_DATA("resources/mask2.jpg", mask2)
#helper_writetoTEST_DATA("resources/and.jpg", And)
#helper_writetoTEST_DATA("resources/or.jpg", bitwiseOr)
helper_writetoTEST_DATA("resources/xor.jpg", bitwiseXor)
#helper_writetoTEST_DATA("resources/not.jpg", bitwiseNot_sq)

In [17]:
mask1 = helper_getimage("mask1.jpg")
mask2 = helper_getimage("mask2.jpg")
expected = helper_getimage("and.jpg")
result = cv_utils.bit_and(mask1, mask2)

In [26]:
plt.imshow(data)
plt.show()
res = cv_utils.apply_mask(data, And)
plt.imshow(res)
plt.show()
#helper_writetoTEST_DATA("resources/and_apply.jpg", res)



In [31]:
data = helper_getimage("cake.jpg")
plt.imshow(data)
plt.show()



In [74]:
res = cv_utils.blur_box(data, 100)
plt.imshow(res)
plt.show()
#helper_writetoTEST_DATA("resources/blur_box_100.jpg", res)
res = cv_utils.blur_box(data, 7)
plt.imshow(res)
plt.show()
#helper_writetoTEST_DATA("resources/blur_box_7.jpg", res)



In [81]:
res = cv_utils.blur_gausian(data, 101)
plt.imshow(res)
plt.show()
#helper_writetoTEST_DATA("resources/blur_gau_101.jpg", res)

res = cv_utils.blur_gausian(data, 7)
plt.imshow(res)
plt.show()
#helper_writetoTEST_DATA("resources/blur_gau_7.jpg", res)



In [86]:
res = cv_utils.blur_median(data, 101)
plt.imshow(res)
plt.show()
#helper_writetoTEST_DATA("resources/blur_median_101.jpg", res)

res = cv_utils.blur_median(data, 7)
plt.imshow(res)
plt.show()
#helper_writetoTEST_DATA("resources/blur_median_7.jpg", res)



In [101]:
res = cv_utils.blur_bilateral(data, 9, 75, 75)
plt.imshow(res)
plt.show()
helper_writetoTEST_DATA("resources/blur_bilat.jpg", res)



In [135]:
dst = cv2.fastNlMeansDenoisingColored(data, None, 6, 6, 7, 21)

plt.imshow(dst)


Out[135]:
<matplotlib.image.AxesImage at 0x7fb4040c6e10>

In [143]:
res = cv_utils.denoise(data, 1)
plt.imshow(res)
#helper_writetoTEST_DATA("resources/denoise1.jpg", res)


Out[143]:
<matplotlib.image.AxesImage at 0x7fb3ff252190>

In [144]:
res = cv_utils.denoise(data, 33)
plt.imshow(res)
#helper_writetoTEST_DATA("resources/denoise2.jpg", res)


Out[144]:
<matplotlib.image.AxesImage at 0x7fb3ff147d50>

In [146]:
res = cv_utils.sharpen(data)
plt.imshow(res)
#helper_writetoTEST_DATA("resources/sharpen.jpg", res)



In [302]:
data = helper_dummychannels()
plt.imshow(data)


Out[302]:
<matplotlib.image.AxesImage at 0x7fb3fe665090>

In [303]:
res = cv_utils.threshold_binary(data, 80, 255)
plt.imshow(res)
plt.show()
#helper_writetoTEST_DATA("resources/tresh_binary255.jpg", res)
res = cv_utils.threshold_binary(data, 80, 100)
plt.imshow(res)
plt.show()
#helper_writetoTEST_DATA("resources/tresh_binary100.jpg", res)



In [304]:
res = cv_utils.threshold_binary_inverse(data, 80, 255)
plt.imshow(res)
plt.show()
#helper_writetoTEST_DATA("resources/tresh_binaryi255.jpg", res)
res = cv_utils.threshold_binary_inverse(data, 80, 100)
plt.imshow(res)
plt.show()
#helper_writetoTEST_DATA("resources/tresh_binaryi100.jpg", res)



In [305]:
res = cv_utils.threshold_truncate(data, 200, 255)
plt.imshow(res)
plt.show()
#helper_writetoTEST_DATA("resources/tresh_trunc200.jpg", res)
res = cv_utils.threshold_truncate(data, 140, 255)
plt.imshow(res)
plt.show()
#helper_writetoTEST_DATA("resources/tresh_trunc140.jpg", res)



In [306]:
res = cv_utils.threshold_tozero(data, 127, 255)
plt.imshow(res)
plt.show()
#helper_writetoTEST_DATA("resources/tresh_tozero.jpg", res)



In [307]:
res = cv_utils.threshold_tozero_inverse(data, 127, 255)
plt.imshow(res)
plt.show()
#helper_writetoTEST_DATA("resources/tresh_tozeroi.jpg", res)



In [251]:
cv2.adaptiveThreshold

In [308]:
data = helper_getimage("cake.jpg")
data = cv_utils.convert_to_gray(data)
plt.imshow(data)


Out[308]:
<matplotlib.image.AxesImage at 0x7fb3fe179350>

In [309]:
res = cv_utils.threshold_adaptive(data)
plt.imshow(res)
plt.show()
#helper_writetoTEST_DATA("resources/tresh_ada.jpg", res)
res = cv_utils.threshold_adaptive(data, blocksize=7)
plt.imshow(res)
plt.show()
#helper_writetoTEST_DATA("resources/tresh_ada7.jpg", res)
res = cv_utils.threshold_adaptive(data, blocksize=15)
plt.imshow(res)
plt.show()
#helper_writetoTEST_DATA("resources/tresh_ada15.jpg", res)



In [ ]:
cv2.threshold

In [311]:
res = cv_utils.threshold_otsu(data, 240)
plt.imshow(res)
plt.show()
#helper_writetoTEST_DATA("resources/tresh_otsu.jpg", res)
res = cv_utils.threshold_gaussianotsu(data, 240)
plt.imshow(res)
plt.show()
#helper_writetoTEST_DATA("resources/tresh_gotsu.jpg", res)



In [336]:
data = helper_getimage("hello.png")
_, data = cv2.threshold(data, 0, 255, cv2.THRESH_BINARY_INV)
plt.imshow(data)


Out[336]:
<matplotlib.image.AxesImage at 0x7fb3fdfa8c50>

In [340]:
res = cv_utils.morph_dilate(data, 5)
plt.imshow(res)
plt.show()
#helper_writetoTEST_DATA("resources/dilate_5.jpg", res)
res = cv_utils.morph_erode(data, 5)
plt.imshow(res)
plt.show()
#helper_writetoTEST_DATA("resources/erode_5.jpg", res)
res = cv_utils.morph_open(data, 5)
plt.imshow(res)
plt.show()
#helper_writetoTEST_DATA("resources/open_5.jpg", res)
res = cv_utils.morph_close(data, 5)
plt.imshow(res)
plt.show()
#helper_writetoTEST_DATA("resources/close_5.jpg", res)
res = cv_utils.morph_dilate(data, (5,5))
plt.imshow(res)
plt.show()
#helper_writetoTEST_DATA("resources/dilate_5x5.jpg", res)
res = cv_utils.morph_erode(data, (5,5))
plt.imshow(res)
plt.show()
#helper_writetoTEST_DATA("resources/erode_5x5.jpg", res)
res = cv_utils.morph_open(data, (5,5))
plt.imshow(res)
plt.show()
#helper_writetoTEST_DATA("resources/open_5x5.jpg", res)
res = cv_utils.morph_close(data, (5,5))
plt.imshow(res)
plt.show()
#helper_writetoTEST_DATA("resources/close_5x5.jpg", res)



In [343]:
data = helper_getimage("car.jpg")
plt.imshow(data)


Out[343]:
<matplotlib.image.AxesImage at 0x7fb3fe04c390>

In [346]:
res = cv_utils.edge_sobel_x(data, 5)
plt.imshow(res)
plt.show()
#helper_writetoTEST_DATA("resources/dilate_5.jpg", res)



In [359]:
res = cv_utils.edge_sobel_x(data, 5)
plt.imshow(res, cmap="gray")
plt.show()
helper_writetoTEST_DATA("resources/edge_sobelx.jpg", res)
res = cv_utils.edge_sobel_y(data, 5)
plt.imshow(res, cmap="gray")
plt.show()
helper_writetoTEST_DATA("resources/edge_sobely.jpg", res)
res = cv_utils.edge_laplacian(data)
plt.imshow(res, cmap="gray")
plt.show()
helper_writetoTEST_DATA("resources/edge_laplacian.jpg", res)
res = cv_utils.edge_canny(data, 50, 120)
plt.imshow(res, cmap="gray")
plt.show()
helper_writetoTEST_DATA("resources/edge_canny.jpg", res)
res = cv_utils.edge_canny_blur(data, 50, 120)
plt.imshow(res, cmap="gray")
plt.show()
helper_writetoTEST_DATA("resources/edge_canny_blur.jpg", res)



In [397]:
data = helper_getimage("affine.jpg")
plt.imshow(data)


Out[397]:
<matplotlib.image.AxesImage at 0x7fb3fd83df90>

In [398]:
points_A = np.float32([[320,15], [700,215], [85,610]])
points_B = np.float32([[0,0], [420,0], [0,594]])

res = cv_utils.perspective_affine(data, points_A, points_B)
plt.imshow(res)
plt.show()
helper_writetoTEST_DATA("resources/affine2.jpg", res)



In [400]:
data = helper_getimage("keyboard.jpg")
plt.imshow(data)


Out[400]:
<matplotlib.image.AxesImage at 0x7fb3fd87e210>

In [402]:
points_A = np.float32([[255,65], [935,330], [50,220], [730,700]])
points_B = np.float32([[0,0], [700,0], [0,300], [700,300]])
newimage_image_size = (700, 300)

res = cv_utils.perspective_nonaffine(data, points_A, points_B, newimage_image_size)
plt.imshow(res)
plt.show()
#helper_writetoTEST_DATA("resources/nonaffine.jpg", res)
res = cv_utils.perspective_nonaffine(data, points_A, points_B)
plt.imshow(res)
plt.show()
#helper_writetoTEST_DATA("resources/nonaffine2.jpg", res)



In [36]:
# Lets create our image
image_data = np.zeros((400,930,3), np.uint8) + 255

cv2.rectangle(image_data, (50,100), (250,300), (255,127,0), -1)
#cv2.rectangle(image_data, (260,100), (460,300), (0,127,255), -1)
cv2.circle(image_data, (360,200), 100, (0,127,255), -1)
cv2.rectangle(image_data, (500,130), (640,270), (100,255,0), -1)
cv2.rectangle(image_data, (680, 100), (880, 300), (100,255,255), -1)

plt.imshow(image_data)

#helper_writetoTEST_DATA("resources/color_shapes.jpg", image_data)



In [26]:
data = helper_getimage("color_shapes.jpg")
plt.imshow(data)


Out[26]:
<matplotlib.image.AxesImage at 0x7f603a049750>

In [22]:
c, h =  cv_utils.compute_contours(data, 254, 255)

In [37]:
image_data = data.copy()
cv2.drawContours(data, c, -1, (0,0,255), 10)
plt.imshow(data)


Out[37]:
<matplotlib.image.AxesImage at 0x7f6039737a90>

In [38]:
helper_writetoTEST_DATA("resources/color_shapes_2.jpg", data)

In [30]:
plt.imshow(data)


Out[30]:
<matplotlib.image.AxesImage at 0x7f6039c91f90>

In [14]:
map(cv_utils.contour_centroid, c)


Out[14]:
[(569, 199), (359, 199), (779, 199), (149, 199)]

In [15]:
map(cv_utils.contour_areas, c)


Out[15]:
[19878.0, 31460.0, 40398.0, 40398.0]

In [ ]:


In [ ]:


In [ ]:


In [ ]:


In [9]:
image_data = helper_getimage("color_shapes.jpg")
expected = [19878.0, 31460.0, 40398.0, 40398.0]

contours, hierarchy = cv_utils.compute_contours(image_data, 254, 255)
result = map(cv_utils.contour_areas, contours)

print(expected)
print(result)

assert np.all(np.equal(result, expected))
#assert False


[19878.0, 31460.0, 40398.0, 40398.0]
[19878.0, 31460.0, 40398.0, 40398.0]

In [63]:
plt.imshow(image_data)


Out[63]:
<matplotlib.image.AxesImage at 0x7fdf5cff2210>

In [64]:
result = map(cv_utils.contour_areas, contours)
result


Out[64]:
[19878.0, 31460.0, 40398.0, 40398.0]

In [39]:
plt.imshow(helper_getimage("color_shapes.jpg"))
plt.show()
plt.imshow(helper_getimage("color_shapes_2.jpg"))
plt.show()



In [43]:
data = helper_getimage("house.png")
plt.imshow(data)


Out[43]:
<matplotlib.image.AxesImage at 0x7f6039908990>

In [71]:
contours, hier = cv_utils.compute_contours(data, 127,255)
plt.imshow(cv_utils.contours_highlight(data, contours))


Out[71]:
<matplotlib.image.AxesImage at 0x7f6038e25950>

In [72]:
rect_coords = cv_utils.contour_boundingbox(contours[0])
plt.imshow(cv_utils.draw_rectangle(data, rect_coords[0], rect_coords[1], (0,255,0), 1))
rect_coords


Out[72]:
((149, 254), (197, 335))

In [73]:
map(cv_utils.contour_boundingbox, contours)


Out[73]:
[((149, 254), (197, 335)),
 ((148, 254), (197, 335)),
 ((107, 238), (214, 344)),
 ((97, 150), (263, 344)),
 ((79, 136), (80, 138)),
 ((75, 135), (78, 138)),
 ((79, 29), (251, 136)),
 ((64, 10), (278, 363))]

In [101]:
from functools import partial
approxs = [ cv_utils.contour_approximation(x,0.001) for x in contours ]
res = cv_utils.contours_highlight(data, approxs[:])
plt.imshow(res)


Out[101]:
<matplotlib.image.AxesImage at 0x7f6017fabc10>

In [107]:
#helper_writetoTEST_DATA("resources/house3.png", res)

In [138]:
data = helper_getimage("flower3.png")
plt.imshow(data)


Out[138]:
<matplotlib.image.AxesImage at 0x7f6016e66190>

In [133]:
contours, hier = cv_utils.compute_contours(data, 176, 255)
plt.imshow(cv_utils.contours_highlight(data, contours[:4]))


Out[133]:
<matplotlib.image.AxesImage at 0x7f6016fee850>

In [139]:
hulls = map(cv_utils.contour_hull_approximation, contours)
res = cv_utils.contours_highlight(data, hulls)
plt.imshow(res)
helper_writetoTEST_DATA("resources/flower4.png", res)



In [155]:
data = helper_getimage("shapes.png")
plt.imshow(data)


Out[155]:
<matplotlib.image.AxesImage at 0x7f6016553d50>

In [169]:



Out[169]:
array([(20, 80), (60, 50), 100.8, (80, 120), (40, 120)], dtype=object)

In [215]:
data = helper_dummychannels()
data.shape


Out[215]:
(256, 256, 3)

In [221]:
cv_utils.draw_rectangle?

In [11]:
data = np.zeros((200,500,3), np.uint8)
#data = helper_dummychannels()

pts = np.array([
      [100,50],
      [50, 150],
      [150, 150],
      ], np.int32)

data = cv_utils.draw_polygon(data, pts, (0,255,0), thickness=1, isClosed=True, filled=True)
data = cv_utils.draw_rectangle(data, (200, 50), (300, 150), (0,255,0), thickness=-1)
data = cv_utils.draw_circle(data, (400, 100), 50, (0,255,0), thickness=-1)

plt.imshow(data)

#helper_writetoTEST_DATA("resources/shapes3", data)



In [12]:
template_data = np.zeros((200,200,3), np.uint8)

pts = np.array([
      [130,50],
      [50, 150],
      [140, 150],
      ], np.int32)

template_data = cv_utils.draw_polygon(template_data, pts, (0,255,0), thickness=1, isClosed=True, filled=True)

plt.imshow(template_data)

#helper_writetoTEST_DATA("resources/shapes4", template_data)



In [13]:
template_c, template_h = cv_utils.compute_contours(template_data, 0, 255)
plt.imshow(cv_utils.contours_highlight(template_data, template_c, (255,0,0)))


Out[13]:
<matplotlib.image.AxesImage at 0x7fed8e761cd0>

In [14]:
search_c, search_h = cv_utils.compute_contours(data, 0, 255)
len(search_c)
plt.imshow(cv_utils.contours_highlight(data, search_c, (255,0,0)))


Out[14]:
<matplotlib.image.AxesImage at 0x7fed8e6ce790>

In [320]:
[ cv_utils.match_shape(x, template_c[0], cv2.CONTOURS_MATCH_I3) for x in search_c]


Out[320]:
[0.18902099881889753, 6.19450632585382, 0.1594613138288294]

In [ ]:

I need some help to understand the output of OpenCV's cv2.matchShapes. I read its documentation but I don't really know what Hu invariants are and I think that might be part of my problem.

I am using matchShapes to compare a skewed triangle to a set of 3 shapes (a triangle, a circle and a square). My problem is that the most similar seems to be either the square, or the circle.

My scenario: I have an image1: [ TRIANGLE SQUARE CIRCLE ]

Another image2: [ SKEWED_TRIANGLE ]

In the end I am getting how similiar the contour in image 2 is to each contour in image 1: [0.189, 6.194, 0.159]

Now, I ahave a bunch of questions:

a) How should I read the "match" metric? (I am assuming bigger is better)

b) should I just clip values to some range? Maybe only values in [0, 1] are relevant?

c) Am I doing something wrong in the code? (I tried the 3 different methods and different thresholds to canny edges)

Below is the code I am using.


In [1]:
import numpy as np
import cv2
import matplotlib.pyplot as plt 

## CREATING IMAGE 1 - A triangle, a square and a circle, next to each other.
im_1 = np.zeros((200,500,3), np.uint8)
#data = helper_dummychannels()

pts = np.array([
      [100,50],
      [50, 150],
      [150, 150],
      ], np.int32)

pts = pts.reshape((-1,1,2))
cv2.fillPoly(im_1, [pts], (0,255,0))
cv2.rectangle(im_1, (200,50), (300, 150), (0,255,0), -1)
cv2.circle(im_1, (400, 100), 50, (0,255,0), -1)
plt.imshow(im_1) ## data has a triangle, rectangle and circle next to one another
plt.show()


## CREATING IMAGE 2 - A slightly skewed triangle
im_2 = np.zeros((200,200,3), np.uint8)

pts = np.array([
      [130,50],
      [50, 150],
      [140, 150],
      ], np.int32)

pts = pts.reshape((-1,1,2))

cv2.fillPoly(im_2, [pts], (0,255,0))

plt.imshow(im_2)
plt.show()


# GRAYSCALING TO USE IN CANNY FILTER
gray_1 = cv2.cvtColor(im_1, cv2.COLOR_BGR2GRAY)
gray_2 = cv2.cvtColor(im_2, cv2.COLOR_BGR2GRAY)

#plt.imshow(gray_1)
#plt.show()
#plt.imshow(gray_2)
#plt.show()


# CANNY EDGES AND CONTOURS
edged_1 = cv2.Canny(gray_1, 30, 200)
_, contours_1, hierarchy_1 = cv2.findContours(edged_1, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)

edged_2 = cv2.Canny(gray_2, 30, 200)
_, contours_2, hierarchy_2 = cv2.findContours(edged_2, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)


## Making sure the contours are ok
plt.imshow(cv2.drawContours(im_1.copy(), contours_1, -1, (255,0,0), 5))
plt.show()
plt.imshow(cv2.drawContours(im_2.copy(), contours_2, -1, (255,0,0), 5))
plt.show()


## COMPUTING cv2.matchShapes - Which of the 3 shapes is most similar to the skewed triangle?
#matches = [ cv2.matchShapes(contours_2[0], x, cv2.CONTOURS_MATCH_I1, 0.0) for x in contours_1 ]
#matches = [ cv2.matchShapes(contours_2[0], x, cv2.CONTOURS_MATCH_I2, 0.0) for x in contours_1 ]
matches = [ cv2.matchShapes(contours_2[0], x, cv2.CONTOURS_MATCH_I3, 0.0) for x in contours_1 ]
print(matches)


[0.18902099881889753, 6.19450632585382, 0.1594613138288294]

In [33]:
data = helper_getimage("skyscrapper.jpg")
data = cv_utils.convert_to_gray(data)
data = cv_utils.edge_canny(data, 50, 150)
plt.imshow(data)


Out[33]:
<matplotlib.image.AxesImage at 0x7fed8a472dd0>

In [34]:
lines = cv_utils.match_hough_lines(data, 223)

res = helper_getimage("skyscrapper.jpg")
map(lambda x: cv2.line(res,x[0],x[1],(0,0,255),2), lines)

plt.imshow(res)

#helper_writetoTEST_DATA("resources/skyscrapper3.jpg", res)



In [38]:
lines = cv_utils.match_hough_lines(data, 450)

res = helper_getimage("skyscrapper.jpg")
map(lambda x: cv2.line(res,x[0],x[1],(0,0,255),2), lines)

plt.imshow(res)

#helper_writetoTEST_DATA("resources/skyscrapper3.jpg", res)


Out[38]:
<matplotlib.image.AxesImage at 0x7fed8d209310>

In [40]:


In [46]:
data = helper_getimage("skyscrapper2.jpg")
data = cv_utils.convert_to_gray(data)
data = cv_utils.edge_canny(data, 100, 170)
plt.imshow(data)


Out[46]:
<matplotlib.image.AxesImage at 0x7fed8d06a450>

In [94]:


In [93]:
lines = cv_utils.match_prob_hough_lines(data, 700, 100, 5)

res = helper_getimage("skyscrapper2.jpg")
map(lambda x: cv2.line(res,x[0],x[1],(0,0,255),2), lines)
plt.imshow(res)

#helper_writetoTEST_DATA("resources/skyscrapper4.jpg", res)
lines


Out[93]:
[[(1111, 422), (1251, 331)],
 [(873, 773), (992, 696)],
 [(969, 510), (1081, 437)],
 [(962, 524), (1089, 441)]]

In [190]:
data = helper_getimage("circles.jpg")
data = cv_utils.crop_inlocal(data, [0,0], [0.37 ,0.37])
data = cv_utils.convert_to_gray(data)
data = cv_utils.threshold_binary(data, 10, 255)
plt.imshow(data)


Out[190]:
<matplotlib.image.AxesImage at 0x7fed83d61e90>

In [191]:
centers, radius = cv_utils.match_circle(data, 1)
res = helper_getimage("circles.jpg")
res = cv_utils.crop_inlocal(res, [0,0], [0.37 ,0.37])
map(lambda x: cv2.circle(res,x[0],x[1],(255,0,255),15), zip(centers, radius))
plt.imshow(res)

helper_writetoTEST_DATA("resources/circles2.jpg", res)



In [194]:
[x for x in zip(centers, radius)]


Out[194]:
[((199, 199), 25), ((199, 40), 25), ((40, 40), 24), ((119, 40), 25)]

In [195]:
data = helper_getimage("cereals2.jpg")
plt.imshow(data)


Out[195]:
<matplotlib.image.AxesImage at 0x7fed83d19950>

In [230]:
centers, radius = cv_utils.match_blobs(data)

In [233]:
centers = [map(int, x) for x in centers]
radius = map(int, radius)
[x for x in zip(centers,radius)]


Out[233]:
[([883, 442], 25),
 ([340, 279], 21),
 ([781, 436], 25),
 ([175, 385], 26),
 ([67, 301], 24)]

In [229]:
res = helper_getimage("cereals2.jpg")
map(lambda x: cv2.circle(res,x[0],x[1],(255,0,255),15), zip(centers, radius))
plt.imshow(res)

#helper_writetoTEST_DATA("resources/circles2.jpg", res)


--------------------------------------------
SystemErrorTraceback (most recent call last)
<ipython-input-229-2179035afe9b> in <module>()
      1 res = helper_getimage("cereals2.jpg")
----> 2 map(lambda x: cv2.circle(res,x[0],x[1],(255,0,255),15), zip(centers, radius))
      3 plt.imshow(res)
      4 
      5 #helper_writetoTEST_DATA("resources/circles2.jpg", res)

<ipython-input-229-2179035afe9b> in <lambda>(x)
      1 res = helper_getimage("cereals2.jpg")
----> 2 map(lambda x: cv2.circle(res,x[0],x[1],(255,0,255),15), zip(centers, radius))
      3 plt.imshow(res)
      4 
      5 #helper_writetoTEST_DATA("resources/circles2.jpg", res)

SystemError: new style getargs format but argument is not a tuple

In [287]:
# Load image then grayscale
image_data = helper_getimage('car.jpg')
image_data = cv_utils.crop_inlocal(image_data, (0.5, 0.1), (1.0,0.6))
plt.imshow(image_data)
image_data.shape


Out[287]:
(306, 408, 3)

In [284]:
harris_corners = cv_utils.describe_corners(image_data, 3 ,3 ,0.05)
harris_corners = cv2.dilate(harris_corners, None)
image_data[harris_corners>0.025*harris_corners.max()]=[255,127,0]
plt.imshow(image_data)
#helper_writetoTEST_DATA("resources/harris_corners.jpg", image_data)



In [312]:
# Load image then grayscale
image_data = helper_getimage('car.jpg')
#image_data = cv_utils.crop_inlocal(image_data, (0.5, 0.1), (1.0,0.6))
plt.imshow(image_data)
image_data.shape


Out[312]:
(612, 816, 3)

In [323]:
corners = cv_utils.describe_good2track(image_data, 300)
corners


Out[323]:
array([[ 499.,  324.],
       [ 225.,  532.],
       [  66.,  179.],
       [ 791.,  566.],
       [ 784.,  189.],
       [ 403.,   19.]], dtype=float32)

In [364]:
# Load image then grayscale
image_data = helper_getimage('car.jpg')
image_data = cv_utils.crop_inlocal(image_data, (0.65, 0.0), (1.0,0.15))
plt.imshow(image_data)
image_data.shape


Out[364]:
(91, 286, 3)

In [365]:
c, r, k = cv_utils.describe_FAST(image_data)

In [366]:
plt.imshow(cv2.drawKeypoints(image_data.copy(), k, image_data.copy(), flags=cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS))


Out[366]:
<matplotlib.image.AxesImage at 0x7fed8341ca90>

In [398]:
# Load image then grayscale
image_data = helper_getimage('car.jpg')
image_data = cv_utils.crop_inlocal(image_data, (0.7, 0.1), (1.0,0.6))
plt.imshow(image_data)


Out[398]:
<matplotlib.image.AxesImage at 0x7fed82da4e10>

In [401]:
c, r, k = cv_utils.describe_BRIEF(image_data)

In [402]:
plt.imshow(cv2.drawKeypoints(image_data.copy(), k, image_data.copy(), flags=cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS))


Out[402]:
<matplotlib.image.AxesImage at 0x7fed8278b5d0>

In [440]:
# Load image then grayscale
image_data = helper_getimage('car.jpg')
image_data = cv_utils.crop_inlocal(image_data, (0.7, 0.24), (0.9,0.35))
plt.imshow(image_data)


Out[440]:
<matplotlib.image.AxesImage at 0x7fed82e5ecd0>

In [444]:
len(c)


Out[444]:
18

In [442]:
c, r, k = cv_utils.describe_ORB(image_data)

In [443]:
plt.imshow(cv2.drawKeypoints(image_data.copy(), k, image_data.copy(), flags=cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS))


Out[443]:
<matplotlib.image.AxesImage at 0x7fed81a8a0d0>

In [466]:
data = helper_getimage("car.jpg")
plt.imshow(data)


Out[466]:
<matplotlib.image.AxesImage at 0x7fed8116c750>

In [467]:
gradients, hog_feats = cv_utils.describe_HOG(data)

In [468]:
for i in range(gradients.shape[-1]):
    plt.imshow(gradients[:,:,i])
    plt.show()



In [469]:
data.shape, (data.shape[0] // 9, data.shape[1] // 9)


Out[469]:
((612, 816, 3), (68, 90))

In [470]:
gradients.shape


Out[470]:
(76, 102, 9)

In [476]:
from cv_utils import download as cv_download

In [477]:
cv_download.haar_frontfacedefault("haarcascade_frontalface_default.xml")
cv_download.haar_eye("haarcascade_eye.xml")


Downloading HAARCASCADE_FRONTALFACE_DEFAULT
Downloading HAARCASCADE_FRONTALFACE_DEFAULT
Out[477]:
'haarcascade_eye.xml'

In [504]:
data = helper_getimage("Patrick_Stewart_and_Hugh_Jackman_Press_Conference_Logan_Berlinale_2017_01.jpg")
plt.imshow(data)


Out[504]:
<matplotlib.image.AxesImage at 0x7fedf4366610>

In [516]:
faces


Out[516]:
[[(489, 124), (688, 323)], [(237, 260), (427, 450)]]

In [515]:
faces = cv_utils.match_face(data)

In [514]:
rects = map(lambda x : cv_utils.draw_rectangle(data, x[0], x[1], (255,0,255), 2), faces)
plt.imshow(rects[1])


Out[514]:
<matplotlib.image.AxesImage at 0x7fed8ab07050>

In [533]:
eyes = cv_utils.match_eyes(data)

In [542]:
rects = map(lambda x : cv_utils.draw_rectangle(data, x[0], x[1], (0,255,255), 2), eyes)
plt.imshow(rects[1])


Out[542]:
<matplotlib.image.AxesImage at 0x7fed817f5450>

In [540]:
eyes[0]


Out[540]:
[(527, 190), (569, 232)]

In [550]:
cv_download.dlib_facelandmarks("shape_predictor_68_face_landmarks.dat")


Downloading Face Landmarks
Out[550]:
'shape_predictor_68_face_landmarks.dat'

In [554]:
!pip install dlib


Collecting dlib
  Downloading dlib-19.6.1.tar.gz (4.0MB)
    100% |################################| 4.0MB 452kB/s ta 0:00:011
Building wheels for collected packages: dlib
  Running setup.py bdist_wheel for dlib ... done
  Stored in directory: /root/.cache/pip/wheels/e3/42/35/b66446e9f5c02ced3f3824ce21765b63090b089b43bb383f20
Successfully built dlib
Installing collected packages: dlib
Successfully installed dlib-19.6.1

In [555]:
import dlib

In [556]:
data = helper_getimage("Patrick_Stewart_and_Hugh_Jackman_Press_Conference_Logan_Berlinale_2017_01.jpg")
plt.imshow(data)


Out[556]:
<matplotlib.image.AxesImage at 0x7fed81653ed0>

In [561]:
landmarks = cv_utils.match_facial_landmarks(data)

In [590]:
l = landmarks[0]
tuple(l.tolist()[0])


Out[590]:
(518, 213)

In [593]:
[cv2.circle(data, tuple(x), 5, (0,255,0)) for x in landmarks[0].tolist()]
plt.imshow(data)


Out[593]:
<matplotlib.image.AxesImage at 0x7fed80e167d0>

In [594]:
[cv2.circle(data, tuple(x), 5, (0,255,0)) for x in landmarks[1].tolist()]
plt.imshow(data)


Out[594]:
<matplotlib.image.AxesImage at 0x7fed80dc72d0>

In [75]:
data = helper_getimage("cake.jpg")
plt.imshow(data)


Out[75]:
<matplotlib.image.AxesImage at 0x7f36c0442b50>

In [76]:
res = cv_utils.compute_grabcut(data, (300,0,700,500), 3)
plt.imshow(res)


Out[76]:
<matplotlib.image.AxesImage at 0x7f36c0332650>

In [77]:
helper_writetoTEST_DATA("resources/cake_mask2.jpg", res)

In [78]:
plt.imshow(helper_getimage("cake_mask2.jpg"))


Out[78]:
<matplotlib.image.AxesImage at 0x7f36c0296990>

In [79]:
np.all(np.equal(res, helper_getimage("cake_mask2.jpg")))


Out[79]:
False

In [68]:
np.random.seed(7)
print(np.random.randint(1000000))
np.random.seed()
print(np.random.randint(1000000))


585903
764077