Beispiel #1
0
def process(imgInput):
	width = len(imgInput[0])
	height = len(imgInput)

	# Threshold
	print("[OMR Image Preprocessing] Thresholding...")
	imgBinary = omr_threshold_image.threshold(imgInput)

	# Find largest connected component
	print("[OMR Image Preprocessing] Finding largest connected component... (may take a few minutes)")
	imgLCC = largestConnectedComponent(imgBinary)

	# Fill page with white pixels
	print("[OMR Image Preprocessing] Filling page with white pixels...")
	imgPageFilled = fillPage(imgLCC)

	# Edge detection
	print("[OMR Image Preprocessing] Performing edge detection...")
	imgEdges = cv2.Canny(imgPageFilled,50,150)

	# Dilate edges
	print("[OMR Image Preprocessing] Dilating edges...")
	dilationKernel = np.ones((8,8),np.uint8)
	imgEdgesDilated = cv2.dilate(imgEdges,dilationKernel,iterations=1)

	# Hough line transform
	print("[OMR Image Preprocessing] Performing Hough line transform...")
	houghLines = cv2.HoughLines(imgEdgesDilated, 1, np.pi/180, min(width/2,height/2))

	# Sort lines by rho value

	sortedHoughLines = sorted(houghLines[0],key=lambda x : x[0])
	"""
	for i in range(0,len(sortedHoughLines)):
		if (sortedHoughLines[i][0] < 0):
			sortedHoughLines[i][0] *= (-1)
			sortedHoughLines[i][1] = (sortedHoughLines[i][1] + np.pi)%(2*np.pi)
	"""
	distanceThreshold = 1

	while (len(sortedHoughLines) > 4):
		nextHoughLines = []
		length = len(sortedHoughLines)
		skip = False
		for i in range(0,length):
			rho1 = sortedHoughLines[i][0]
			rho2 = sortedHoughLines[(i+1)%length][0]
			theta1 = sortedHoughLines[i][1]
			theta2 = sortedHoughLines[(i+1)%length][1]
		
			distance = math.sqrt(rho1*rho1 + rho2*rho2 - 2*rho1*rho2*np.cos(theta2 - theta1))
			if (not(skip)):
				if (distance < distanceThreshold):
					#nextHoughLines.append(sortedHoughLines[i])
					nextHoughLines.append(np.array([(rho1 + rho2)/2,(theta1 + theta2)/2]))
					skip = True
				else:
					nextHoughLines.append(sortedHoughLines[i])
			else:
				skip = False

		distanceThreshold += 1
		sortedHoughLines = copy.deepcopy(nextHoughLines)
		print('New iteration:')
		for rho,theta in sortedHoughLines:
			print('rho: ' + str(rho) + ' theta: ' + str(theta))

	# Highlight lines on original image
	for rho,theta in sortedHoughLines:
		print('rho: ' + str(rho) + ' theta: ' + str(theta))
		cosTheta = np.cos(theta)
		sinTheta = np.sin(theta)
		x0 = cosTheta*rho
		y0 = sinTheta*rho
		length = max(width,height)
		x1 = int(x0 + length * (-sinTheta))
		y1 = int(y0 + length * (cosTheta))
		x2 = int(x0 - length * (-sinTheta))
		y2 = int(y0 - length * (cosTheta))

		cv2.line(imgInput,(x1,y1),(x2,y2),(0,0,255),2)

	# Perform perspective transform
	intersectionPoints = []

	for i in range(0,len(sortedHoughLines)-1):
		for j in range(i+1,len(sortedHoughLines)):
			intersectionPoints.append(intersectPolarLines(sortedHoughLines[i][0],sortedHoughLines[i][1],sortedHoughLines[j][0],sortedHoughLines[j][1]))
	print(intersectionPoints)
	finalIntersectionPoints = []

	for point in intersectionPoints:
		if (not(point == None)):
			x = point[0]
			y = point[1]
			if (not(x < 0 or x > width-1) and not(y < 0 or y > height-1)):
				finalIntersectionPoints.append((int(x),int(y)))
	finalIntersectionPoints = np.array(finalIntersectionPoints,dtype='float32')
	print(finalIntersectionPoints)

	transformMatrix = cv2.getPerspectiveTransform(finalIntersectionPoints,np.array([(0,0),(0,height),(width,0),(width,height)],dtype='float32'))

	outputImage = cv2.warpPerspective(imgBinary,transformMatrix,(width,height))
	return outputImage

	# Output the thresholded image, the page outline and the hough transform output on the original image to two separate jpg files
	"""
Beispiel #2
0
def process(imgInput):
    width = len(imgInput[0])
    height = len(imgInput)

    # Threshold
    print("[OMR Image Preprocessing] Thresholding...")
    imgBinary = omr_threshold_image.threshold(imgInput)

    # Find largest connected component
    print(
        "[OMR Image Preprocessing] Finding largest connected component... (may take a few minutes)"
    )
    imgLCC = largestConnectedComponent(imgBinary)

    # Fill page with white pixels
    print("[OMR Image Preprocessing] Filling page with white pixels...")
    imgPageFilled = fillPage(imgLCC)

    # Edge detection
    print("[OMR Image Preprocessing] Performing edge detection...")
    imgEdges = cv2.Canny(imgPageFilled, 50, 150)

    # Dilate edges
    print("[OMR Image Preprocessing] Dilating edges...")
    dilationKernel = np.ones((8, 8), np.uint8)
    imgEdgesDilated = cv2.dilate(imgEdges, dilationKernel, iterations=1)

    # Hough line transform
    print("[OMR Image Preprocessing] Performing Hough line transform...")
    houghLines = cv2.HoughLines(imgEdgesDilated, 1, np.pi / 180,
                                min(width / 2, height / 2))

    # Sort lines by rho value

    sortedHoughLines = sorted(houghLines[0], key=lambda x: x[0])
    """
	for i in range(0,len(sortedHoughLines)):
		if (sortedHoughLines[i][0] < 0):
			sortedHoughLines[i][0] *= (-1)
			sortedHoughLines[i][1] = (sortedHoughLines[i][1] + np.pi)%(2*np.pi)
	"""
    distanceThreshold = 1

    while (len(sortedHoughLines) > 4):
        nextHoughLines = []
        length = len(sortedHoughLines)
        skip = False
        for i in range(0, length):
            rho1 = sortedHoughLines[i][0]
            rho2 = sortedHoughLines[(i + 1) % length][0]
            theta1 = sortedHoughLines[i][1]
            theta2 = sortedHoughLines[(i + 1) % length][1]

            distance = math.sqrt(rho1 * rho1 + rho2 * rho2 -
                                 2 * rho1 * rho2 * np.cos(theta2 - theta1))
            if (not (skip)):
                if (distance < distanceThreshold):
                    #nextHoughLines.append(sortedHoughLines[i])
                    nextHoughLines.append(
                        np.array([(rho1 + rho2) / 2, (theta1 + theta2) / 2]))
                    skip = True
                else:
                    nextHoughLines.append(sortedHoughLines[i])
            else:
                skip = False

        distanceThreshold += 1
        sortedHoughLines = copy.deepcopy(nextHoughLines)
        print('New iteration:')
        for rho, theta in sortedHoughLines:
            print('rho: ' + str(rho) + ' theta: ' + str(theta))

    # Highlight lines on original image
    for rho, theta in sortedHoughLines:
        print('rho: ' + str(rho) + ' theta: ' + str(theta))
        cosTheta = np.cos(theta)
        sinTheta = np.sin(theta)
        x0 = cosTheta * rho
        y0 = sinTheta * rho
        length = max(width, height)
        x1 = int(x0 + length * (-sinTheta))
        y1 = int(y0 + length * (cosTheta))
        x2 = int(x0 - length * (-sinTheta))
        y2 = int(y0 - length * (cosTheta))

        cv2.line(imgInput, (x1, y1), (x2, y2), (0, 0, 255), 2)

    # Perform perspective transform
    intersectionPoints = []

    for i in range(0, len(sortedHoughLines) - 1):
        for j in range(i + 1, len(sortedHoughLines)):
            intersectionPoints.append(
                intersectPolarLines(sortedHoughLines[i][0],
                                    sortedHoughLines[i][1],
                                    sortedHoughLines[j][0],
                                    sortedHoughLines[j][1]))
    print(intersectionPoints)
    finalIntersectionPoints = []

    for point in intersectionPoints:
        if (not (point == None)):
            x = point[0]
            y = point[1]
            if (not (x < 0 or x > width - 1)
                    and not (y < 0 or y > height - 1)):
                finalIntersectionPoints.append((int(x), int(y)))
    finalIntersectionPoints = np.array(finalIntersectionPoints,
                                       dtype='float32')
    print(finalIntersectionPoints)

    transformMatrix = cv2.getPerspectiveTransform(
        finalIntersectionPoints,
        np.array([(0, 0), (0, height), (width, 0), (width, height)],
                 dtype='float32'))

    outputImage = cv2.warpPerspective(imgBinary, transformMatrix,
                                      (width, height))
    return outputImage

    # Output the thresholded image, the page outline and the hough transform output on the original image to two separate jpg files
    """
Beispiel #3
0
import sys
import cv2
import omr_threshold_image
import omr_classes
import omr_staff_line_detection
import omr_staff_line_removal
import omr_recognition
import omr_reconstruction
import omr_bar_line_detection

img = cv2.imread(sys.argv[1],0)

# Threshold
img = omr_threshold_image.threshold(img)

# Copy of thresholded image for recognition output
imgRecognitionOutput = img.copy()
imgRecognitionOutput = cv2.cvtColor(imgRecognitionOutput,cv2.COLOR_GRAY2RGB)

# Get staff line detection data
staffData = omr_staff_line_detection.getStaffData(img)

# Remove staff lines
#img = omr_staff_line_removal.removeStaffLines(img,staffData)

# Test removeStaffLinesSP
img = omr_staff_line_removal.removeStaffLinesSP(img,staffData)
raw_input("Press Enter to continue...")

# Perform recognition
musicalObjects = omr_recognition.performRecognition(img,staffData)