Esempio n. 1
0
    def Harris_Corner(self):
        self.threshold = 0.999999999999
        temp_i = self.image_i.copy()
        temp1_i = self.image_i.copy()
        gray_i = cv2.cvtColor(temp_i, cv2.COLOR_BGR2GRAY)
        gray_i = numpy.float32(gray_i)
        dst_i = cv2.cornerHarris(gray_i, 2, 3, 0.025)
        dst_i = cv2.dilate(dst_i, None)
        # Threshold for an optimal value, it may vary depending on the image.
        temp_i[dst_i < 0.01 * dst_i.max()] = [0, 0, 0]
        temp1_i[dst_i > 0.01 * dst_i.max()] = [0, 0, 255]
        hist_i = cv2.calcHist([temp_i], [0], None, [256], [0, 256])
        temp_j = self.image_j.copy()
        temp1_j = self.image_j.copy()
        gray_j = cv2.cvtColor(temp_j, cv2.COLOR_BGR2GRAY)
        gray_j = numpy.float32(gray_j)
        dst_j = cv2.cornerHarris(gray_j, 2, 3, 0.025)
        dst_j = cv2.dilate(dst_j, None)
        # Threshold for an optimal value, it may vary depending on the image.
        temp_j[dst_j < 0.01 * dst_j.max()] = [0, 0, 0]
        temp1_j[dst_j > 0.01 * dst_j.max()] = [0, 0, 255]
        hist_j = cv2.calcHist([temp_j], [0], None, [256], [0, 256])

        self.measure = cv2.compareHist(hist_i, hist_j, cv.CV_COMP_CORREL)
        self.assertGreater(self.measure, self.threshold)

        print self.measure
Esempio n. 2
0
def TipDetector(I):
    
    I.flags.writeable = True
    
    # Convert RGB to YUV
    Y=0.3*I[:,:,2]+0.6*I[:,:,1]+0.1*I[:,:,0]
    V=0.4375*I[:,:,2]-0.375*I[:,:,1]-0.0625*I[:,:,0]
    U=-0.15*I[:,:,2]-0.3*I[:,:,1]+0.45*I[:,:,0]

    # Find pink
    M=np.ones((np.shape(I)[0], np.shape(I)[1]), np.uint8)*255
    for i in range(0,np.shape(I)[0]):
        for j in range(0,np.shape(I)[1]):
            if V[i,j]>15 and U[i,j]>-7:
                M[i,j]=0
    kernel = np.ones((5,5),np.uint8)   
    M = cv2.morphologyEx(M, cv2.MORPH_OPEN, kernel)
    M=cv2.GaussianBlur(M,(7,7),8)
    
    # find Harris corners in pink mask
    dst = cv2.cornerHarris(M,5,3,0.04)
    dst = cv2.dilate(dst,None)
    ret, dst = cv2.threshold(dst,0.7*dst.max(),255,0)
    dst = np.uint8(dst)
    E = np.where(dst > 0.01*dst.max())
    
    # find Harris corners in image
    gray1 = cv2.cvtColor(I,cv2.COLOR_BGR2GRAY)
    gray1 = np.float32(gray1)
    dst1 = cv2.cornerHarris(gray1,3,3,0.04)
    dst1 = cv2.dilate(dst1,None)
    ret1, dst1 = cv2.threshold(dst1,0.01*dst1.max(),255,0)
    dst1 = np.uint8(dst1)
    E1 = np.where(dst1 > 0.01*dst1.max())

    # no tip identified  
    if not E or not E1:
        return [0,0]
    
    # Rearrange the coordinates in more readable format
    ind1 = np.lexsort((E1[1],E1[0]))
    C1=[(E1[1][i],E1[0][i]) for i in ind1]
    ind = np.lexsort((E[1],E[0]))
    C=[(E[1][i],E[0][i]) for i in ind]
    
    # Identify the tip
    D=[]
    for i in range(1,np.shape(C1)[0]):
        for j in range(1,np.shape(C)[0]):
       	    if abs(C1[i][0]-C[j][0])<5 and abs(C1[i][1]-C[j][1])<5:
                D.append([int(np.uint(C1[i][0]*2)), int(np.uint(C1[i][1]*2))])
    if not D:
        return [0,0]
    else:
        return count(D)
Esempio n. 3
0
def cartoon_detection():
    num_down = 2
    num_bilateral = 7

    img_rgb = cv2.imread('Z:/Cartographer/kyoto.jpg')

    # downsampling using Gaussian pyramid
    img_color = img_rgb
    for _ in xrange(num_down):
        img_color = cv2.pyrDown(img_color)

    # small bilateral filter repeatedly applied
    for _ in xrange(num_bilateral):
        img_color = cv2.bilateralFilter(img_color, 29, 20, 7)

    # upsample to original size
    for _ in xrange(num_down):
        img_color = cv2.pyrUp(img_color)

    # convert to gray
    gray = cv2.cvtColor(img_rgb, cv2.COLOR_RGB2GRAY)
    gray = np.float32(gray)

    # detect corners
    dst = cv2.cornerHarris(gray,2,3,0.04)

    dst = cv2.dilate(dst, None)
    print(dst)

    img_rgb[dst>0.01*dst.max()] = [0,0,255]
    
    # show
    cv2.imshow('dst', img_rgb)
    if cv2.waitKey(0) & 0xff == 27:
        cv2.destroyAllWindows()
def Get_wall_coordinates(source, dest):

    x = source;
    y = dest;

# *******************************Image Processing for identifying corners in the image**********************************
    image = cv2.imread(x)

    gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
    gray = np.float32(gray)
    dst = cv2.cornerHarris(gray, 2, 3, 0.04)
    image[dst > 0.01 * dst.max()] = [0, 0, 255]

    cv2.imwrite('coordinateImage.jpg', image)

# *******************************Getting the pixel values of the detected corners***************************************
    getCoord = np.where(np.all(image == (0, 0, 255), axis=-1))
    coordinates = zip(getCoord[0], getCoord[1])
    x = np.array(coordinates, dtype="int")
    print ("Coordinates : ")
    print (x)

# ******************************* Generating the text file *************************************************************
    filename1 = open(y, "w")
    filename1.write(str(coordinates))
    filename1.close()

    #plt.scatter(getCoord[0], getCoord[1])
    #plt.show()
Esempio n. 5
0
def cornerpoints(filename,thres=0.1):

    img = cv2.imread(filename)
    gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)

    gray = np.float32(gray)

    if gray.shape!=(512,512):
        gray = cv2.resize(gray, (512,512), interpolation=cv2.INTER_LINEAR)
    dst = cv2.cornerHarris(gray,2,3,0.04)

    #result is dilated for marking the corners, not important
    dst = cv2.dilate(dst,None)

    cords = []
    nrow, ncol = dst.shape
    #print nrow,ncol
    gap = min(nrow,ncol)*0.1

    lasti=lastj=-2
    for i in range(nrow):
        for j in range(ncol):
            #print i,j
            if dst[i,j]>thres*dst.max():
                if max(abs(i-lasti),abs(j-lastj))>gap:
                    cords.append((i,j))
                    lasti=i
                    lastj=j
                    #print i,j
    
    

    return cords
Esempio n. 6
0
def harris_corners_edge(img):
    img_grey = cv2.cvtColor(img, cv.CV_BGR2GRAY)
    img_grey = cv2.Canny(img_grey, 100, 200)
    dst = cv2.cornerHarris(img_grey, 2, 3, 0.04)
    dst = cv2.dilate(dst,None)
    img[dst>0.01*dst.max()]=[0,0,255]
    cv2.imshow("display", img)
def image_word_matrix(filename):
    img = cv2.imread(filename)
    gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
    #cv2.imshow("gray",gray)
    gray = np.float32(gray)
    # 输入图像必须是 float32 ,最后一个参数在 0.04 到 0.05 之间
    dst = cv2.cornerHarris(gray,2,3,0.04)
    #result is dilated for marking the corners, not important
    dst = cv2.dilate(dst,None)
    # Threshold for an optimal value, it may vary depending on the image.
    img[dst>0.01*dst.max()]=[255,255,255]
    gray1 = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
    #cv2.imshow('dst',gray1)
    gray2 = (gray1-gray)
    #cv2.imshow("san",gray2)

    gray2 = np.int32(gray2)


    image_word_matrix = []
    for i in range(40):
        for j in range(150):
            if gray2[i,j]>10:
                image_word_matrix.append([i,j])
    return image_word_matrix
Esempio n. 8
0
    def HarrisCorner(self,img):
        '''
            処理の概要
            args :      -> 
            dst  :      -> 
            param:      -> 
        '''
        
        # img = cv2.resize(img,(648,1170))
        src = img
        gimg = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
        cv2.imshow('srcgray',gimg)
        
        # 前処理 # uint8
        # gimg = cv2.GaussianBlur(gimg, (9,9), 2**1)
        gimg = cv2.medianBlur(gimg,5)
        gimg = cv2.filter2D(gimg, cv2.CV_8U, cls.sharpenKernel)
        print np.array(gimg).dtype,np.array(gimg).shape
        cv2.imshow('filter',gimg)
        # edges = cv2.Canny(gimg,150,200,apertureSize = 3)   
        


        # harris
        dst = cv2.cornerHarris(gimg, blockSize = 2, ksize = 3, k = 0.04)
        dst = cv2.dilate(dst,None)

        img[dst>0.01*dst.max()]=[0,0,255]
        # display results ##################
        # print np.amax(dst)
        cv2.imshow('img',img)    
        cv2.imshow('dst',dst/np.amax(dst))
Esempio n. 9
0
def get_corners_subpixel(filename):
    img = cv2.imread(filename)
    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

    # find Harris corners
    gray = np.float32(gray)
    dst = cv2.cornerHarris(gray, 2, 3, 0.04)
    dst = cv2.dilate(dst, None)
    ret, dst = cv2.threshold(dst, 0.01 * dst.max(), 255, 0)
    dst = np.uint8(dst)

    # find centroids
    ret, labels, stats, centroids = cv2.connectedComponentsWithStats(dst)

    # define the criteria to stop and refine the corners
    criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 100, 0.001)
    corners = cv2.cornerSubPix(
        gray, np.float32(centroids), (5, 5), (-1, -1), criteria)

    # Now draw them
    res = np.hstack((centroids, corners))
    res = np.int0(res)
    img[res[:, 1], res[:, 0]] = [0, 0, 255]
    img[res[:, 3], res[:, 2]] = [0, 255, 0]

    cv2.imwrite(filename.replace('.jpg', '_corner_sub.jpg'), img)
Esempio n. 10
0
def find_corners(image_name):
    image_name_path = path + image_name
    img = cv2.imread(image_name_path)
    shape = list(img.shape)
    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

    gray = np.float32(gray)
    dst = cv2.cornerHarris(gray, 2, 3, 0.03)

    # result is dilated for marking the corners, not important
    dst = cv2.dilate(dst, None)

    print(len(dst))
    print(dst > .2 * dst.max())
    corners = dst > .2 * dst.max()
    points = []

    for i in range(shape[0]):
        for j in range(shape[1]):
            # print(img[i,j])
            if corners[i, j]:
                # print(i, j)
                points.append((j, i))
    print(points)
    return points
