Created
December 19, 2015 22:11
-
-
Save Khalian/5399c75b9aadc13051ac to your computer and use it in GitHub Desktop.
Code to remove lines from sheet music files and/or display them
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
__author__ = 'asanyal' | |
import cv2 | |
import numpy as np | |
def getHoughLines(imgFile): | |
img = cv2.imread(imgFile) | |
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY) | |
edges = cv2.Canny(gray,50,150,apertureSize = 3) | |
lines = cv2.HoughLines(edges, 1, np.pi/180, 200) | |
for line in lines: | |
for rho,theta in line: | |
a = np.cos(theta) | |
b = np.sin(theta) | |
x0 = a*rho | |
y0 = b*rho | |
x1 = int(x0 + 1000*(-b)) | |
y1 = int(y0 + 1000*(a)) | |
x2 = int(x0 - 1000*(-b)) | |
y2 = int(y0 - 1000*(a)) | |
cv2.line(img,(x1,y1),(x2,y2),(0,0,255),2) | |
cv2.imshow('gray',img) | |
cv2.waitKey(0) | |
def removeLinesFromSheet(imgFile): | |
img = cv2.imread(imgFile) | |
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY) | |
adaptiveThreshHoldImage = cv2.adaptiveThreshold(~gray, 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY, 15, -2) | |
cv2.imshow('gray', adaptiveThreshHoldImage) | |
horizontal = adaptiveThreshHoldImage.copy() | |
vertical = adaptiveThreshHoldImage.copy() | |
(rows, cols) = adaptiveThreshHoldImage.shape | |
horizontalSize = cols/30 | |
verticalSize = rows/30 | |
horizontalStructure = cv2.getStructuringElement(cv2.MORPH_RECT,(horizontalSize, 1)) | |
horizontal = cv2.erode(horizontal, horizontalStructure, iterations = 1) | |
horizontal = cv2.dilate(horizontal, horizontalStructure, iterations = 1) | |
cv2.imshow('horizontal', horizontal) | |
verticalStructure = cv2.getStructuringElement(cv2.MORPH_RECT,(1, 3)) | |
vertical = cv2.erode(vertical, verticalStructure, iterations = 1); | |
vertical = cv2.dilate(vertical, verticalStructure, iterations = 1); | |
vertical = cv2.bitwise_not(vertical) | |
cv2.imshow('vertical', vertical) | |
smoothedThreshHoldImg = cv2.adaptiveThreshold(vertical, 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY, 3, -2) | |
cv2.imshow('edges', smoothedThreshHoldImg) | |
cv2.waitKey(0) | |
def read_staff_lines(img): | |
edges = cv2.Canny(img,150,700) | |
lines = cv2.HoughLines(edges,1,np.pi/180,200) | |
staff_lines = [] | |
for rho,theta in lines[0]: | |
a = np.cos(theta) | |
b = np.sin(theta) | |
x0 = a*rho | |
y0 = b*rho | |
x1 = int(x0 + 1000*(-b)) | |
y1 = int(y0 + 1000*(a)) | |
x2 = int(x0 - 1000*(-b)) | |
y2 = int(y0 - 1000*(a)) | |
th = 10 #pixels | |
too_close = False | |
# only appends lines if they're a reasonable distance | |
# from other lines | |
# would be better if used averaging between close lines instead of | |
# just taking the first one we see and tossing the other | |
for y in staff_lines: | |
if abs(y - y1) <= th: | |
too_close = True | |
if not too_close: | |
staff_lines.append((y1)) | |
return staff_lines | |
# Running a test case | |
def main(): | |
getHoughLines("sampleFile.png") | |
removeLinesFromSheet("sampleFile.png") | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment