diff --git a/colors_hsv_3d_line.py b/colors_hls_3d_line.py similarity index 62% rename from colors_hsv_3d_line.py rename to colors_hls_3d_line.py index 4504f9f..4f6d65d 100755 --- a/colors_hsv_3d_line.py +++ b/colors_hls_3d_line.py @@ -21,69 +21,69 @@ N = 5 -def transform(img1, img2): - res = img2.copy() +def transform(disp_img, img1, img2): + res = disp_img mask, (x,y) = pi2R.lines2d.transform(img1, img2) + res[mask] = [0.,0.,0.] -# print x,y - res[x,y] = [0.5,1.,1.] -# res[x+1,y] = [0.5,1.,1.] - res[x-1,y] = [0.5,1.,1.] - res[x,y+1] = [0.5,1.,1.] - res[x,y-1] = [0.5,1.,1.] + color = [0,255,255] + res[x,y] = color +# res[x+1,y] = color + res[x-1,y] = color + res[x,y+1] = color + res[x,y-1] = color return res, (x,y) # Load an color image in grayscale img1 = cv2.imread(name1,cv2.IMREAD_COLOR) -img1 = cv2.GaussianBlur(img1,(15,5),0) - -img1_hsv_dis = cv2.cvtColor(img1, cv2.COLOR_BGR2HSV); -img1_hsv = img1_hsv_dis.astype(np.float32)/[180.,255.,255.] +#img1 = cv2.GaussianBlur(img1,(15,5),0) +img1_hls = cv2.cvtColor(img1, cv2.COLOR_BGR2HLS); +img1_hls = img1_hls.astype(np.float32)/[180.,255.,255.] img2 = cv2.imread(name2,cv2.IMREAD_COLOR) -img2 = cv2.GaussianBlur(img2,(15,5),0) +#img2 = cv2.GaussianBlur(img2,(15,5),0) +img2_hls = cv2.cvtColor(img2, cv2.COLOR_BGR2HLS) +img2_hls = img2_hls.astype(np.float32)/[180.,255.,255.] -img2_hsv = cv2.cvtColor(img2, cv2.COLOR_BGR2HSV) +disp_img = img2[:,:,[2,1,0]] -img2_hsv_dis = img2_hsv -img2_hsv = img2_hsv.astype(np.float32)/[180.,255.,255.] - -img2_hsv_dis, xy = transform(img1_hsv, img2_hsv) -#img2_hsv_dis = img2_hsv_dis.astype(np.float32)/[180.,255.,255.] +disp_img, xy = transform(disp_img, img1_hls, img2_hls) fig = plt.figure() ax = fig.add_subplot(1,2,1) -imgplot = ax.imshow(hsv_to_rgb(img2_hsv_dis)) + +#imgplot = ax.imshow(disp_img) +imgplot = ax.imshow(pi2R.lines2d.y_data) rectangle = ax.add_patch(Rectangle((0, 0),2*N,2*N,alpha=0.2)) -(y,x,z) = img2_hsv.shape +(y,x,z) = img2.shape plt.axis([0., x, y, 0.]) ax3d = fig.add_subplot(1,2,2, projection='3d') msize = 1 -hsv_r, = ax3d.plot([], [], 'r.', markersize=msize) -hsv_b, = ax3d.plot([], [], 'b.', markersize=msize) +hls_r, = ax3d.plot([], [], 'r.', markersize=msize) +hls_b, = ax3d.plot([], [], 'b.', markersize=msize) ax3d.set_xlabel('H') -ax3d.set_ylabel('S') -ax3d.set_zlabel('V') +ax3d.set_ylabel('L') +ax3d.set_zlabel('S') r_x, r_y = 0, 0 def set_rectangle_xy(x,y, N): global r_x, r_y x, y = int(x), int(y) r_x, r_y = x, y - n_hsv1 = img1_hsv[y-N:y+N,x-N:x+N].reshape(4*N*N,3) - n_hsv2 = img2_hsv[y-N:y+N,x-N:x+N].reshape(4*N*N,3) - hsv_b.set_data(n_hsv1[:,0], n_hsv1[:,1]) - hsv_b.set_3d_properties(n_hsv1[:,2]) - hsv_r.set_data(n_hsv2[:,0], n_hsv2[:,1]) - hsv_r.set_3d_properties(n_hsv2[:,2]) + n_hls1 = img1_hls[y-N:y+N,x-N:x+N].reshape(4*N*N,3) + n_hls2 = img2_hls[y-N:y+N,x-N:x+N].reshape(4*N*N,3) + hls_b.set_data(n_hls1[:,0], n_hls1[:,1]) + hls_b.set_3d_properties(n_hls1[:,2]) + hls_r.set_data(n_hls2[:,0], n_hls2[:,1]) + hls_r.set_3d_properties(n_hls2[:,2]) rectangle.set_width(2*N) rectangle.set_height(2*N) rectangle.set_xy(np.array([x-N,y-N])) diff --git a/pi2R/lines.py b/pi2R/lines.py index 7601ba1..f307198 100644 --- a/pi2R/lines.py +++ b/pi2R/lines.py @@ -108,10 +108,9 @@ def compute_points_3d_(self, points_2d): class Line: def __init__(self, normal_image_name, laser_image_name): self.n_img = cv2.imread(normal_image_name, cv2.IMREAD_COLOR) - self.n_img = cv2.GaussianBlur(self.n_img,(15,5),0) - + #self.n_img = cv2.GaussianBlur(self.n_img,(15,5),0) self.l_img = cv2.imread(laser_image_name, cv2.IMREAD_COLOR) - self.l_img = cv2.GaussianBlur(self.l_img,(15,5),0) + #self.l_img = cv2.GaussianBlur(self.l_img,(15,5),0) def get_points_2d_(self): diff --git a/pi2R/lines2d.py b/pi2R/lines2d.py index f4a4035..11a8165 100644 --- a/pi2R/lines2d.py +++ b/pi2R/lines2d.py @@ -4,6 +4,7 @@ import numpy as np import cv2 import math +import scipy.ndimage as ndimage from scipy.signal import argrelmax, argrelmin from scipy.ndimage.filters import gaussian_filter,gaussian_filter1d @@ -18,12 +19,12 @@ def bright_mask(h1,s1,v1,h2,s2,v2): mask_satur = s2+0.05 < s1 return np.logical_and(np.logical_and(mask_bright1, mask_bright2),mask_satur) -def max_laser(y, h1,v1,s1,h2,v2,s2, not_mask): +def max_laser(y, d, not_mask): start = timer() - y[not_mask] = -1. -# y = gaussian_filter1d(y, sigma=2, axis=1) - y = gaussian_filter(y, sigma=2) - y[not_mask] = -1. + y[not_mask] = d + y = gaussian_filter1d(y, sigma=2, axis=1) +# y = gaussian_filter(y, sigma=2) +# y[not_mask] = d retu = argrelmax(y, axis=1) end = timer() print 'max_laser time:',end - start @@ -37,31 +38,34 @@ def red_mask(h1,s1,v1,h2,s2,v2): mask_red2 = -d > h2 return np.logical_or(mask_red1, mask_red2) -def wrap_red(h): - h[h>0.5]-=1. - return h - def split(img): - return wrap_red(img[:,:,0]),img[:,:,1],img[:,:,2] + return img[:,:,0],img[:,:,1],img[:,:,2] +y_data = [] def transform(img1, img2): + global y_data start = timer() - h1,s1,v1 = split(img1) - h2,s2,v2 = split(img2) - - y = (v2-v1)**2 + (s2-s1)**2+(h2-h1)**2 - - mask_brighter = v2 > v1+0.07 - mask_brighter = np.logical_and(y > 0.01,mask_brighter) - -# mask_b = bright_mask(h1,s1,v1, h2,s2,v2) -# mask_red = red_mask(h1,s1,v1, h2,s2,v2) + h1,l1,s1 = split(img1) + h2,l2,s2 = split(img2) + +# l1 = ndimage.gaussian_filter1d(l1, 3, axis=1) +# l2 = ndimage.gaussian_filter1d(l2, 3, axis=1) +# l1 = ndimage.gaussian_filter1d(l1, 3, axis=0) +# l2 = ndimage.gaussian_filter1d(l2, 3, axis=0) +# s1 = ndimage.gaussian_filter1d(l1, 3, axis=1) +# s2 = ndimage.gaussian_filter1d(l2, 3, axis=1) + y_data = l2-l1#+(s2-s1)*0.3 +# w = l2 > 0.4 +# y_data[w] += (s2-s1)[w] + d = 0.00 +# mask_brighter = np.logical_and(y > d, l2 > 0.15) + mask_brighter = y_data > d + (sy,sx,sz) = img2.shape + mask_brighter[sy-10:sy,:] = False -# mask = np.logical_and(np.logical_or(mask_b,mask_brighter), mask_red) -# mask_not = np.logical_not(mask) mask_not = np.logical_not(mask_brighter) - x,y = max_laser(y, h1,v1,s1,h2, v2, s2, mask_not) + x,y = max_laser(y_data, d, mask_not) end = timer() print 'transform time:',end - start @@ -70,10 +74,10 @@ def transform(img1, img2): def get_points_2d_g(img1, img2): start = timer() - img1_hsv = cv2.cvtColor(img1, cv2.COLOR_BGR2HSV); + img1_hsv = cv2.cvtColor(img1, cv2.COLOR_BGR2HLS); img1_hsv = img1_hsv.astype(np.float32)/[180.,255.,255.] - img2_hsv = cv2.cvtColor(img2, cv2.COLOR_BGR2HSV) + img2_hsv = cv2.cvtColor(img2, cv2.COLOR_BGR2HLS) img2_hsv = img2_hsv.astype(np.float32)/[180.,255.,255.] image, (x,y) = transform(img1_hsv, img2_hsv) end = timer()