Esempio n. 11
0
def get_harris_corners():
    filename = 'emptyboard.png'
    img = cv2.imread(filename)
    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    gray = np.float32(gray)
    dst = cv2.cornerHarris(gray,2,3,0.04)
    dst = cv2.dilate(dst,None)
    ret, dst = cv2.threshold(dst,0.01*dst.max(),255,0)
    dst = np.uint8(dst)
    
    # find centroids
    ret, labels, stats, centroids = cv2.connectedComponentsWithStats(dst)

    # define the criteria to stop and refine the corners
    criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 100, 0.001)
    corners = cv2.cornerSubPix(gray,np.float32(centroids),(5,5),(-1,-1),criteria)

    #print and write to file
    print(corners)
    f = open('color1.txt', 'w')
    for item in corners:
        f.write(str(item[0]) + "\t" + str(item[1]) + "\n")

    # Now draw them
    res = np.hstack((centroids,corners))
    res = np.int0(res)
    #img[res[:,1],res[:,0]]=[0,0,255] #RED
    img[res[:,3],res[:,2]] = [0,255,0] #GREEN

    cv2.imwrite('color1.png',img)
Esempio n. 12
0
def init_harris_corners_and_cluster(monochrome_pil_img, polar_side_maximums, polar_side_minimums, origin):
    harris_img = cv2.cornerHarris(numpy.array(monochrome_pil_img), 3, 3, 0.04)
    harris_corners = []
    for x in range(0, harris_img.shape[0]):
        for y in range(0, harris_img.shape[1]):
            if harris_img[x,y] > CV_HARRIS_CORNER_THRESHOLD:
                harris_corners.append((x,y))
    maxes_and_mins = list(polar_side_maximums)
    for i in range(0, len(polar_side_minimums)):
        maxes_and_mins.append(polar_side_minimums[i])
    for i in range(0, len(maxes_and_mins)):
        radius = maxes_and_mins[i][1]
        angle = maxes_and_mins[i][0]
        dx = int(radius * math.cos(angle))
        dy = int(radius * math.sin(angle))
        pixel = (origin[0] + dx, origin[1] - dy)
        maxes_and_mins[i] = Cluster(pixel)
    clusters = Clusters(harris_corners, maxes_and_mins)
    clusters.fit_data_to_clusters(1, 0)
    #remove_clusters_with_corners_under_threshold
    i = 0
    while i < len(clusters):
        if len(clusters[i]) <= MIN_CORNER_CLUSTER:
            del clusters[i]
        else:
            i += 1
    return len(clusters)
Esempio n. 13
0
	def HarrisCorners(self):
		primo = 1
		while True:
			(grabbed, frame) = self.camera.read()
			gray = cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY)
			gray = np.float32(gray)
			dst = cv2.cornerHarris(gray,2,3,0.02)
			
			dst = cv2.dilate(dst,None)
			
			# Threshold for an optimal value, it may vary depending on the image.
			frame[dst>0.02*dst.max()]=[255]
		   
			# draw a small circle on the original image
			cv2.imshow('dst',frame)
			
			#cv2.circle(image,[10,10],3,(0,255,0),-1)
			#cv2.circle(image,[120,120],3,(0,255,0),-1)
			
			# if the 'q' key is pressed, stop the loop (Note: waitKey are necessary for display camera output)
			if cv2.waitKey(1) & 0xFF == ord("q"):
				break
			elif cv2.waitKey(1) & 0xFF == ord("a"):
				primo = 1
			elif cv2.waitKey(1) & 0xFF == ord("s"):
				primo = 0
Esempio n. 14
0
    def corner_harris(source):
        """
        Question 1.1

        Parameters
        ----------
        source: l'image initiale

        Returns
        -------
        corners: liste de coordonées des points d'intérêt
        """
        # On récupère les distances minimales pour chaque point de l'image
        dst = cv2.cornerHarris(source, 2, 3, 0.04)

        # Seuil empirique pour définir les valeurs que l'on garde
        dst[dst < 0.001 * dst.max()] = 0

        corners = []
        for index, x in np.ndenumerate(dst):
            if x > 0.00001:
                corners.append([[index[1], index[0]]])

        corners = np.array(corners)

        for i in corners:
            x, y = i.ravel()
            cv2.circle(source, (x, y), 3, 255, -1)

        return corners
def corner_frac(img, h=400, w=640, show=False):
    """
    INPUT:  (1) 3D numpy array of image data
    OUTPUT: (1) float: the fraction of the image deemed
                to be a corner as per the cornerHarris algorithm

    Takes a greyscale 400x640 image (default).
    Computes the fraction of each image that is a "corner"
    according to the cornerHarris algorithm in cv2.
    """
    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    gray = np.float32(gray)
    dist = cv2.cornerHarris(gray, 2, 3, 0.04)
    avg_dist = 1.8e8  # good middleground determined over many images
    dist_metric = dist > 0.01 * avg_dist
    corners_google_watermark = len(dist_metric[dist_metric[380:][:] == True])
    true_corners = len(dist_metric[dist_metric[:] == True])
    num_corners = float(true_corners - corners_google_watermark)
    if show:
        img[dist > 0.01 * dist.max()] = [0, 0, 255]
        cv2.imshow("dist", img)
        cv2.waitKey(0)
    cv2.destroyAllWindows()
    corner_frac = num_corners / (h * w)
    return corner_frac
def corner_detect(img):
    # gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    gray = img
    # find Harris corners
    gray = np.float32(gray)
    dst = cv2.cornerHarris(gray, 2, 3, 0.04)
    dst = cv2.dilate(dst, None)
    ret, dst = cv2.threshold(dst, 0.01 * dst.max(), 255, 0)
    dst = np.uint8(dst)

    # find centroids
    ret, labels, stats, centroids = cv2.connectedComponentsWithStats(dst)

    # define the criteria to stop and refine the corners
    criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 100, 0.001)
    corners = cv2.cornerSubPix(gray, np.float32(centroids), (5, 5), (-1, -1), criteria)

    # Now draw them
    PIX = 1
    res = np.hstack((centroids, corners))
    res = np.int0(res)
    for w in range(-PIX, PIX):
        for ww in range(-PIX, PIX):
            try:
                img[res[:, 1] + w, res[:, 0] + ww] = 255  # [0, 0, 255]
                img[res[:, 3] + w, res[:, 2] + ww] = 0  # [0, 255, 0]
            except IndexError:  # FIXME
                pass
Esempio n. 17
0
    def _detect(self,im):
        '''
        void cvCornerHarris( const CvArr* image, CvArr* harris_responce, int block_size, int aperture_size=3, double k=0.04 );
        '''
        import cv2
        
        gray = im.asOpenCV2BW()
        #gray = opencv.cvCreateImage( opencv.cvGetSize(cvim), 8, 1 );
        #corners = cv.CreateImage( cv.GetSize(gray), 32, 1 );
        #opencv.cvCvtColor( cvim, gray, opencv.CV_BGR2GRAY );
    
        corners = cv2.cornerHarris(gray.T,self.block_size,self.aperture_size,self.k)

        #data_buffer = corners.tostring()
        #corners = np.frombuffer(data_buffer,np.float32).reshape(corners.height,corners.width).transpose()        
        
        footprint = np.ones((3,3))
        mx = ndi.maximum_filter(corners, footprint = footprint)
        local_maxima = (corners == mx) * (corners != np.zeros(corners.shape)) # make sure to remove completly dark points

        points = np.nonzero(local_maxima)
        del local_maxima
        
        points = np.array([points[0],points[1]]).transpose()
        L = []
        for each in points:
            L.append((corners[each[0],each[1]],each[0],each[1],None))
        
        return L
Esempio n. 18
0
def get_corner(img):
    gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
    gray = np.float32(gray)
    dst = cv2.cornerHarris(gray,2,3,0.04)
    # dst = cv2.dilate(dst,None)
    # img[dst>0.01*dst.max()]=[0,0,255]
    return dst
def opencv_harris_corner(img):
	floating_point_img = np.float32(img)
	R = cv2.cornerHarris(floating_point_img, 2, 3, 0.06)
	R = cv2.dilate(R, None)

	corners_image = img.copy()
	# Threshold the R values
	THRESHOLD = 1000000
	height, width = img.shape
	WINDOWSIZE = 3
	for i in range(WINDOWSIZE, height - WINDOWSIZE):
		for j in range(WINDOWSIZE, width - WINDOWSIZE):
			r = R.item((i,j))
			if r > THRESHOLD:
				# Check for local maxima
				flag = False
				for x in range(-WINDOWSIZE, WINDOWSIZE+1):
					for y in range(-WINDOWSIZE, WINDOWSIZE+1):
						if x == 0 and y == 0:
							continue
						if r < R.item((i+x, j+y)):
							flag = True
							break
					if flag:
						break
				if not flag:
					corners_image.itemset((i, j), img.item((i, j)))
					cv2.circle(corners_image,(j,i), 10, 255, thickness=1, lineType=8, shift=0)
					# print r
	return corners_image	
Esempio n. 20
0
def detect_harris_squares(img):
  gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
  gray = cv2.GaussianBlur(gray,(3,3),0)
  gray = cv2.adaptiveThreshold(gray, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY_INV, 3, 2) 

  gray = cv2.morphologyEx(gray,cv2.MORPH_DILATE, cv2.getStructuringElement(cv2.MORPH_RECT, (4, 4)),iterations = 1)
  gray = cv2.morphologyEx(gray,cv2.MORPH_ERODE, cv2.getStructuringElement(cv2.MORPH_RECT, (4, 4)),iterations = 1)
  mask, x, y, width, height = get_board_mask(gray)
  roi = gray[y: y+ height, x: x + width]


  #result is dilated for marking the corners, not important
  
  # Threshold for an optimal value, it may vary depending on the image.

  dst = roi.copy()


  
  rst = cv2.cornerHarris(dst, 5, 1, 0.04)
  #dst = cv2.dilate(dst, None)
  width, height = rst.shape

  dst = cv2.cvtColor(dst, cv2.COLOR_GRAY2BGR)
  dst_max = rst.max()*0.5
  for y in xrange(0, height):
    for x in xrange(0, width):
      harris = rst[x][y]
      # check the corner detector response
      if harris > dst_max:
       # draw a small circle on the original image
       cv2.circle(dst, (x,y), 2, (255, 0, 25))
Esempio n. 21
0
	def animpingpong(self):
		obj=self.Object
		img=None
		if not obj.imageFromNode:
			img = cv2.imread(obj.imageFile)
		else:
			print "copy image ..."
			img = obj.imageNode.ViewObject.Proxy.img.copy()
			print "cpied"
		
		print " loaded"
		print (obj.blockSize,obj.ksize,obj.k)
		gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
		gray = np.float32(gray)
		# dst = cv2.cornerHarris(gray,3,3,0.00001)
		dst = cv2.cornerHarris(gray,obj.blockSize,obj.ksize*2+1,obj.k/10000)
		dst = cv2.dilate(dst,None)
		img[dst>0.01*dst.max()]=[0,0,255]
		if True:
			print "zeige"
			cv2.imshow(obj.Label,img)
			print "gezeigt"
		else:
			from matplotlib import pyplot as plt
			plt.subplot(121),plt.imshow(img,cmap = 'gray')
			plt.title('Edge Image'), plt.xticks([]), plt.yticks([])
			plt.subplot(122),plt.imshow(dst,cmap = 'gray')
			plt.title('Corner Image'), plt.xticks([]), plt.yticks([])
			plt.show()
		print "fertig"
		self.img=img
Esempio n. 22
0
def cornerHarris_demo(val):
    thresh = val

    # Detector parameters
    blockSize = 2
    apertureSize = 3
    k = 0.04

    # Detecting corners
    dst = cv.cornerHarris(src_gray, blockSize, apertureSize, k)

    # Normalizing
    dst_norm = np.empty(dst.shape, dtype=np.float32)
    cv.normalize(dst, dst_norm, alpha=0, beta=255, norm_type=cv.NORM_MINMAX)
    dst_norm_scaled = cv.convertScaleAbs(dst_norm)

    # Drawing a circle around corners
    for i in range(dst_norm.shape[0]):
        for j in range(dst_norm.shape[1]):
            if int(dst_norm[i,j]) > thresh:
                cv.circle(dst_norm_scaled, (j,i), 5, (0), 2)

    # Showing the result
    cv.namedWindow(corners_window)
    cv.imshow(corners_window, dst_norm_scaled)
Esempio n. 23
0
def detectCorners():
    try:
        print "[Notice] | Initilizing Corner Detection..."
        cap = cv2.VideoCapture(0)
        print "[Notice] | Starting Corner Detection..."
        print "/n [Notice] | Press CTRL + C to properly exit."
        while(1):
            _, frame = cap.read()
            img = frame
            gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
            gray = np.float32(gray)
            dst = cv2.cornerHarris(gray,2,3,0.04)
            dst = cv2.dilate(dst,None)
            img[dst>0.01*dst.max()]=[0,0,255]
            cv2.imshow('Corner Tracking',img)
            
            k = cv2.waitKey(5) & 0xFF
            if k == 27:
                break
        print "[Notice] | Corner Detection Complete!"
        cv2.destroyAllWindows()
    except KeyboardInterrupt:
        toDo = raw_input("-"*45+"\n\n[Notice] | Script Paused...\n>" + "---"*13 + "<\n> Main Menu-------------(1)" + "\n> Continue Script-------(Any Other Key)\n>" + "---"*13 + "<\n\n> ")
        if "1" in toDo:
            cv2.destroyAllWindows()
            main()
        else:
            pass
    except Exception,e:
        if "something@R" in str(e):
            print "[Error] | Something Happaned."
        else:
            raw_input("[Error] | > " + str(e))
            cv2.destroyAllWindows()
            main()
Esempio n. 24
0
def harrisCorner(pic):
	#convert to grayscale so we can process
	gray_pic = cv2.cvtColor(pic, cv2.COLOR_BGR2GRAY)
	gray_pic = np.float32(gray_pic)

	#do the harris corners algorithm
	corners = cv2.cornerHarris(gray_pic, 2, 3, 0.04)
	return cv2.dilate(corners,None)
Esempio n. 25
0
def harris(img,block=21,aperture=11,param=0.2):
    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    img = VUtil.finlaynorm(img)
    gray,g,r = cv2.split(img)
    corner = cv2.cornerHarris(np.float32(gray),block,aperture,param)
    corner = cv2.dilate(corner,None)
    img[corner>0.01*corner.max()] = (0,0,255)
    return img
Esempio n. 26
0
def cornerHarrisFunc(img):
    '''
    Calculates the energy with Corner Harris.   
    @param img The Image
    @return Numpy array with the energy
    '''
    img_g = __makeGray(img).astype("uint8")
    return cv2.cornerHarris(img_g, 2, 3, 0.04).astype(np.float64)
Esempio n. 27
0
def test_harris (gray) :
  """
  Find corners using harris
  """
  gray32 = np.float32(gray)
  dst = cv2.cornerHarris(gray32,blocksize,ksize,k)
  dst = cv2.dilate(dst,None)
  return dst
Esempio n. 28
0
def showImg ():
    global gray, blockSize_value, ksize_value, k_value, filename
    #print ("blockSize_value %s \n ksize_value = %s \n k_value = %s \n" % (blockSize_value, ksize_value, k_value)) 
    harris = cv2.cornerHarris(gray, blockSize_value, ksize_value, k_value)
    harris = cv2.dilate(harris, None)
    result = cv2.imread(filename)
    result[harris > thresh * harris.max()] = [0, 0, 255]
    cv2.imshow(windowTitle, result)
Esempio n. 29
0
	def detect(self,inp):
		#Harris corner dector works on single channel
		inp = self.__load__(inp);
		dest = cv2.cornerHarris(self.__gray__(inp),self.block_size,self.ap_size,self.k);
		dest=cv2.dilate(dest,None)		
		x,y = np.where(dest>self.qualityLevel*dest.max());
		kp = [cv2.KeyPoint(_y,_x,dest[_x][_y]) for _x,_y in zip(x,y)]
		return kp
Esempio n. 30
0
 def cornerHarris(self):
   gray = cv2.cvtColor(self.imagen,cv2.COLOR_BGR2GRAY)
   gray=np.float32(gray)
   dst=cv2.cornerHarris(gray,2,3,0.04)
   dst = cv2.dilate(dst,None)
   img=self.imagen
   img[dst>0.01*dst.max()]=[0,0,255]
   return Imagen(img,self.name+"cornerHarris")
Esempio n. 31
0
#Importing real chess image
real_chess=cv2.imread('real_chessboard.jpg')
real_chess=cv2.cvtColor(real_chess,cv2.COLOR_BGR2RGB)
plt.imshow(real_chess)

#convert the real chess to gray
gray_real_chess=cv2.cvtColor(real_chess,cv2.COLOR_BGR2GRAY)
plt.imshow(gray_real_chess,cmap='gray')


#Converting in float as it accepts float value not an integer
gray=np.float32(gray_flat_chess)
 
#Applying Harris Corner Detection
dst=cv2.cornerHarris(src=gray,blockSize=2,ksize=3,k=0.04)
dst=cv2.dilate(dst,None)

flat_chess[dst>0.01*dst.max()]=[255,0,0]
plt.imshow(flat_chess)


#Converting in float as it accepts float value not an integer
gray=np.float32(gray_real_chess)

#Applying Harris Corner Detection
dst=cv2.cornerHarris(src=gray,blockSize=2,ksize=3,k=0.04)
dst=cv2.dilate(dst,None)

real_chess[dst>0.01*dst.max()]=[255,0,0]
plt.imshow(real_chess)
Esempio n. 32
0
#The width and height are from gd[0,1]
croppedImg1 = cupImgOneGray[279:362, 226:311]
plt.imshow(croppedImg1, cmap='gray')
plt.show()

#crop imgTwo with gdOne
croppedImg2 = cupImgTwoGray[279:362, 226:311]
plt.imshow(croppedImg2, cmap='gray')
plt.show()

#Convert both images to np floats
cupImgOneFloat = np.float32(croppedImg1)
cupImgTwoFloat = np.float32(croppedImg2)

#Get the Harris Corner
cupImgOneHarrisCorner = cv2.cornerHarris(cupImgOneFloat, 2, 3, 0, 0.04)
cupImgTwoHarrisCorner = cv2.cornerHarris(cupImgTwoFloat, 2, 3, 0, 0.04)
#Display the Corners for Image One
plt.imshow(cupImgOneHarrisCorner, cmap='gray')
plt.show()
#Display the Corners for Image Two
plt.imshow(cupImgTwoHarrisCorner, cmap='gray')
plt.show()

#With the harris corners, do a local max surpress of the image
#Use a 3x3 region to scan for the highest value
#In the region make all other values 0


#Get the height and width of the cub image
def non_maxima(harris, width=3):
import matplotlib

matplotlib.use('TkAgg')
import matplotlib.pyplot as plt
import numpy as np
import matplotlib.image as mplotimg
import cv2

waffle = cv2.imread(
    '/Users/jakub.knitter/OneDrive/PrivateRepo/Sandbox/Python/ComputerVision/images/waffle.jpg'
)
waffle_copy = np.copy(waffle)
waffle_copy = cv2.cvtColor(waffle_copy, cv2.COLOR_BGR2RGB)
waffle_gray = cv2.cvtColor(waffle_copy, cv2.COLOR_RGB2GRAY)

corners1 = cv2.cornerHarris(waffle_gray, 2, 3, 0.04)
# Dilation seems to expand items that will be indicated
corners = cv2.dilate(corners1, None)
plt.imshow(corners)
plt.show()

thresh = 0.05 * corners.max()
waffle_for_the_circles = np.copy(waffle)

for j in range(0, corners.shape[0]):
    for i in range(0, corners.shape[1]):
        if (corners[j, i]) > thresh:
            # image, center of the circle, color, thickness
            cv2.circle(waffle_for_the_circles, (i, j), 1, (0, 255, 0), 1)

plt.imshow(waffle_for_the_circles)
Esempio n. 34
0
#blank_image = np.zeros((num_of_rows, num_of_cols, 1), np.uint8)
#for ii in range(2, num_of_rows-1):
#	for jj in range(2,num_of_cols-1):
		#if image[ii][jj] >= threshold and blank_image[ii][jj] == 0:
			#blank_image[ii][jj] = image[ii][jj]
#		window = np.array([ [image[ii-1][jj-1], image[ii-1][jj], image[ii-1][jj+1]],
#			 [image[ii][jj-1], image[ii][jj], image[ii][jj+1]],
#			 [image[ii+1][jj-1], image[ii+1][jj], image[ii+1][jj+1]] ])
#		image[ii,jj] = np.median(window)

image_seven = cv.Canny(image_one, 50, 150)
image_eight = cv.Canny(image_eight, 100, 200)
image_nine = cv.Canny(image, 100, 200)
image_ten = cv.Canny(image_three, 100,200)
image_eleven = cv.Canny(image_four, 100,200)
# cv.imwrite('images/temp01.jpg', image)
cv.imwrite('images/tempXYZ.jpg', image_one)
cv.imwrite('images/tempCrCb.jpg', image_two)
cv.imwrite('images/tempHSV.jpg', image_three)
cv.imwrite('images/tempHLS.jpg', image_four)
cv.imwrite('images/tempLab.jpg', image_five)
cv.imwrite('images/tempLuv.jpg', image_six)
cv.imwrite('images/tempCannyXYZ.jpg', image_seven)
cv.imwrite('images/tempCannygray.jpg', image_eight)
cv.imwrite('images/XYZgray.jpg', cv.cvtColor(cv.cvtColor(image, cv.COLOR_BGR2XYZ), cv.COLOR_BGR2GRAY))
cv.imwrite('images/gray.jpg', cv.cvtColor(image, cv.COLOR_BGR2GRAY))
cv.imwrite('images/tempCannyHLS.jpg', image_eleven)

image_twelve = cv.cornerHarris(cv.cvtColor(image, cv.COLOR_BGR2GRAY), 2, 5, 0.04)
cv.imwrite('images/tempHarris.jpg', image_twelve)
Esempio n. 35
0
import cv2 as cv
import numpy as np
img = cv.imread('BW.jpg')
cv.imshow('image', img)
gray = cv.cvtColor(img, cv.COLOR_BGR2GRAY)
gray = np.float32(gray)
dat = cv.cornerHarris(gray, 2, 3, 0.04)
dst = cv.dilate(dat, None)
img[dst > 0.01 * dst.max()] = [0, 0, 255]
cv.imshow('Result', img)
if cv.waitKey(0) & 0xff == 27:
    cv.destroyWindow()
Esempio n. 36
0
            result = np.vstack((result, stack_images_line(line, scale)))
    return result


