def erode(img): if not img is None: # if no image is passed, no process is taken place and an error is printed to the command line h, w = np.shape( img ) # allocates the height and width of the image to the variables "h" and "w" respectively new_image = np.zeros( (h, w), np.uint8 ) # defines an image with coordinates 0 with the same size as the original # Iterate through the image pixel by pixel with {py, px} indicating the coordinates for the Kernels origin for py in range(0, h): for px in range(0, w): img[px, py] = 255 - img[px, py] # creates a complement image by subtracting the grayscale value from the max value new_image = dilate.dilate( img) # dilates the complement image and allocates it to new_image for py in range(0, h): for px in range(0, w): new_image[px, py] = 255 - new_image[ px, py] # creates the complements of the dilatied image return new_image else: print("No image file successfully loaded." ) # printed to tell the user that the image was not loaded
def threesitematrix(gamma, eta, t, verbose=False, tol=1e-12): '''Generate the matrix corresponding to a 3-site PT symmetric Hamiltonian with parameters gamma and eta, evolved to time t. The form of the (non-Hermitian) Hamiltonian is: / i*gamma -eta 0 \ H = | -eta 0 -eta | \ 0 -eta -i*gamma / gamma : (imaginary) on-site potential eta : coupling between adjacent sites t : evolution time verbose : (optional) print verbose output to stdout return : (numpy array) time-evolution operator (not unitary)''' gamma=float(gamma) eta=float(eta) t=float(t) H=numpy.array([[1j*gamma, -eta, 0], [-eta, 0, -eta], [0, -eta, -1j*gamma]],\ dtype=complex) if abs(2*eta**2-gamma**2)<tol: # critical case raise Exception('Critical case', 'Singular matrix') elif 2*eta**2>gamma**2: # symmetric case if verbose: print "Symmetric case" evals=numpy.array([0,numpy.sqrt(2*eta**2-gamma**2),\ -numpy.sqrt(2*eta**2-gamma**2)], dtype=complex) vzero=1/numpy.sqrt(gamma**2+2*eta**2)*numpy.array([eta,gamma*1j,-eta],\ dtype=complex) vplus=1/(2*numpy.sqrt(2)*eta)*numpy.array([-1j*gamma-numpy.sqrt(2*eta**2-\ gamma**2), 2*eta, 1j*eta-numpy.sqrt(2*eta**2-gamma**2)],dtype=complex) vminus=1/(2*numpy.sqrt(2)*eta)*numpy.array([-1j*gamma+numpy.sqrt(2*eta**2-\ gamma**2), 2*eta, 1j*gamma+numpy.sqrt(2*eta**2-gamma**2)],dtype=complex) elif 2*eta**2 < gamma**2: # broken case if verbose: print "Broken case" evals=numpy.array([0,1j*numpy.sqrt(gamma**2-2*eta**2),\ -1j*numpy.sqrt(gamma*2-2*eta**2)], dtype=complex) vzero=1/numpy.sqrt(gamma**2+2*eta**2)*numpy.array([eta,gamma*1j,-eta],\ dtype=complex) vplus=1/(2*gamma)*numpy.array([1j*(-gamma - numpy.sqrt(gamma**2-2*eta**2)),\ 2*eta, 1j*(gamma - numpy.sqrt(gamma**2 - 2*eta**2))], dtype=complex) vminus=1/(2*gamma)*numpy.array([1j*(-gamma + numpy.sqrt(gamma**2 -\ 2*eta**2)), 2*eta, 1j*(gamma + numpy.sqrt(gamma**2 - 2*eta**2))], dtype=complex) # Now calculate the exponential V=numpy.array([[vzero[0],vplus[0],vminus[0]],\ [vzero[1],vplus[1],vminus[1]],\ [vzero[2],vplus[2],vminus[2]]],dtype=complex) D=numpy.array([[numpy.exp(-1j*evals[0]*t),0,0], [0,numpy.exp(-1j*evals[1]*t),0], [0,0,numpy.exp(-1j*evals[2]*t)]],dtype=complex) G=numpy.dot(V, numpy.dot(D, numpy.linalg.inv(V))) evals,evecs = linalg.eig(numpy.dot(G,numpy.conjugate(numpy.transpose(G)))) normG = numpy.sqrt(max(abs(evals))) G /= normG U = dilate(numpy.matrix(G)) return U, normG
def close(img): if not img is None: # if no image is passed, no process is taken place and an error is printed to the command line new_image = dilate.dilate( img ) # runs dilation on the input image and allocates it to new_image final_image = erode.erode( new_image ) # runs erosion on the new_image and allocates it to itself return final_image # returns the new_image else: print("No image file successfully loaded.")
def ptmatrix(gamma, J, t, verbose=False): '''Generate the matrix corresponding to a PT symmetric Hamiltonian with parameters gamma and J, evolved to a time t. The form of the (non-Hermitian) Hamiltonian is: H = -J \sigma_x - i \gamma -sigma_z gamma : on-site potential J : cross-site tunneling t : evolution time verbose : print verbose output to stdout return : (numpy array) time-evolution operator (not necessarily unitary''' G = numpy.empty((2,2),dtype=complex) if gamma<J: if verbose: print "PT-symmetric\n" eps = numpy.sqrt(J**2-gamma**2) alpha = eps*t G[0,0] = numpy.cos(alpha)-gamma*numpy.sin(alpha)/eps G[0,1] = complex(0,J*numpy.sin(alpha)/eps) G[1,0] = complex(0,J*numpy.sin(alpha)/eps) G[1,1] = numpy.cos(alpha)+gamma*numpy.sin(alpha)/eps elif gamma>J: if verbose: print "non-PT-symmetric\n" eps = numpy.sqrt(gamma**2-J**2) beta = eps*t G[0,0] = numpy.cosh(beta)-gamma*numpy.sinh(beta)/eps G[0,1] = complex(0,J*numpy.sinh(beta)/eps) G[1,0] = complex(0,J*numpy.sinh(beta)/eps) G[1,1] = numpy.cosh(beta)+gamma*numpy.sinh(beta)/eps else: if verbose: print "PT-phase boundary\n" G[0,0] = 1-J*t G[0,1] = complex(0,J*t) G[1,0] = complex(0,J*t) G[1,1] = 1+J*t evals,evecs = linalg.eig(numpy.dot(G,numpy.conjugate(numpy.transpose(G)))) normG = numpy.sqrt(max(abs(evals))) G /= normG U = dilate(numpy.matrix(G)) return U, normG
def main(): # Accessing the file filenameIn = sys.argv[1] filenameOut = sys.argv[2] readImage = cv2.imread(filenameIn, 0) # My erosion implementation img_dilated = dilation.dilate(readImage) img_closed = erosion.erode(img_dilated) # A perfect erosion, performed by OpenCV's native method # img_closed2 = cv2.morphologyEx(readImage, cv2.MORPH_CLOSE, numpy.ones((5,5), numpy.uint8)) # Normally save image, but for dev we just show it cv2.imwrite(filenameOut, img_closed) # cv2.imwrite('lena_closing_perfect.png', img_closed2) # cv2.imshow('mine', img_closed) cv2.waitKey(0) cv2.destroyAllWindows()
def closing(): cv2.imwrite('./lena_closing.png', erode(dilate(image)))
# Structuring array definition # Structuring array must satisfy these requirements: ## Same Length and Width ## Dimensions must be odd ## Elements must be either 255 or 0 se5 = np.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]]) se3 = np.array([[255, 255, 255], [255, 255, 255], [255, 255, 255]]) # Dilation test run # Iterate over all images and apply operator dilated = {} for key in bmp: dilated[key] = dilate(bmp[key], se5) #Save. Weird jankyness that needs a conversion to work properly Image.fromarray(dilated[key]).convert('L').save(key[:-4] + '_dl5.bmp') # Repeat for erode opened and closed, using different operators eroded = {} for key in bmp: eroded[key] = erode(bmp[key], se3) Image.fromarray(eroded[key]).convert('L').save(key[:-4] + '_er3.bmp') opened = {} for key in bmp: opened[key] = opening(bmp[key], se3) Image.fromarray(opened[key]).convert('L').save(key[:-4] + '_op3.bmp') closed = {}
def opening(): cv2.imwrite('./lena_opening.png', dilate(erode(image)))
def closing(bitmap, strucEl): working = dilate(bitmap, strucEl) working = erode(working, strucEl) return working