if __name__ == '__main__':
    root = tk.Tk()
    screen_width = root.winfo_screenwidth()
    screen_height = root.winfo_screenheight()

    img = cv2.imread("resources/lena.png")
    # img = cv2.imread("resources/lena.png", cv2.IMREAD_GRAYSCALE)
    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    print(img.shape)
    blurred = cv2.GaussianBlur(gray, (11, 11), 0)
    canny = cv2.Canny(gray, 90, 90)
    harris = cv2.cornerHarris(gray, 2, 11, 0.15)
    kernel = np.ones((5, 5), np.uint8)
    dilated = cv2.dilate(canny, kernel, iterations=1)
    eroded = cv2.erode(dilated, kernel, iterations=1)
    # img_stack= np.vstack((np.hstack((img, canny, harris)), np.hstack((gray, dilated, eroded))))
    img_stack = stack_images(((canny, harris), (dilated, eroded)), 0.5)
    cv2.imshow("Lena Images", img_stack)
    # cv2.imshow("Lena [edge detection]", canny)
    # cv2.imshow("Lena [corner detection]", harris)
    # cv2.imshow("Lena [image dilation]", dilated)
    # cv2.imshow("Lena [image erosion]", eroded)

    # cv2.resizeWindow("Lena", width, height)
    # cv2.moveWindow("Lena", (screen_width - width) // 2, (screen_height - height) // 2 )
    cv2.waitKey(60000)
    cv2.destroyAllWindows()
Esempio n. 37
0
#!/usr/bin/env python2
# -*- coding: utf-8 -*-
"""
Created on Wed Jun  7 20:28:05 2017

@author: Mohnish_Devadiga
"""

import cv2
import numpy as np

# Load image then grayscale
image = cv2.imread('images/chess.jpg')
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

# The cornerHarris function requires the array datatype to be float32
gray = np.float32(gray)

harris_corners = cv2.cornerHarris(gray, 3, 3, 0.05)

#We use dilation of the corner points to enlarge them\
kernel = np.ones((7,7),np.uint8)
harris_corners = cv2.dilate(harris_corners, kernel, iterations = 2)

# Threshold for an optimal value, it may vary depending on the image.
image[harris_corners > 0.025 * harris_corners.max() ] = [255, 127, 127]

cv2.imshow('Harris Corners', image)
cv2.waitKey(0)
cv2.destroyAllWindows()
# -*- coding: utf-8 -*-

import cv2
import numpy as np
from matplotlib import pyplot as plt

filename = '../assets2/chessboard2.jpg'
img = cv2.imread(filename)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

gray = np.float32(gray)
dst = cv2.cornerHarris(gray, 2, 3, 0.04)

#result is dilated for marking the corners, not important
dst = cv2.dilate(dst, None)

# Threshold for an optimal value, it may vary depending on the image.
img[dst > 0.01 * dst.max()] = [0, 0, 255]

cv2.imshow('dst', img)
if cv2.waitKey(0) & 0xff == 27:
    cv2.destroyAllWindows()
Esempio n. 39
0
    # image_rgb_cv = image_rgb_mine
    # image_gray = cv2.imread("lab.jpg",0)
    image_rgb_mine = cv2.imread("test.png", 1)
    image_rgb_cv = image_rgb_mine
    image_gray = cv2.imread("test.png", 0)
    height = image_gray.shape[0]
    width = image_gray.shape[1]

    # show original one
    cv2.imshow('Original One', image_rgb_mine)

    print("Processing... !!!")

    # use the cv function to compare
    image_gray = np.float32(image_gray)
    corner_harris_cv = cv2.cornerHarris(image_gray, 2, 3, 0.04)
    # corner_harris_cv = cv2.dilate(corner_harris_cv, None, iterations=3)

    # we use sobel operators to get the x-align gradient and y-align gradient
    # using covolution function we use in HW1
    I_x = gradient_x(image_gray, height, width)
    I_y = gradient_y(image_gray, height, width)

    # as the definition, that is the formula
    # we let Ixx, Ixy and Iyy as below
    Ixx = I_x**2
    Ixy = I_y * I_x
    Iyy = I_y**2

    # set some variables,
    # like window size that compute between Ixx and Iyy and Ixy
    def white_out_borders(scaledimg):
        ## PART 1 ##
        ## Declare empty lists ##
        axis0limit = []
        axis0limitindexnp = []
        axis0limitindexflat = []
        axis1limit = []
        axis1limitindexnp = []
        axis1limitindexflat = []
        toplistlimit = []
        bottomlistlimit = []
        rightlistlimit = []
        leftlistlimit = []

        scaledimg_borders = scaledimg.copy()
        ## Get the red channel as we have a priori knowledge that the border will be reddish-brown wood ##
        scaledimg_borders_redchannel = scaledimg_borders[:, :, 2]
        rows, cols = scaledimg_borders_redchannel.shape
        ## Convert the image to an array ##
        image_data = np.asarray(scaledimg_borders_redchannel)
        ## Save each pixel value ##
        rpxvalue = []
        for i in range(rows):
            for j in range(cols):
                rpxvalue.append((image_data[i, j]))

        rimage_aslist = list(rpxvalue[i:i + cols]
                             for i in range(0, len(rpxvalue), cols))
        rimage_asarray = np.asarray(rimage_aslist)

        ## Get vertical standard deviation ##
        rgbsta0 = np.std(rimage_asarray, axis=0)
        ## Get horizontal standard deviation ##
        rgbsta1 = np.std(rimage_asarray, axis=1)

        ## Set the threshold of 20 ##
        sdthreshold = 20

        ## Get the pixel index where the value is below the threshold value - this gives us the row and column at which the border ends ##
        for i in range(len(rgbsta0)):
            if rgbsta0[i] < sdthreshold:
                axis0limit.append(rgbsta0[i])
                axis0limitindexnp.append(np.where(rgbsta0 == rgbsta0[i]))

        for i in range(len(axis0limitindexnp)):
            axis0limitindexflat.append(axis0limitindexnp[i][0][0])

        for i in range(len(rgbsta1)):
            if rgbsta1[i] < sdthreshold:
                axis1limit.append(rgbsta1[i])
                axis1limitindexnp.append(np.where(rgbsta1 == rgbsta1[i]))

        for i in range(len(axis1limitindexnp)):
            axis1limitindexflat.append(axis1limitindexnp[i][0][0])

        ## Disqualify any points in the center of the image as we are only interested in the points at the extreme ends ##
        splitlisthresholdrows = rows / 4
        splitlisthresholdcols = cols / 4

        ## Get the top, bottom, left and right coordinates where the border ends ##
        for i in axis0limitindexflat:
            if (i < splitlisthresholdrows) & (i <= 0.05 * rows):
                leftlistlimit.append(i)
            elif (i > splitlisthresholdrows) & (i >= 0.95 * rows):
                rightlistlimit.append(i)

        for i in axis1limitindexflat:
            if (i < splitlisthresholdcols) & (i <= 0.05 * cols):
                toplistlimit.append(i)
            elif (i > splitlisthresholdcols) & (i >= 0.95 * cols):
                bottomlistlimit.append(i)

        try:
            toplimit = max(toplistlimit)
        except ValueError:
            toplimit = int(0.02 * rows)

        try:
            bottomlimit = min(bottomlistlimit)
        except ValueError:
            bottomlimit = int(0.98 * rows)

        try:
            leftlimit = max(leftlistlimit)
        except ValueError:
            leftlimit = int(0.02 * cols)

        try:
            rightlimit = min(rightlistlimit)
        except ValueError:
            rightlimit = int(0.98 * cols)

        ### PART 2 ###
        ## Declare empty lists ##
        cornerstoplist = []
        cornerstoplistx = []
        cornerstoplisty = []
        cornersbottomlist = []
        cornersbottomlistx = []
        cornersbottomlisty = []
        cornersleftlist = []
        cornersleftlistx = []
        cornersleftlisty = []
        cornersrightlist = []
        cornersrightlistx = []
        cornersrightlisty = []

        scaledimg_borders_2 = scaledimg.copy()
        mediansmoothedimg_borders = cv2.medianBlur(scaledimg_borders_2, 3)
        grayimg_borders = cv2.cvtColor(mediansmoothedimg_borders,
                                       cv2.COLOR_BGR2GRAY)

        ## Apply the Sobel filter to get the border edges ##
        v_edges = cv2.Sobel(grayimg_borders,
                            cv2.CV_16S,
                            1,
                            0,
                            ksize=3,
                            scale=1,
                            delta=0,
                            borderType=cv2.BORDER_DEFAULT)
        h_edges = cv2.Sobel(grayimg_borders,
                            cv2.CV_16S,
                            0,
                            1,
                            ksize=3,
                            scale=1,
                            delta=0,
                            borderType=cv2.BORDER_DEFAULT)

        abs_grad_x = cv2.convertScaleAbs(v_edges)
        abs_grad_y = cv2.convertScaleAbs(h_edges)

        mask = np.zeros(grayimg_borders.shape, dtype=np.uint8)
        linewidth = (
            (grayimg_borders.shape[0] + grayimg_borders.shape[1])) / 50

        ## Detect the vertical edges ##
        magv = np.abs(v_edges)
        magv2 = (255 * magv / np.max(magv)).astype(np.uint8)
        _, mag2 = cv2.threshold(magv2, 15, 255, cv2.THRESH_BINARY)
        contours, hierarchy = cv2.findContours(mag2.copy(), cv2.RETR_EXTERNAL,
                                               cv2.CHAIN_APPROX_SIMPLE)

        for contour in contours:
            _, _, w, h = cv2.boundingRect(contour)
            if h > grayimg_borders.shape[0] / 4 and w < linewidth:
                cv2.drawContours(mask, [contour], -1, 255, -1)

        ## Detect the horizontal edges ##
        magh = np.abs(h_edges)
        magh2 = (255 * magh / np.max(magh)).astype(np.uint8)
        _, mag2 = cv2.threshold(magh2, 15, 255, cv2.THRESH_BINARY)
        contours, hierarchy = cv2.findContours(mag2.copy(), cv2.RETR_EXTERNAL,
                                               cv2.CHAIN_APPROX_SIMPLE)

        for contour in contours:
            _, _, w, h = cv2.boundingRect(contour)
            if w > grayimg_borders.shape[1] / 4 and h < linewidth:
                cv2.drawContours(mask, [contour], -1, 255, -1)

        ## Perform dilation to strengthten the lines ##
        kerneldilate = np.ones((5, 5), np.uint8)
        dilation = cv2.dilate(mask, kerneldilate, 10)

        #dstr = cv2.resize(dilation, (800,800))
        #cv2.imshow('dst',dstr)

        ## It is difficult to extract the precise line coordinates from the Sobel filter, so we get it by pixel level instead ##
        ## Use Corner Harris detection to get the pixel-level coordinates of the border ##
        dst = cv2.cornerHarris(dilation, 2, 3, 0.001)
        ret, dst = cv2.threshold(dst, 0.01 * dst.max(), 255, 0)
        dst = np.uint8(dst)

        ret, labels, stats, centroids = cv2.connectedComponentsWithStats(dst)
        criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 100,
                    0.001)
        corners = cv2.cornerSubPix(dst, np.float32(centroids), (5, 5),
                                   (-1, -1), criteria)

        ## Set the limits to prevent overfitting and cutting out important data ##
        upperrowlimit = int(rows * (5 / 100))
        lowerrowlimit = int(rows * (95 / 100))
        leftcollimit = int(cols * (5 / 100))
        rightcollimit = int(cols * (95 / 100))
        heightpadding = int(rows) - 100
        widthpadding = int(cols) - 100

        ## Get all the pixels that fall within the upper and lower limit and get the furthest point ##
        for i in range(len(corners)):
            if ((corners[i][1] <= upperrowlimit) & (corners[i][1] >= 10)):
                cornerstoplist.append((int(corners[i][0]), int(corners[i][1])))
                cornerstoplistx.append((int(corners[i][0])))
                cornerstoplisty.append((int(corners[i][1])))
        try:
            maxcornerstoplist = int(max(cornerstoplisty))
        except statistics.StatisticsError:
            maxcornerstoplist = 0.02 * rows
        except ValueError:
            maxcornerstoplist = 0.02 * rows

        ## Get all the pixels that fall within the upper and lower limit and get the lowest point ##
        for i in range(len(corners)):
            if ((corners[i][1] >= lowerrowlimit) &
                (corners[i][1] <= heightpadding)):
                cornersbottomlist.append(
                    (int(corners[i][0]), int(corners[i][1])))
                cornersbottomlistx.append((int(corners[i][0])))
                cornersbottomlisty.append((int(corners[i][1])))
        try:
            mincornersbottomlist = int(min(cornersbottomlisty))
        except statistics.StatisticsError:
            mincornersbottomlist = 0.98 * rows
        except ValueError:
            mincornersbottomlist = 0.98 * rows

        ## Get all the pixels that fall within the upper and lower limit and get the average ##
        for i in range(len(corners)):
            if ((corners[i][0] <= leftcollimit) & (corners[i][0] >= 10)):
                cornersleftlist.append(
                    (int(corners[i][0]), int(corners[i][1])))
                cornersleftlistx.append((int(corners[i][0])))
                cornersleftlisty.append((int(corners[i][1])))
        try:
            meancornersleftlist = int(statistics.mean(cornersleftlistx))
        except statistics.StatisticsError:
            meancornersleftlist = 0.02 * cols
        except ValueError:
            meancornersleftlist = 0.02 * cols

        ## Get all the pixels that fall within the upper and lower limit and get the average ##
        for i in range(len(corners)):
            if ((corners[i][0] >= rightcollimit) &
                (corners[i][0] <= widthpadding)):
                cornersrightlist.append(
                    (int(corners[i][0]), int(corners[i][1])))
                cornersrightlistx.append((int(corners[i][0])))
                cornersrightlisty.append((int(corners[i][1])))
        try:
            meancornersrightlist = int(statistics.mean(cornersrightlistx))
        except statistics.StatisticsError:
            meancornersrightlist = 0.98 * cols
        except ValueError:
            meancornersrightlist = 0.98 * cols

        ## Get the coordinates that has the least error, or that ensures that it does not intersect with the tray area ##
        topborder = int(max(toplimit, maxcornerstoplist))
        bottomborder = int(min(bottomlimit, mincornersbottomlist))
        leftborder = int(max(leftlimit, meancornersleftlist))
        rightborder = int(min(rightlimit, meancornersrightlist))

        ## Remove the border by setting all the pixels to white and that no bounding boxes will be generated at that location ##
        whitenedbordersimg = scaledimg.copy()
        topblank = 255 * np.ones(shape=[topborder, cols, 3], dtype=np.uint8)
        bottomblank = 255 * np.ones(shape=[rows - bottomborder, cols, 3],
                                    dtype=np.uint8)
        leftblank = 255 * np.ones(shape=[rows, leftborder, 3], dtype=np.uint8)
        rightblank = 255 * np.ones(shape=[rows, cols - rightborder, 3],
                                   dtype=np.uint8)

        whitenedbordersimg[0:topborder, 0:cols] = topblank
        whitenedbordersimg[bottomborder:rows] = bottomblank
        whitenedbordersimg[0:cols, 0:leftborder] = leftblank
        whitenedbordersimg[0:rows, rightborder:cols] = rightblank

        whitenedbordersimgsub = cv2.cvtColor(whitenedbordersimg,
                                             cv2.COLOR_BGR2GRAY)
        whitenedbordersimgsub = cv2.subtract(whitenedbordersimgsub, dilation)
        whitenedbordersimgsub = cv2.cvtColor(whitenedbordersimgsub,
                                             cv2.COLOR_GRAY2BGR)

        #whitenedbordersimgsubr = cv2.resize(whitenedbordersimgsub, (800,800))
        #cv2.imshow('dst',whitenedbordersimgsubr)

        return whitenedbordersimg, whitenedbordersimgsub
##   Codes inspired by:
##   Github.com/imvinod/
##   Official Documentation
##==============================================================================

import cv2
import numpy as np
#PRE PROCESSING
#image = cv2.imread('avengers.jpg')
#image = cv2.resize(image, None, fx=0.5, fy=0.5, interpolation=cv2.INTER_AREA)
#image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
#image = cv2.Canny(image, 110, 255)
#image = cv2.medianBlur(image,5)
#ret,image = cv2.threshold(image,100,255,cv2.THRESH_BINARY)

#HARRIS CORNER DETECTION
image = cv2.imread('imgs\demo1.jpg')
#image = cv2.resize(image, None, fx=0.5, fy=0.5, interpolation=cv2.INTER_AREA)
image_h = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
image_h = np.float32(image_h)
blockSize = 2
apertureSize = 1
harris = cv2.cornerHarris(image_h, blockSize, apertureSize, 0.05)
kernel = np.ones((3, 3), np.uint8)
harris = cv2.dilate(harris, kernel, iterations=4)
image[harris > 0.05 * harris.max()] = [255, 127, 255]
cv2.imshow('Harris Corners', image)

cv2.waitKey()
cv2.destroyAllWindows()
Esempio n. 42
0
import cv2
import numpy as np

resim = cv2.imread('ucgen.png')
griResim = cv2.cvtColor(resim, cv2.COLOR_BGR2GRAY)
_, thresh = cv2.threshold(griResim, 150, 255, cv2.THRESH_BINARY)
konturler, _ = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE)

for i in konturler:
    resim = cv2.imread('ucgen.png')
    griResim = np.float32(griResim)
    mask = np.zeros(
        griResim.shape, dtype="uint8"
    )  #veri tipi 8 bitlik unsigned int olan griResimin shape'i kadar 0 oluşturup dizi yaratır
    cv2.fillPoly(mask, [i], (255, 255, 255))
    dst = cv2.cornerHarris(mask, 5, 3, 0.04)
    ret, dst = cv2.threshold(dst, 0.1 * dst.max(), 255, 0)
    dst = np.uint8(dst)
    ret, etiketler, istatistikler, geo_merkez = cv2.connectedComponentsWithStats(
        dst)
    kriter = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 100, 0.001)
    koseler = cv2.cornerSubPix(griResim, np.float32(geo_merkez), (5, 5),
                               (-1, -1), kriter)

if len(koseler) == 4:
    print('Resimdeki şekil üçgendir. \nÜçgenin Köşeleri: ')

    for i in range(1, len(koseler)):
        print(koseler[i])  #her bir köşenin x,y koordinatlarını yazdırıyoruz

        cv2.circle(resim, tuple(np.round(koseler[i]).astype("int")), 3,
Esempio n. 43
0
def get_watershed_image(image, mask):
    """
    Creates and returns segmented image.
    In returned image left segment has value -5 and right segment -10.
    Those two segments represents source and sink in graph.
    :param image: inverted difference of two images
    :param mask: mask of overlapping region
    :return: Segmented image
    """
    height, width = mask.shape
    original_image = cv2.cvtColor(image, cv2.COLOR_GRAY2BGR)
    original_mask = cv2.cvtColor(mask, cv2.COLOR_GRAY2BGR)
    contours = cv2.findContours(mask, cv2.RETR_LIST, cv2.CHAIN_APPROX_NONE)[1]
    epsilon = 0.01 * cv2.arcLength(contours[0], True)
    approx = cv2.approxPolyDP(contours[0], epsilon, True)
    box_img = cv2.cornerHarris(mask, 5, 3, 0.04)

    flat = box_img.flatten()
    indices = np.argpartition(flat, -4)[-4:]
    indices = indices[np.argsort(-flat[indices])]
    corners = np.unravel_index(indices, box_img.shape)
    corners = corners[1]
    ext_left = (np.sort(corners))[0]
    ext_right = (np.sort(corners))[-1]
    ext_left2 = (np.sort(corners))[1]
    ext_right2 = (np.sort(corners))[-2]

    gray = cv2.threshold(image, 0, 255,
                         cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)[1]
    gray = np.where(mask == 0, mask, gray)
    # kernel = np.ones((3, 3), np.uint8)
    # gray = cv2.morphologyEx(gray, cv2.MORPH_OPEN, kernel, iterations=1)
    # gray = cv2.dilate(gray, kernel, iterations=2)

    gray[mask != 255] = 255
    """
    cv2.imshow("diff", gray)
    cv2.waitKey()
    """
    half = int(ext_left + ((ext_right - ext_left) / 2))
    for i in range(height):
        if gray[i, half] == 255:
            gray[i, half] = 0
        else:
            break

    for i in range(height - 1, -1, -1):
        if gray[i, half] == 255:
            gray[i, half] = 0
        else:
            break
    """
    cv2.imshow("diff", gray)
    cv2.waitKey()
    """

    ret, markers = cv2.connectedComponents(gray)

    watershed_image = cv2.watershed(original_image, markers)
    original_mask[watershed_image == -1] = [0, 0, 255]
    """
    cv2.imshow("diff", original_mask)
    cv2.waitKey()
    """

    original_mask[watershed_image == 1] = [255, 0, 0]
    original_mask[watershed_image == 2] = [0, 255, 0]
    original_mask[watershed_image == -6] = [255, 0, 255]
    original_mask[watershed_image == -1] = [0, 0, 255]
    """
    cv2.imshow("diff", original_mask)
    cv2.waitKey()
    """

    return watershed_image
    def detect_rectangles(self, image, circles):
        def add_center_points_from_circles(center_points, circles):
            for key, elem in circles.items():
                # x and y are interchanged
                cy = int(elem['y'] + elem['width'] / 2)
                cx = int(elem['x'] + elem['height'] / 2)
                new_center_point = [cx, cy]
                center_points.append(new_center_point)

        gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

        gray = np.float32(gray)
        dst = cv2.cornerHarris(gray, 2, 5, 0.04)

        # cv2.imshow("Gray", gray)

        # result is dilated for marking the corners, not important
        dst = cv2.dilate(dst, None)

        corner_indexes = np.where(dst > 0.01 * dst.max())
        result = np.array([[corner_indexes[0][0], corner_indexes[1][0]]])
        for ith_corner in range(len(corner_indexes[0])):
            p1 = np.array(
                [corner_indexes[0][ith_corner], corner_indexes[1][ith_corner]])
            should_add = True
            if gray[p1[0], p1[1]] == 255:
                should_add = False
            else:
                for p2 in result:
                    if self.calc_dist(p2, p1) < 30:
                        should_add = False

            if should_add:
                result = np.vstack((result, p1))

        diamond_corners = []
        for r in result:
            diamond_corners.append(r)
        diamond_corners = np.array(diamond_corners)

        center_points = []
        add_center_points_from_circles(center_points, circles)
        rectangles = {}
        while (np.size(result) != 0):
            upper_left_corner_idx = np.argmin(np.sum(result, axis=1))
            upper_left_corner_point = result[upper_left_corner_idx]

            # the corner point is not directly on the edge
            if gray[upper_left_corner_point[0] + 1,
                    upper_left_corner_point[1]] != 0:
                upper_left_corner_point = [
                    upper_left_corner_point[0], upper_left_corner_point[1] + 1
                ]

            idx_to_remove = [upper_left_corner_idx]

            can_continue = True
            next_point = upper_left_corner_point
            max_right = 10
            while can_continue:
                left_down = gray[next_point[0] + 1, next_point[1] - 1]
                down = gray[next_point[0] + 1, next_point[1]]
                right_down = gray[next_point[0] + 1, next_point[1] + 1]
                right = gray[next_point[0], next_point[1] + 1]
                if down == 0:
                    next_point = [next_point[0] + 1, next_point[1]]
                elif right_down == 0:
                    next_point = [next_point[0] + 1, next_point[1] + 1]
                elif left_down == 0:
                    next_point = [next_point[0] + 1, next_point[1] - 1]
                elif right == 0 and max_right > 0:
                    max_right -= 1
                    next_point = [next_point[0], next_point[1] + 1]
                else:
                    can_continue = False

            # TODO: calc dist
            can_continue = True
            lower_left_corner = next_point
            max_up = 10
            while can_continue:
                right_up = gray[next_point[0] - 1, next_point[1] + 1]
                right = gray[next_point[0], next_point[1] + 1]
                right_down = gray[next_point[0] + 1, next_point[1] + 1]
                up = gray[next_point[0] - 1, next_point[1]]
                if right == 0:
                    next_point = [next_point[0], next_point[1] + 1]
                elif right_up == 0:
                    next_point = [next_point[0] - 1, next_point[1] + 1]
                elif right_down == 0:
                    next_point = [next_point[0] + 1, next_point[1] + 1]
                elif up == 0 and max_up > 0:
                    max_up -= 1
                    next_point = [next_point[0] - 1, next_point[1]]
                else:
                    can_continue = False
            # TODO: calc dist
            can_continue = True
            lower_right_corner = next_point
            max_left = 10
            while can_continue:
                left_up = gray[next_point[0] - 1, next_point[1] - 1]
                up = gray[next_point[0] - 1, next_point[1]]
                right_up = gray[next_point[0] - 1, next_point[1] + 1]
                left = gray[next_point[0], next_point[1] - 1]
                if up == 0:
                    next_point = [next_point[0] - 1, next_point[1]]
                elif left_up == 0:
                    next_point = [next_point[0] - 1, next_point[1] - 1]
                elif right_up == 0:
                    next_point = [next_point[0] - 1, next_point[1] + 1]
                elif left == 0 and max_left > 0:
                    max_left -= 1
                    next_point = [next_point[0], next_point[1] - 1]
                else:
                    can_continue = False

            can_continue = True
            upper_right_corner = next_point
            while can_continue:
                left_up = gray[next_point[0] - 1, next_point[1] - 1]
                left = gray[next_point[0], next_point[1] - 1]
                left_down = gray[next_point[0] + 1, next_point[1] - 1]
                if left == 0:
                    next_point = [next_point[0], next_point[1] - 1]
                elif left_down == 0:
                    next_point = [next_point[0] + 1, next_point[1] - 1]
                elif left_up == 0:
                    next_point = [next_point[0] - 1, next_point[1] - 1]
                else:
                    can_continue = False

            dist = self.calc_dist(upper_left_corner_point, next_point)
            dist_left = self.calc_dist(upper_left_corner_point,
                                       lower_left_corner)
            dist_bottom = self.calc_dist(lower_right_corner, lower_left_corner)
            dist_right = self.calc_dist(lower_right_corner, upper_right_corner)
            if dist < 40 and dist_left > 40 and dist_bottom > 40 and dist_right > 40:
                x1 = upper_left_corner_point[0]
                x2 = upper_right_corner[0]
                y1 = upper_left_corner_point[1]
                y2 = lower_left_corner[1]
                x = int((x1 + x2) / 2)
                y = int((y1 + y2) / 2)
                w = int(
                    (upper_right_corner[1] + lower_right_corner)[1] / 2) - y
                h = int((lower_left_corner[0] + lower_right_corner[0]) / 2) - x
                cv2.rectangle(image, (y, x), (y + w, x + h), (0, 255, 0), 3)

                cx = int(y + w / 2)
                cy = int(x + h / 2)
                new_center_point = [cx, cy]
                should_add = True
                for center_point in center_points:
                    dist = self.calc_dist(new_center_point, center_point)
                    if dist < 40:
                        should_add = False

                if should_add:
                    center_points.append([cx, cy])
                    id = 'task_' + IdGenerator.next()
                    x = x
                    y = y
                    rectangles[id] = {
                        'type': 'bpmn:Task',
                        'x': y,
                        'y': x,
                        'width': w,
                        'height': h
                    }

            to_choose = [
                x for x in range(result.shape[0]) if x not in idx_to_remove
            ]
            result = result[to_choose, :]

        while (np.size(diamond_corners) != 0):
            top_idx = np.argmin(diamond_corners, axis=0)[0]
            top_corner = diamond_corners[top_idx]

            # the corner point is not directly on the edge
            if gray[top_corner[0] + 1, top_corner[1]] != 0:
                top_corner = [top_corner[0] + 3, top_corner[1]]

            idx_to_remove = [top_idx]

            can_continue = True
            next_point = top_corner
            max_left = 20
            while can_continue:
                left_down = gray[next_point[0] + 1, next_point[1] - 1]
                down = gray[next_point[0] + 1, next_point[1]]
                left = gray[next_point[0], next_point[1] - 1]
                if left_down == 0:
                    next_point = [next_point[0] + 1, next_point[1] - 1]
                elif down == 0:
                    next_point = [next_point[0] + 1, next_point[1]]
                elif left == 0 and max_left > 0:
                    max_left -= 1
                    next_point = [next_point[0], next_point[1] - 1]
                else:
                    can_continue = False

            # TODO: calc dist
            can_continue = True
            left_corner = next_point
            max_down = 20
            max_up = 10
            while can_continue:
                down = gray[next_point[0] + 1, next_point[1]]
                right = gray[next_point[0], next_point[1] + 1]
                right_down = gray[next_point[0] + 1, next_point[1] + 1]
                right_up = gray[next_point[0] - 1, next_point[1] + 1]
                if right_down == 0:
                    next_point = [next_point[0] + 1, next_point[1] + 1]
                elif right == 0:
                    next_point = [next_point[0], next_point[1] + 1]
                elif down == 0 and max_down > 0:
                    max_down -= 1
                    next_point = [next_point[0] + 1, next_point[1]]
                elif right_up == 0 and max_up > 0:
                    max_up -= 1
                    next_point = [next_point[0] - 1, next_point[1] + 1]
                else:
                    can_continue = False
            # TODO: calc dist
            can_continue = True
            bottom_corner = next_point
            max_right = 20
            while can_continue:
                right = gray[next_point[0], next_point[1] + 1]
                up = gray[next_point[0] - 1, next_point[1]]
                right_up = gray[next_point[0] - 1, next_point[1] + 1]
                if right_up == 0:
                    next_point = [next_point[0] - 1, next_point[1] + 1]
                elif up == 0:
                    next_point = [next_point[0] - 1, next_point[1]]
                elif right == 0 and max_right > 0:
                    max_right -= 1
                    next_point = [next_point[0], next_point[1] + 1]
                else:
                    can_continue = False

            can_continue = True
            right_corner = next_point
            max_up = 20
            max_down = 10
            while can_continue:
                left_up = gray[next_point[0] - 1, next_point[1] - 1]
                left = gray[next_point[0], next_point[1] - 1]
                up = gray[next_point[0] - 1, next_point[1]]
                left_down = gray[next_point[0] + 1, next_point[1] - 1]
                if left_up == 0:
                    next_point = [next_point[0] - 1, next_point[1] - 1]
                elif left == 0:
                    next_point = [next_point[0], next_point[1] - 1]
                elif up == 0 and max_up > 0:
                    max_up -= 1
                    next_point = [next_point[0] - 1, next_point[1]]
                elif left_down == 0 and max_down > 0:
                    max_down -= 1
                    next_point = [next_point[0] + 1, next_point[1] - 1]
                else:
                    can_continue = False

            dist = self.calc_dist(top_corner, next_point)
            dist_left = self.calc_dist(top_corner, left_corner)
            dist_bottom = self.calc_dist(bottom_corner, left_corner)
            dist_right = self.calc_dist(bottom_corner, right_corner)
            if dist < 50 and dist_left > 40 and dist_bottom > 40 and dist_right > 40:
                x = top_corner[0]
                y = left_corner[1]
                w = right_corner[1] - y
                h = bottom_corner[0] - x
                # cv2.rectangle(image, (y , x), (y+w, x+h ), (0, 255, 0), 3)

                cx = int(y + w / 2)
                cy = int(x + h / 2)
                new_center_point = [cx, cy]
                should_add = True
                for center_point in center_points:
                    dist = self.calc_dist(new_center_point, center_point)
                    if dist < 40:
                        should_add = False

                if should_add:
                    center_points.append([cx, cy])
                    id = 'gateway_' + IdGenerator.next()
                    rectangles[id] = {
                        'type': 'bpmn:ExclusiveGateway',
                        'x': int(y),
                        'y': int(x),
                        'width': int(w),
                        'height': int(h)
                    }

            to_choose = [
                x for x in range(diamond_corners.shape[0])
                if x not in idx_to_remove
            ]
            diamond_corners = diamond_corners[to_choose, :]

        return rectangles
Esempio n. 45
0
    X2 = x_train.copy()
    for c in range(CHANNELS):
        X2[plot_idx, :, :, c] = mms.fit_transform(X2[plot_idx, :, :, c])
    print(np.shape(X2))
    plt.subplot(121)
    plt.imshow(X2[plot_idx, :, :, :])
    plt.subplot(122)
    plt.imshow(x_train_canny[plot_idx, :, :, :])
    plt.show()

if USE_CORNERHARRIS:
    mms = MinMaxScaler()
    for i in range(n_images):
        for c in range(CHANNELS):
            img = x_train[i, :, :, c].astype(np.float32)
            dst = cornerHarris(img, 2, 3, 0.04)
            dst = dilate(dst,None)
            img[dst > 0.01*dst.max()] = 1.0
            x_train_cornerharris[i, :, :, c] = img
    X2 = x_train.copy()
    for c in range(CHANNELS):
        X2[plot_idx, :, :, c] = mms.fit_transform(X2[plot_idx, :, :, c])
    print(np.shape(X2))
    plt.subplot(121)
    plt.imshow(X2[plot_idx, :, :, :])
    plt.subplot(122)
    plt.imshow(x_train_cornerharris[plot_idx, :, :, c])
    plt.show()

if TRAIN:
    # Define model:
Esempio n. 46
0
def main():

    # 画像を読み込み、グレースケール変換
    # コーナー検出ではグレースケールの画像で特徴を取り出す
    chess_board = cv2.imread(IMG_PATH)
    gray_chess_board = cv2.cvtColor(chess_board, cv2.COLOR_BGR2GRAY)
    corner_harris = chess_board.copy()
    corner_shi = chess_board.copy()
    corner_fast = gray_chess_board.copy()

    # --- Harrisコーナー検出 ---
    # いろいろな方向に画素の位置を元の位置(x,y)から微小に(u,v)移動してみて画素値がどのように違うかを求める
    # 単精度浮動小数点型にキャスト
    gray = np.float32(gray_chess_board)
    # blocksize:コーナー検出の際に考慮する隣接する領域のサイズ
    # ksize:ソーベルフィルタの勾配パラメーターのカーネルサイズ
    # k:コーナーを判定する閾値
    dst = cv2.cornerHarris(src=gray, blockSize=2, ksize=3, k=0.04)
    # 膨張処理を施して特徴づけ
    # マーキングのために行っているだけで、特にコーナー検出と直接関係ない
    dst = cv2.dilate(dst, None)
    # 元画像に、先ほど処理したdstが0.01*dst.max()より大きい値の部分を青色で表示するように値を代入
    corner_harris[dst > 0.01 * dst.max()] = [255, 0, 0]

    # --- J.ShiとC.Tomasiのコーナー検出 ---
    # Harrisを改良(簡素化)し、より良い結果を出すようにした
    # goodFeaturesToTrack()でコーナを検出
    # 第2引数:検出したいコーナー数を指定。−1にすると全てのコーナーを検出する。
    # 第3引数:検出するコーナーの最低限の質を0から1の間の値で指定。
    # 第4引数:検出される2つのコーナー間の最低限のユークリッド距離。
    corners = cv2.goodFeaturesToTrack(gray_chess_board, 100, 0.01, 10)
    # np.int0で整数にキャスト
    corners = np.int0(corners)
    # for-in文を使って、cornersからコーナーデータを一つずつ取り出す
    # これをravel()で1次元化して(x, y)の座標に代入
    # この(x, y)を用いて、circle()を使ってコーナー部分に丸印をつける。半径3、太さを−1にして塗りつぶしの指定。
    for i in corners:
        (x, y) = i.ravel()
        cv2.circle(corner_shi, (x, y), 3, color=(0, 255, 0), thickness=-1)

    # --- FASTのコーナー検出 ---
    # 注目画素pの周辺の円周上の決まった16画素を観測し、輝度がしきい値以上のピクセルが連続してn個以上になる点を特徴点とする
    fast = cv2.FastFeatureDetector_create()
    #fast.setThreshold(150)
    #fast.setNonmaxSuppression(False)
    kp = fast.detect(corner_fast, None)
    corner_fast2 = cv2.drawKeypoints(corner_fast, kp, None)
    print('print FAST params:')
    print("\tThreshold: ", fast.getThreshold())
    print("\tnonmaxSuppression: ", fast.getNonmaxSuppression())
    print("\tneighborhood: ", fast.getType())
    print("\tTotal Keypoints with nonmaxSuppression: ", len(kp))

    # 結果を出力
    cv2.imshow('chess_board', chess_board)
    cv2.imshow('Harris', corner_harris)
    cv2.imshow('Shi-Tomasi', corner_shi)
    cv2.imshow('FAST', corner_fast2)

    cv2.waitKey(0)
    cv2.destroyAllWindows()
Esempio n. 47
0
def main():

    # Setup LEDBlobDetector parameters
    params = cv.SimpleBlobDetector_Params()
    # Change thresholds
    # Image will be thresholded beforehand, meaning only one thresholding step is necessary
    params.minThreshold = 0
    params.maxThreshold = 256

    # Filter by Area
    params.filterByArea = True
    params.minArea = 7
    params.maxArea = 40

    # Filter by Circularity
    params.filterByCircularity = False
    # Filter by Convexity
    params.filterByConvexity = True
    params.minConvexity = .5
    # Filter by Inertia
    params.filterByInertia = True
    params.minInertiaRatio = .1

    ver = (cv.__version__).split('.')
    if int(ver[0]) < 3:
        LEDBlobDetector = cv.SimpleBlobDetector(params)
    else:
        LEDBlobDetector = cv.SimpleBlobDetector_create(params)

    # Filter by Area
    params.filterByArea = True
    params.minArea = 300
    params.maxArea = 1600
    # Filter by Circularity
    params.filterByCircularity = True
    params.minCircularity = .70
    # Filter by Convexity
    params.filterByConvexity = True
    params.minConvexity = .4
    # Filter by Inertia
    params.filterByInertia = True
    params.minInertiaRatio = .05

    ver = (cv.__version__).split('.')
    if int(ver[0]) < 3:
        ObstacleBlobDetector = cv.SimpleBlobDetector(params)
    else:
        ObstacleBlobDetector = cv.SimpleBlobDetector_create(params)

    # Filter by Area
    params.filterByArea = True
    params.minArea = 20
    params.maxArea = 350

    # Filter by Circularity
    params.filterByCircularity = False
    params.minCircularity = .1
    params.maxCircularity = .70
    # Filter by Convexity
    params.filterByConvexity = False
    params.minConvexity = .2
    # Filter by Inertia
    params.filterByInertia = False
    params.minInertiaRatio = .2

    ver = (cv.__version__).split('.')
    if int(ver[0]) < 3:
        BlockBlobDetector = cv.SimpleBlobDetector(params)
    else:
        BlockBlobDetector = cv.SimpleBlobDetector_create(params)

    params.minArea = 700
    params.maxArea = 4200

    # Filter by Circularity
    params.filterByCircularity = False
    params.minCircularity = .3
    params.maxCircularity = .70

    ver = (cv.__version__).split('.')
    if int(ver[0]) < 3:
        BlockSurrBlobDetector = cv.SimpleBlobDetector(params)
    else:
        BlockSurrBlobDetector = cv.SimpleBlobDetector_create(params)

    LowerLED = np.array([0, 0, 245])
    UpperLED = np.array([180, 51, 255])

    LowerMothershipLED = np.array([80, 102, 178])
    UpperMothershipLED = np.array([90, 204, 255])

    LowerBlockObstacle = np.array([0, 0, 165])
    UpperBlockObstacle = np.array([180, 51, 255])

    LowerBlockSurr = np.array([10, 90, 140])
    UpperBlockSurr = np.array([20, 165, 192])

    frameNum = 1
    path = '/home/pi/.virtualenvs/tower/lib/python3.5/site-packages/tower/data/'
    while True:
        image = cv.imread(path + ('frame%d.png' % frameNum))

        if (image is None) or (image.size == 0):
            break

        hsvImage = cv.cvtColor(image, cv.COLOR_BGR2HSV)

        LEDmask = cv.inRange(hsvImage, LowerLED, UpperLED)
        inv_LEDmask = cv.bitwise_not(LEDmask)

        MothershipMask = cv.inRange(hsvImage, LowerMothershipLED,
                                    UpperMothershipLED)
        inv_MothershipMask = cv.bitwise_not(MothershipMask)

        BlockObstacleMask = cv.inRange(hsvImage, LowerBlockObstacle,
                                       UpperBlockObstacle)
        inv_BlockObstacleMask = cv.bitwise_not(BlockObstacleMask)

        BlockSurrMask = cv.inRange(hsvImage, LowerBlockSurr, UpperBlockSurr)

        ret = cv.imwrite(path + ("frame%dLEDmask.png" % frameNum), inv_LEDmask)
        ret = cv.imwrite(path + ("frame%dMothershipLEDMask.png" % frameNum),
                         inv_MothershipMask)
        ret = cv.imwrite(path + ("frame%dBlockObstacleMask.png" % frameNum),
                         inv_BlockObstacleMask)
        ret = cv.imwrite(path + ("frame%dBlockSurrMask.png" % frameNum),
                         BlockSurrMask)

        gray = np.float32(BlockSurrMask)
        dst = cv.cornerHarris(gray, 5, 3, 0.04)

        #result is dilated for marking the corners, not important
        dst = cv.dilate(dst, None)

        # Threshold for an optimal value, it may vary depending on the image.
        hsvImage[dst > 0.2 * dst.max()] = [0, 0, 255]
        ret = cv.imwrite(path + ("frame%dCorners.png" % frameNum), hsvImage)

        keypoints = LEDBlobDetector.detect(inv_LEDmask)
        im_with_keypoints = image.copy()
        print("LED Keypoints: %d" % len(keypoints))
        for marker in keypoints:
            im_with_keypoints = cv.drawMarker(im_with_keypoints,
                                              tuple(int(i) for i in marker.pt),
                                              color=(0, 255, 0))
        ret = cv.imwrite(path + ("frame%dLEDKeypoints.png" % frameNum),
                         im_with_keypoints)

        keypoints = LEDBlobDetector.detect(inv_MothershipMask)
        im_with_keypoints = image.copy()
        print("Mothership LED Keypoints: %d" % len(keypoints))
        for marker in keypoints:
            im_with_keypoints = cv.drawMarker(im_with_keypoints,
                                              tuple(int(i) for i in marker.pt),
                                              color=(0, 255, 0))
        ret = cv.imwrite(
            path + ("frame%dMothershipLEDKeypoints.png" % frameNum),
            im_with_keypoints)

        keypoints = BlockBlobDetector.detect(BlockObstacleMask)
        im_with_keypoints = image.copy()
        print("Block Keypoints: %d" % len(keypoints))
        for marker in keypoints:
            im_with_keypoints = cv.drawMarker(im_with_keypoints,
                                              tuple(int(i) for i in marker.pt),
                                              color=(0, 255, 0))
        ret = cv.imwrite(path + ("frame%dBlockKeypoints.png" % frameNum),
                         im_with_keypoints)

        keypoints = BlockSurrBlobDetector.detect(BlockSurrMask)
        im_with_keypoints = image.copy()
        print("Block Surr Keypoints: %d" % len(keypoints))
        for marker in keypoints:
            im_with_keypoints = cv.drawMarker(im_with_keypoints,
                                              tuple(int(i) for i in marker.pt),
                                              color=(0, 255, 0))
        ret = cv.imwrite(path + ("frame%dBlockSurrKeypoints.png" % frameNum),
                         im_with_keypoints)

        keypoints = ObstacleBlobDetector.detect(inv_BlockObstacleMask)
        im_with_keypoints = image.copy()
        print("Obstacle Keypoints: %d" % len(keypoints))
        for marker in keypoints:
            im_with_keypoints = cv.drawMarker(im_with_keypoints,
                                              tuple(int(i) for i in marker.pt),
                                              color=(0, 255, 0))
        ret = cv.imwrite(path + ("frame%dObstacleKeypoints.png" % frameNum),
                         im_with_keypoints)

        print("Frame %d finished" % frameNum)
        frameNum += 1
    #END OF while
    print("Script Complete")
Esempio n. 48
0
# -*- coding: utf-8 -*-
"""
Created on Sun Dec 24 13:10:49 2017

@author: Michael
"""

import cv2
import numpy as np
import tsvimage

img = tsvimage.load(1)

gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

gray = np.float32(gray)
dst = cv2.cornerHarris(gray, 20, 3, 0.01)

#result is dilated for marking the corners, not important
dst = cv2.dilate(dst, None)

# Threshold for an optimal value, it may vary depending on the image.
img[dst > 0.01 * dst.max()] = [0, 0, 255]

cv2.imshow('dst', img)
if cv2.waitKey(0) & 0xff == 27:
    cv2.destroyAllWindows()
import numpy as np
import cv2 as cv
import matplotlib.pyplot as plt

# read
img = cv.imread("blox.jpg")
img = cv.cvtColor(img, cv.COLOR_BGR2RGB)
gray = cv.cvtColor(img, cv.COLOR_RGB2GRAY)
gray_float = np.float32(gray)

# harris to get potential points
dst = cv.cornerHarris(gray_float, 2, 3, 0.04)
dst = cv.dilate(dst, None)

# create mask with threshold, assign potential points to 255
ret, mask = cv.threshold(dst, 0.01*dst.max(), 255, cv.THRESH_BINARY)
mask = np.uint8(mask)

# result of harris
harris = img.copy()
harris[mask == 255] = [255, 0, 0]

# find centroids of mask
ret, labels, stats, centroids = cv.connectedComponentsWithStats(mask)

# assign the [:, 1] y axis means row number, [:, 0] x axis means column number
centroid = img.copy()
fancy = np.int0(centroids)
centroid[fancy[:, 1], fancy[:, 0]] = [0, 255, 0]

# define the criteria to stop and refine the corners
Esempio n. 50
0
import cv2
import numpy as np

filename = 'small.png'
img = cv2.imread(filename)

img2 = np.float32(img)
dst = cv2.cornerHarris(img2, 2, 3, 0.04)

#result is dilated for marking the corners, not important
dst = cv2.dilate(dst, None)

# Threshold for an optimal value, it may vary depending on the image.
img[dst > 0.01 * dst.max()] = [0, 0, 255]

cv2.imshow('dst', img)
if cv2.waitKey(0) & 0xff == 27:
    cv2.destroyAllWindows()
Esempio n. 51
0
import cv2
import matplotlib.pyplot as plt

img_bgr = cv2.imread('01.png')
img_rgb = cv2.cvtColor(img_bgr, cv2.COLOR_BGR2RGB)
plt.figure(figsize=(18, 6))
plt.subplot(131)
plt.imshow(img_bgr)
plt.subplot(132)
plt.imshow(img_rgb)
img_gray = cv2.cvtColor(img_bgr, cv2.COLOR_BGR2GRAY)
plt.subplot(133)

corners = cv2.cornerHarris(img_gray, 2, 3, 0.04)
plt.imshow(corners, cmap='gray')
plt.show()
Esempio n. 52
0
while 1:

    k = cv2.waitKey(1) & 0xff
    if k == 27:
        break

    T1 = cv2.getTrackbarPos('Neighbor_k', 'image')
    T2 = cv2.getTrackbarPos('Sobel_k', 'image')
    T3 = cv2.getTrackbarPos('Equation_k', 'image')
    T4 = cv2.getTrackbarPos('Threshold_k', 'image')

    if T2 % 2 == 0:
        T2 += 1

    dst = cv2.cornerHarris(gray, T1, T2, T3/100)
    dst = cv2.dilate(dst, None)
    img[dst > T4/100 * dst.max()] = [0, 0, 255]

    cv2.imshow('dst', img)

cv2.destroyAllWindows()


# Shi-Tomasi
img = cv2.imread('Data/Dog1.jpg')
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
corners = cv2.goodFeaturesToTrack(gray, 25, 0.01, 10)
corners = np.int0(corners)

for i in corners:
			for sobel in range(1, 11, 2): # x5

				#for every Harris corner threshold [0.5, 1.0] at step 
					# size 0.05
				for cornerThresh in range(64, 100, 2): # x18
					#Harris is run here

					#changing the threshold to something usable
					tempCornerThresh = cornerThresh / 100.0

					#calculating the corner-ness of each pixel
					harCorners = cv2.dilate(
						cv2.cornerHarris(
							k=tempContraThresh,
							ksize=sobel,

							#blockSize is kept constant
							blockSize=3,
							src=numpy.float32(scaled)
							),
						None
						)

					#thresholding the Harris measure
					harCompar = tempCornerThresh * harCorners.max()
					harCorners = harCorners[harCorners > harCompar]

					#thresholding by number of corners
					if (minCorners < len(harCorners) < maxCorners):
						harTempSum += len(harCorners)
						harCounter += 1
					#if
Esempio n. 54
0
import cv2
import numpy as np
# from matplotlib import pyplot as plt
import kMeans

img = cv2.imread('Ferrari2.png', 0)
img = np.float32(img)
corners = cv2.cornerHarris(img, 2, 3, 0.04)
# plt.subplot(2,1,1), plt.imshow(corners ,cmap = 'jet')
# plt.title('Harris Corner Detection'), plt.xticks([]), plt.yticks([])

img2 = cv2.imread('Ferrari2.png')
corners2 = cv2.dilate(corners, None, iterations=3)
crit = 0.01 * corners2.max()
img2[corners2 > crit] = [255, 0, 0]
# print 'corners'
# print corners
# print 'corners2'
# print corners2
# plt.subplot(2,1,2),plt.imshow(img2,cmap = 'gray')
# plt.title('Canny Edge Detection'), plt.xticks([]), plt.yticks([])
#
# plt.show()

target = []
for y in range(0, corners2.shape[1]):
    for x in range(0, corners2.shape[0]):
        if corners2[x, y] > crit:
            target = target + [[x, y]]
target = np.array(target)
print target.shape
Esempio n. 55
0
import cv2
import numpy as np

I = cv2.imread('square.jpg')
G = cv2.cvtColor(I, cv2.COLOR_BGR2GRAY)

G = np.float32(G)
window_size = 2
soble_kernel_size = 3  # kernel size for gradients
alpha = 0.1
H = cv2.cornerHarris(G, window_size, soble_kernel_size, alpha)
#cv2.imshow('dastan',H)
# normalize C so that the maximum value is 1
H = H / H.max()

# C[i,j] == 255 if H[i,j] > 0.01, and C[i,j] == 0 otherwise
C = np.uint8(H > 0.005) * 255

## connected components
nc, CC = cv2.connectedComponents(C)
# to count the number of corners we count the number
# of nonzero elements of C (wrong way to count corners!)
n = nc - 1

# Show corners as red pixels in the original image
for x in range(1, nc, 1):
    I[CC == x] = [0, 0, 255]

cv2.imshow('corners', C)
cv2.waitKey(0)  # press any key
Esempio n. 56
0
def find_rooms(img,
               noise_removal_threshold=1,
               corners_threshold=0.001,
               room_closing_max_length=10,
               gap_in_wall_threshold=500000):
    """

    :param img: grey scale image of rooms, already eroded and doors removed etc.
    :param noise_removal_threshold: Minimal area of blobs to be kept.
    :param corners_threshold: Threshold to allow corners. Higher removes more of the house.
    :param room_closing_max_length: Maximum line length to add to close off open doors.
    :param gap_in_wall_threshold: Minimum number of pixels to identify component as room instead of hole in the wall.
    :return: rooms: list of numpy arrays containing boolean masks for each detected room
             colored_house: A colored version of the input image, where each room has a random color.
    """
    assert 0 <= corners_threshold <= 1
    # Remove noise left from door removal

    img[img < 128] = 0
    img[img > 128] = 255
    _, contours, _ = cv2.findContours(~img, cv2.RETR_EXTERNAL,
                                      cv2.CHAIN_APPROX_SIMPLE)
    mask = np.zeros_like(img)
    for contour in contours:
        area = cv2.contourArea(contour)
        if area > noise_removal_threshold:
            cv2.fillPoly(mask, [contour], 255)

    img = ~mask

    # Detect corners (you can play with the parameters here)
    dst = cv2.cornerHarris(img, 2, 3, 0.04)
    dst = cv2.dilate(dst, None)
    corners = dst > corners_threshold * dst.max()

    # Draw lines to close the rooms off by adding a line between corners on the same x or y coordinate
    # This gets some false positives.
    # You could try to disallow drawing through other existing lines for example.
    for y, row in enumerate(corners):
        x_same_y = np.argwhere(row)
        for x1, x2 in zip(x_same_y[:-1], x_same_y[1:]):

            if x2[0] - x1[0] < room_closing_max_length:
                color = 0
                cv2.line(img, (x1, y), (x2, y), color, 1)

    for x, col in enumerate(corners.T):
        y_same_x = np.argwhere(col)
        for y1, y2 in zip(y_same_x[:-1], y_same_x[1:]):
            if y2[0] - y1[0] < room_closing_max_length:
                color = 0
                cv2.line(img, (x, y1), (x, y2), color, 1)

    # Mark the outside of the house as black
    _, contours, _ = cv2.findContours(~img, cv2.RETR_EXTERNAL,
                                      cv2.CHAIN_APPROX_SIMPLE)
    contour_sizes = [(cv2.contourArea(contour), contour)
                     for contour in contours]
    biggest_contour = max(contour_sizes, key=lambda x: x[0])[1]
    mask = np.zeros_like(mask)
    cv2.fillPoly(mask, [biggest_contour], 255)
    img[mask == 0] = 0

    # Find the connected components in the house
    ret, labels = cv2.connectedComponents(img)
    img = cv2.cvtColor(img, cv2.COLOR_GRAY2RGB)
    unique = np.unique(labels)
    rooms = []
    for label in unique:
        component = labels == label
        if img[component].sum(
        ) == 0 or np.count_nonzero(component) < gap_in_wall_threshold:
            color = 0
        else:
            rooms.append(component)
            color = np.random.randint(0, 255, size=3)
        img[component] = color

    return rooms, img
Esempio n. 57
0
import cv2
import numpy as np

img_org = cv2.imread('demo02.jpg')
img = cv2.resize(img_org, (600, 800))
img_com = cv2.resize(img_org, (300, 400))

gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
gray = np.float32(gray)

dst = cv2.cornerHarris(gray, 2, 23, 0.04)
print("600*800", np.sum(dst > 0.01 * dst.max()))

gray_com = cv2.cvtColor(img_com, cv2.COLOR_BGR2GRAY)
gray_com = np.float32(gray_com)
dst_com = cv2.cornerHarris(gray_com, 2, 23, 0.04)
print("300*400", np.sum(dst_com > 0.01 * dst_com.max()))

print(dst.shape)
print(dst > 0.01 * dst.max())
img[dst > 0.01 * dst.max()] = [0, 255, 0]
img_com[dst_com > 0.01 * dst_com.max()] = [0, 0, 255]
while True:
    cv2.imshow('corner', img)
    cv2.imshow('corner_com', img_com)
    if cv2.waitKey(2000) & 0xff == ord("q"):
        break
cv2.destroyAllWindows()
Esempio n. 58
0
"""


import matplotlib.image as mpimg
import matplotlib.pyplot as plt
import numpy as np
import cv2 

#Lee la imagen
img_Color = mpimg.imread("img/image.jpg")
#plt.imshow(img_Color, cmap='gray')

#Transforma la imagen a escala de grises
img_Gris = cv2.cvtColor(img_Color,  cv2.COLOR_BGR2GRAY)
#plt.imshow(img_Gris, cmap='gray')

#obtiene las esquinas de la imagen
corners = cv2.cornerHarris(img_Gris, 5,3, 0.1)
corners_dilate = cv2.dilate(corners, np.ones((8,8), np.uint8), iterations =1)
#plt.imshow(corners_dilate, cmap='gray')

#Copia la imagen de color ya que no la variable img_Color es solo para lectura
img_copy = img_Color.copy()
#Colorea de verde las esquinas detectadas 0,255,0, B,G,R para open cv
img_copy[corners_dilate > 0.01 * corners_dilate.max()] = [0,255,0];
plt.imshow(img_copy, cmap='gray')



Esempio n. 59
0

cap = cv2.VideoCapture(0)
cv2.namedWindow("K")
cv2.resizeWindow("K", 600, 600)
cv2.createTrackbar("Corner", "K", 1, 100, empty)
cv2.createTrackbar("Dist", "K", 1, 100, empty)

lower_yellow = np.array([8, 89, 142])
upper_yellow = np.array([87, 255, 255])

while True:
    _, frame = cap.read()
    corner = cv2.getTrackbarPos("Corner", "K")
    dist = cv2.getTrackbarPos("Dist", "K")
    corner = corner / 100
    dist = dist / 100
    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
    inrange = cv2.inRange(gray, lower_yellow, upper_yellow)
    gray = np.float32(inrange)

    dst = cv2.cornerHarris(gray, 5, 3, corner)

    frame[dst > dist * dst.max()] = [0, 0, 255]
    stack = Func.stackImages(0.6, ([frame], [gray]))
    cv2.imshow("dst", stack)
    key = cv2.waitKey(1)
    if key == 27:
        print("break")
        exit()
Esempio n. 60
0
import numpy as np
import cv2



while(True):
    image = cv2.imread("box.jpg")
    gray = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)
    blur = cv2.bilateralFilter(gray,9,75,75)
    edges = cv2.Canny(blur,100,200)
    dilate = cv2.dilate(edges,None)
    dilate = cv2.bilateralFilter(dilate,9,75,75)
    # gray1 = np.float32(edges)
    dst = cv2.cornerHarris(dilate,2,3,0.04)
    dst = cv2.dilate(dst,None)
    image[dst>0.01*dst.max()]=[0,0,255]


    cv2.imshow("image",image)





    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

cv2.destroyAllWindows()