{"id":2861,"date":"2025-04-06T09:36:46","date_gmt":"2025-04-06T00:36:46","guid":{"rendered":"https:\/\/plaza.umin.ac.jp\/~OIO\/cornetto\/?p=2861"},"modified":"2025-04-06T10:27:49","modified_gmt":"2025-04-06T01:27:49","slug":"%e7%94%bb%e5%83%8f%e3%81%ae%e8%ad%9c%e9%9d%a2%e3%82%92midi%e3%83%95%e3%82%a1%e3%82%a4%e3%83%ab%e3%81%b8%e5%a4%89%e6%8f%9b","status":"publish","type":"post","link":"https:\/\/plaza.umin.ac.jp\/~OIO\/cornetto\/2025\/04\/06\/%e7%94%bb%e5%83%8f%e3%81%ae%e8%ad%9c%e9%9d%a2%e3%82%92midi%e3%83%95%e3%82%a1%e3%82%a4%e3%83%ab%e3%81%b8%e5%a4%89%e6%8f%9b\/","title":{"rendered":"\u753b\u50cf\u306e\u8b5c\u9762\u3092MIDI\u30d5\u30a1\u30a4\u30eb\u3078\u5909\u63db"},"content":{"rendered":"\n<p>\u5199\u30e1\u3068\u304b\u30b9\u30ad\u30e3\u30f3\u3057\u305f\u8b5c\u9762\u306e\u753b\u50cf\u30d5\u30a1\u30a4\u30eb\u3092MIDI\u306b\u5909\u63db\u3067\u304d\u305f\u3089\u3068\u601d\u3063\u3066\u8a66\u3057\u3066\u3044\u307e\u3059\u3002\u304b\u3064\u3066\u4f7f\u3063\u3066\u3044\u305ffinale\u306b\u3082\u305d\u306e\u6a5f\u80fd\u304c\u3042\u3063\u305f\u306e\u3067\u3059\u304c\u3001\u79c1\u304c\u4f7f\u7528\u3057\u3066\u3044\u305f\u7248\u304c2012\u7248\u3068\u53e4\u304b\u3063\u305f\u306e\u304b\u3042\u307e\u308a\u6b63\u78ba\u306a\u5909\u63db\u306f\u3067\u304d\u306a\u304b\u3063\u305f\u3067\u3059\u3002muse score\u306b\u3082\u5909\u63db\u6a5f\u80fd\u3042\u308a\u307e\u3059\u304c\u3001web\u30b5\u30a4\u30c8\u3078\u8a98\u5c0e\u3055\u308c\u3066\u3001\u305d\u3053\u3078\u8b5c\u9762\u3092\u30a2\u30c3\u30d7\u30ed\u30fc\u30c9\u3059\u308b\u5f62\u306b\u306a\u308a\u307e\u3059\u3002\u753b\u50cf\u3092\u304d\u308c\u3044\u306b\u6574\u3048\u3066\u304b\u3089\u30a2\u30c3\u30d7\u3057\u3066\u3082\u3001\u5927\u304d\u304f\u624b\u76f4\u3057\u3057\u306a\u3044\u3068\u3060\u3081\u3067\u3001\u76f4\u63a5\u5165\u529b\u306e\u307b\u3046\u304c\u826f\u3044\u306e\u3067\u306f\u306a\u3044\u304b\u3068\u3044\u3046\u304f\u3089\u3044\u3001\u79c1\u306e\u76ee\u7684\u306b\u5bfe\u3057\u3066\u306f\u975e\u5e38\u306b\u4e0d\u6b63\u78ba\u306a\u51fa\u529b\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n\n\n\n<p>\u6700\u8fd1(2025\u5e74\u73fe\u5728)\u306f\u3044\u308d\u3044\u308d\u306a\u6709\u6599\u7248\u30bd\u30d5\u30c8\u3067\u305d\u306e\u6a5f\u80fd\u304c\u8ffd\u52a0\u3055\u308c\u3066\u3044\u307e\u3059\u306e\u3067\u3001\u304a\u3068\u306a\u3057\u304f\u8ab2\u91d1\u3059\u308b\u306e\u304c\u6b63\u89e3\u3060\u308d\u3046\u3068\u306f\u601d\u3044\u307e\u3059\u3002<\/p>\n\n\n\n<p>\u4eca\u56de\u306f\u3001\u7121\u6599\u3067\u4f7f\u3048\u308bpython\u306e\u30b9\u30af\u30ea\u30d7\u30c8\u3067\u5b9f\u884c\u3059\u308b\u3053\u3068\u3092\u76ee\u6307\u3057\u3066\u3044\u307e\u3059\u3002<\/p>\n\n\n\n<p><strong>Install the required libraries<\/strong>:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>opencv-python<\/code>&nbsp;for image processing.<\/li>\n\n\n\n<li><code>midiutil<\/code>&nbsp;for creating MIDI files.<\/li>\n\n\n\n<li><code>numpy<\/code>&nbsp;for numerical operations.<\/li>\n<\/ul>\n\n\n\n<p>You can install these libraries using pip:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>pip install opencv-python midiutil numpy<\/code><\/pre>\n\n\n\n<p>\u307e\u305a\u306f\u3001Python\u306e\u30e9\u30a4\u30d6\u30e9\u30ea\u3067\u3042\u308b<code>opencv-python<\/code>\u3001<code>midiutil<\/code>\u3001<code>numpy<\/code>\u304c\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u3055\u308c\u307e\u3059\u3002\u3053\u308c\u3089\u306e\u30e9\u30a4\u30d6\u30e9\u30ea\u306f\u3001\u753b\u50cf\u51e6\u7406\u3084MIDI\u30d5\u30a1\u30a4\u30eb\u306e\u4f5c\u6210\u3001\u6570\u5024\u8a08\u7b97\u306b\u5f79\u7acb\u3061\u307e\u3059\u3002<\/p>\n\n\n\n<p><\/p>\n\n\n\n<h3 class=\"wp-block-heading\">1. \u30e9\u30a4\u30d6\u30e9\u30ea\u306e\u30a4\u30f3\u30dd\u30fc\u30c8<\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nimport cv2\nimport numpy as np\nfrom midiutil import MIDIFile\n<\/pre><\/div>\n\n\n<ul class=\"wp-block-list\">\n<li><code>cv2<\/code>: OpenCV\u3068\u3044\u3046\u30e9\u30a4\u30d6\u30e9\u30ea\u3067\u3001\u753b\u50cf\u51e6\u7406\u3092\u884c\u3044\u307e\u3059\u3002<\/li>\n\n\n\n<li><code>numpy<\/code>: \u6570\u5024\u8a08\u7b97\u3092\u52b9\u7387\u7684\u306b\u884c\u3046\u305f\u3081\u306e\u30e9\u30a4\u30d6\u30e9\u30ea\u3067\u3059\u3002<\/li>\n\n\n\n<li><code>midiutil<\/code>: MIDI\u30d5\u30a1\u30a4\u30eb\u3092\u4f5c\u6210\u3059\u308b\u305f\u3081\u306e\u30e9\u30a4\u30d6\u30e9\u30ea\u3067\u3059\u3002<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">2. \u753b\u50cf\u306e\u8aad\u307f\u8fbc\u307f<\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nimage_path = &#039;path_to_your_image.jpg&#039;\nimage = cv2.imread(image_path, cv2.IMREAD_GRAYSCALE)\n<\/pre><\/div>\n\n\n<ul class=\"wp-block-list\">\n<li><code>image_path<\/code>: \u753b\u50cf\u30d5\u30a1\u30a4\u30eb\u306e\u30d1\u30b9\u3092\u6307\u5b9a\u3057\u307e\u3059\u3002\u30a6\u30a4\u30f3\u30c9\u30a6\u30ba\u306e\u30d1\u30b9\u306e\u30d0\u30c3\u30af\u30b9\u30e9\u30c3\u30b7\u30e5\uff08\uffe5\u3067\u8868\u793a\u3055\u308c\u308b\u5834\u5408\u3082\u3042\u308a\u307e\u3059\uff09\u306f\u30b9\u30e9\u30c3\u30b7\u30e5\/\u306b\u4fee\u6b63\u3059\u308b\u304b\u3001\\\\\u306e\u3088\u3046\u306b2\u56de\u30d0\u30c3\u30af\u30b9\u30e9\u30c3\u30b7\u30e5\u3092\u542b\u3080\u8a18\u53f7\u306b\u3057\u3085\u3046\u305b\u3044\u304c\u5fc5\u8981\u3002<\/li>\n\n\n\n<li><code>cv2.imread<\/code>: \u753b\u50cf\u3092\u8aad\u307f\u8fbc\u307f\u307e\u3059\u3002<code>cv2.IMREAD_GRAYSCALE<\/code>\u306f\u753b\u50cf\u3092\u30b0\u30ec\u30fc\u30b9\u30b1\u30fc\u30eb\uff08\u767d\u9ed2\uff09\u3067\u8aad\u307f\u8fbc\u3080\u30aa\u30d7\u30b7\u30e7\u30f3\u3067\u3059\u3002<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">3. \u753b\u50cf\u306e\u524d\u51e6\u7406<\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nblurred = cv2.GaussianBlur(image, (5, 5), 0)\n_, binary_image = cv2.threshold(blurred, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)\n<\/pre><\/div>\n\n\n<ul class=\"wp-block-list\">\n<li><code>cv2.GaussianBlur<\/code>: \u753b\u50cf\u3092\u307c\u304b\u3057\u3066\u30ce\u30a4\u30ba\u3092\u6e1b\u3089\u3057\u307e\u3059\u3002<\/li>\n\n\n\n<li><code>cv2.threshold<\/code>: \u753b\u50cf\u3092\u4e8c\u5024\u5316\uff08\u767d\u3068\u9ed2\u3060\u3051\u306b\u3059\u308b\uff09\u3057\u307e\u3059\u3002<code>cv2.THRESH_BINARY + cv2.THRESH_OTSU<\/code>\u306f\u81ea\u52d5\u7684\u306b\u9069\u5207\u306a\u95be\u5024\u3092\u9078\u3073\u307e\u3059\u3002<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">4. \u97f3\u7b26\u306e\u8f2a\u90ed\u3092\u898b\u3064\u3051\u308b<\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\ncontours, _ = cv2.findContours(binary_image, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)\n<\/pre><\/div>\n\n\n<ul class=\"wp-block-list\">\n<li><code>cv2.findContours<\/code>: \u753b\u50cf\u306e\u4e2d\u306e\u8f2a\u90ed\u3092\u898b\u3064\u3051\u307e\u3059\u3002<code>cv2.RETR_EXTERNAL<\/code>\u306f\u5916\u5074\u306e\u8f2a\u90ed\u3060\u3051\u3092\u898b\u3064\u3051\u3001<code>cv2.CHAIN_APPROX_SIMPLE<\/code>\u306f\u8f2a\u90ed\u306e\u70b9\u3092\u7c21\u7565\u5316\u3057\u307e\u3059\u3002<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">5. \u97f3\u7b26\u3092MIDI\u30ce\u30fc\u30c8\u306b\u5909\u63db\u3059\u308b\u95a2\u6570<\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\ndef note_to_midi(note):\n    note_mapping = {\n        &#039;C&#039;: 60, &#039;C#&#039;: 61, &#039;D&#039;: 62, &#039;D#&#039;: 63, &#039;E&#039;: 64,\n        &#039;F&#039;: 65, &#039;F#&#039;: 66, &#039;G&#039;: 67, &#039;G#&#039;: 68, &#039;A&#039;: 69,\n        &#039;A#&#039;: 70, &#039;B&#039;: 71\n    }\n    return note_mapping.get(note, 60)  # Default to C4 if note not found\n<\/pre><\/div>\n\n\n<ul class=\"wp-block-list\">\n<li><code>note_to_midi<\/code>: \u97f3\u7b26\u306e\u540d\u524d\u3092MIDI\u30ce\u30fc\u30c8\u756a\u53f7\u306b\u5909\u63db\u3059\u308b\u95a2\u6570\u3067\u3059\u3002\u4f8b\u3048\u3070\u3001<code>C<\/code>\u306f60\uff08C4\uff09\u306b\u5bfe\u5fdc\u3057\u307e\u3059\u3002<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">6. MIDI\u30d5\u30a1\u30a4\u30eb\u306e\u4f5c\u6210<\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nmidi_file = MIDIFile(1)\nmidi_file.addTempo(0, 0, 120)\n<\/pre><\/div>\n\n\n<ul class=\"wp-block-list\">\n<li><code>MIDIFile(1)<\/code>: 1\u30c8\u30e9\u30c3\u30af\u306eMIDI\u30d5\u30a1\u30a4\u30eb\u3092\u4f5c\u6210\u3057\u307e\u3059\u3002<\/li>\n\n\n\n<li><code>addTempo<\/code>: \u30c6\u30f3\u30dd\uff08\u901f\u5ea6\uff09\u3092\u8a2d\u5b9a\u3057\u307e\u3059\u3002\u3053\u3053\u3067\u306f120 BPM\u3067\u3059\u3002<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">7. \u97f3\u7b26\u3092MIDI\u30d5\u30a1\u30a4\u30eb\u306b\u8ffd\u52a0<\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nfor contour in contours:\n    x, y, w, h = cv2.boundingRect(contour)\n    if h &amp;gt; 10:  # Filter out small contours that are not notes\n        note = &#039;C&#039;  # Placeholder for actual note detection logic\n        midi_note = note_to_midi(note)\n        midi_file.addNote(0, 0, midi_note, x \/ 10.0, 1, 100)\n<\/pre><\/div>\n\n\n<ul class=\"wp-block-list\">\n<li><code>cv2.boundingRect<\/code>: \u8f2a\u90ed\u3092\u56f2\u3080\u77e9\u5f62\u3092\u53d6\u5f97\u3057\u307e\u3059\u3002<\/li>\n\n\n\n<li><code>addNote<\/code>: MIDI\u30d5\u30a1\u30a4\u30eb\u306b\u97f3\u7b26\u3092\u8ffd\u52a0\u3057\u307e\u3059\u3002\u3053\u3053\u3067\u306f\u3001\u97f3\u7b26\u306e\u9ad8\u3055\uff08<code>midi_note<\/code>\uff09\u3001\u958b\u59cb\u6642\u9593\uff08<code>x \/ 10.0<\/code>\uff09\u3001\u9577\u3055\uff081\uff09\u3001\u30d9\u30ed\u30b7\u30c6\u30a3\uff08100\uff09\u3092\u6307\u5b9a\u3057\u307e\u3059\u3002<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">8. MIDI\u30d5\u30a1\u30a4\u30eb\u306e\u4fdd\u5b58<\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nwith open(&quot;output.mid&quot;, &quot;wb&quot;) as output_file:\n    midi_file.writeFile(output_file)\n<\/pre><\/div>\n\n\n<ul class=\"wp-block-list\">\n<li><code>open<\/code>: \u30d5\u30a1\u30a4\u30eb\u3092\u958b\u304d\u307e\u3059\u3002<code>\"wb\"<\/code>\u306f\u66f8\u304d\u8fbc\u307f\u30e2\u30fc\u30c9\uff08\u30d0\u30a4\u30ca\u30ea\uff09\u3067\u3059\u3002<\/li>\n\n\n\n<li><code>writeFile<\/code>: MIDI\u30d5\u30a1\u30a4\u30eb\u3092\u66f8\u304d\u8fbc\u307f\u307e\u3059\u3002<\/li>\n<\/ul>\n\n\n\n<p><\/p>\n\n\n\n<p>\u5b9f\u884c\u30d5\u30a1\u30a4\u30eb\u306e\u307b\u3046\u3092\u307e\u3068\u3081\u307e\u3057\u305f\u3002\u3053\u308c\u3092\u300cimage2midi.py\u300d\u306e\u3088\u3046\u306a\u540d\u524d\u3067\u4fdd\u5b58\u3057\u3066\u5b9f\u884c\u3057\u307e\u3059<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>python image2midi.py<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>\n\nimport cv2\nimport numpy as np\nfrom midiutil import MIDIFile\n\n# Load the image\nimage_path = 'X:\/\uff0a\uff0a\uff0a\uff0a\/\uff0a\uff0a\uff0a\uff0a\/\uff0a\uff0a\uff0a\uff0a.jpg'\nimage = cv2.imread(image_path, cv2.IMREAD_GRAYSCALE)\n\n# Preprocess the image for better OCR results\nblurred = cv2.GaussianBlur(image, (5, 5), 0)\n_, binary_image = cv2.threshold(blurred, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)\n\n# Use OpenCV to find contours of the notes\ncontours, _ = cv2.findContours(binary_image, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)\n\n# Function to map note positions to MIDI notes\ndef note_to_midi(note):\n    note_mapping = {\n        'C': 60, 'C#': 61, 'D': 62, 'D#': 63, 'E': 64,\n        'F': 65, 'F#': 66, 'G': 67, 'G#': 68, 'A': 69,\n        'A#': 70, 'B': 71\n    }\n    return note_mapping.get(note, 60)  # Default to C4 if note not found\n\n# Create a MIDI file with one track\nmidi_file = MIDIFile(1)\nmidi_file.addTempo(0, 0, 120)\n\n# Iterate through contours and add notes to the MIDI file\nfor contour in contours:\n    x, y, w, h = cv2.boundingRect(contour)\n    if h &gt; 10:  # Filter out small contours that are not notes\n        note = 'C'  # Placeholder for actual note detection logic\n        midi_note = note_to_midi(note)\n        midi_file.addNote(0, 0, midi_note, x \/ 10.0, 1, 100)\n\n# Save the MIDI file\nwith open(\"output.mid\", \"wb\") as output_file:\n    midi_file.writeFile(output_file)\n\nprint(\"MIDI file has been created successfully.\")<\/code><\/pre>\n\n\n\n<p>\u30a4\u30f3\u30d7\u30c3\u30c8\u30d5\u30a1\u30a4\u30eb\u2193<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"503\" height=\"646\" src=\"https:\/\/i0.wp.com\/plaza.umin.ac.jp\/~OIO\/cornetto\/wp-content\/uploads\/sites\/2\/2025\/04\/image-1.png?resize=503%2C646&#038;ssl=1\" alt=\"\" class=\"wp-image-2868\" srcset=\"https:\/\/i0.wp.com\/plaza.umin.ac.jp\/~OIO\/cornetto\/wp-content\/uploads\/sites\/2\/2025\/04\/image-1.png?w=503&amp;ssl=1 503w, https:\/\/i0.wp.com\/plaza.umin.ac.jp\/~OIO\/cornetto\/wp-content\/uploads\/sites\/2\/2025\/04\/image-1.png?resize=234%2C300&amp;ssl=1 234w\" sizes=\"auto, (max-width: 503px) 100vw, 503px\" \/><\/figure>\n\n\n\n<p>\u30a2\u30a6\u30c8\u30d7\u30c3\u30c8\u30d5\u30a1\u30a4\u30eb\u2193<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"687\" height=\"720\" src=\"https:\/\/i0.wp.com\/plaza.umin.ac.jp\/~OIO\/cornetto\/wp-content\/uploads\/sites\/2\/2025\/04\/image.png?resize=687%2C720&#038;ssl=1\" alt=\"\" class=\"wp-image-2867\" srcset=\"https:\/\/i0.wp.com\/plaza.umin.ac.jp\/~OIO\/cornetto\/wp-content\/uploads\/sites\/2\/2025\/04\/image.png?w=687&amp;ssl=1 687w, https:\/\/i0.wp.com\/plaza.umin.ac.jp\/~OIO\/cornetto\/wp-content\/uploads\/sites\/2\/2025\/04\/image.png?resize=286%2C300&amp;ssl=1 286w, https:\/\/i0.wp.com\/plaza.umin.ac.jp\/~OIO\/cornetto\/wp-content\/uploads\/sites\/2\/2025\/04\/image.png?resize=630%2C660&amp;ssl=1 630w\" sizes=\"auto, (max-width: 687px) 100vw, 687px\" \/><\/figure>\n\n\n\n<p>\u6b8b\u5ff5\u3067\u3057\u305f\u3002\u3053\u308c\u3058\u3083\u8a71\u306b\u306a\u3089\u306a\u3044\u306e\u3067\u3001\u3082\u3046\u5c11\u3057\u60aa\u3042\u304c\u304d\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">9. \u97f3\u7b26\u306e\u4f4d\u7f6e\u3092\u6b63\u78ba\u306b\u691c\u51fa<\/h3>\n\n\n\n<p>\u97f3\u7b26\u306e\u4f4d\u7f6e\u3092\u6b63\u78ba\u306b\u691c\u51fa\u3059\u308b\u305f\u3081\u306b\u3001\u97f3\u7b26\u306e\u5f62\u72b6\u306b\u57fa\u3065\u3044\u305f\u30d5\u30a3\u30eb\u30bf\u30ea\u30f3\u30b0\u3092\u884c\u3044\u307e\u3059\u3002\u4ee5\u4e0b\u306e\u30b3\u30fc\u30c9\u3067\u306f\u3001\u97f3\u7b26\u306e\u5f62\u72b6\u3092\u8003\u616e\u3057\u3066\u30d5\u30a3\u30eb\u30bf\u30ea\u30f3\u30b0\u3092\u884c\u3063\u3066\u3044\u307e\u3059\u3002<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n# Iterate through contours and add notes to the MIDI file\nfor contour in contours:\n    x, y, w, h = cv2.boundingRect(contour)\n    aspect_ratio = w \/ float(h)\n    if 0.2 &amp;lt; aspect_ratio &amp;lt; 0.8 and h &amp;gt; 10:  # Filter based on aspect ratio and height\n        note = &#039;C&#039;  # Placeholder for actual note detection logic\n        midi_note = note_to_midi(note)\n        midi_file.addNote(0, 0, midi_note, x \/ 10.0, 1, 100)\n\n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\">10. \u97f3\u7b26\u306e\u5f62\u72b6\u3092\u8003\u616e\u3057\u305f\u30d5\u30a3\u30eb\u30bf\u30ea\u30f3\u30b0<\/h3>\n\n\n\n<p>\u97f3\u7b26\u306e\u5f62\u72b6\u3092\u8003\u616e\u3057\u3066\u30d5\u30a3\u30eb\u30bf\u30ea\u30f3\u30b0\u3092\u884c\u3046\u3053\u3068\u3067\u3001\u97f3\u7b26\u306e\u8a8d\u8b58\u7cbe\u5ea6\u3092\u5411\u4e0a\u3055\u305b\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002\u4ee5\u4e0b\u306e\u30b3\u30fc\u30c9\u3067\u306f\u3001\u97f3\u7b26\u306e\u5f62\u72b6\u306b\u57fa\u3065\u3044\u305f\u30d5\u30a3\u30eb\u30bf\u30ea\u30f3\u30b0\u3092\u884c\u3063\u3066\u3044\u307e\u3059\u3002<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n# Iterate through contours and add notes to the MIDI file\nfor contour in contours:\n    x, y, w, h = cv2.boundingRect(contour)\n    aspect_ratio = w \/ float(h)\n    if 0.2 &amp;lt; aspect_ratio &amp;lt; 0.8 and h &amp;gt; 10:  # Filter based on aspect ratio and height\n        note = &#039;C&#039;  # Placeholder for actual note detection logic\n        midi_note = note_to_midi(note)\n        midi_file.addNote(0, 0, midi_note, x \/ 10.0, 1, 100)\n<\/pre><\/div>\n\n\n<p>\u3067\u3001\u5168\u4f53\u50cf image2MIDI.py\u306f<br><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import cv2\nimport numpy as np\nfrom midiutil import MIDIFile\n\n# Load the image\nimage_path = '\uff58:\/\uff0a\uff0a\uff0a\uff0a\/\uff0a\uff0a\uff0a.jpg'\nimage = cv2.imread(image_path, cv2.IMREAD_GRAYSCALE)\n\n# Enhance the image contrast\nimage = cv2.equalizeHist(image)\n\n# Preprocess the image for better OCR results\nblurred = cv2.GaussianBlur(image, (5, 5), 0)\nedges = cv2.Canny(blurred, 50, 150)\n_, binary_image = cv2.threshold(edges, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)\n\n# Use OpenCV to find contours of the notes\ncontours, _ = cv2.findContours(binary_image, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)\n\n# Function to map note positions to MIDI notes\ndef note_to_midi(note):\n    note_mapping = {\n        'C': 60, 'C#': 61, 'D': 62, 'D#': 63, 'E': 64,\n        'F': 65, 'F#': 66, 'G': 67, 'G#': 68, 'A': 69,\n        'A#': 70, 'B': 71\n    }\n    return note_mapping.get(note, 60)  # Default to C4 if note not found\n\n# Create a MIDI file with one track\nmidi_file = MIDIFile(1)\nmidi_file.addTempo(0, 0, 120)\n\n# Iterate through contours and add notes to the MIDI file\nfor contour in contours:\n    x, y, w, h = cv2.boundingRect(contour)\n    aspect_ratio = w \/ float(h)\n    if 0.2 &lt; aspect_ratio &lt; 0.8 and h &gt; 10:  # Filter based on aspect ratio and height\n        note = 'C'  # Placeholder for actual note detection logic\n        midi_note = note_to_midi(note)\n        midi_file.addNote(0, 0, midi_note, x \/ 10.0, 1, 100)\n\n# Save the MIDI file\nwith open(\"output.mid\", \"wb\") as output_file:\n    midi_file.writeFile(output_file)\n\nprint(\"MIDI file has been created successfully.\")<\/code><\/pre>\n\n\n\n<p>\u3067\u3001\u3053\u306e\u51fa\u529b\u306f<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"700\" height=\"828\" src=\"https:\/\/i0.wp.com\/plaza.umin.ac.jp\/~OIO\/cornetto\/wp-content\/uploads\/sites\/2\/2025\/04\/image-2.png?resize=700%2C828&#038;ssl=1\" alt=\"\" class=\"wp-image-2874\" srcset=\"https:\/\/i0.wp.com\/plaza.umin.ac.jp\/~OIO\/cornetto\/wp-content\/uploads\/sites\/2\/2025\/04\/image-2.png?w=700&amp;ssl=1 700w, https:\/\/i0.wp.com\/plaza.umin.ac.jp\/~OIO\/cornetto\/wp-content\/uploads\/sites\/2\/2025\/04\/image-2.png?resize=254%2C300&amp;ssl=1 254w, https:\/\/i0.wp.com\/plaza.umin.ac.jp\/~OIO\/cornetto\/wp-content\/uploads\/sites\/2\/2025\/04\/image-2.png?resize=649%2C768&amp;ssl=1 649w, https:\/\/i0.wp.com\/plaza.umin.ac.jp\/~OIO\/cornetto\/wp-content\/uploads\/sites\/2\/2025\/04\/image-2.png?resize=558%2C660&amp;ssl=1 558w\" sizes=\"auto, (max-width: 700px) 100vw, 700px\" \/><\/figure>\n\n\n\n<p>\u97f3\u7b26\u5897\u3048\u305f\u3051\u3069\u3001\u30c0\u30e1\u30c0\u30e1\u3067\u3057\u305f<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5199\u30e1\u3068\u304b\u30b9\u30ad\u30e3\u30f3\u3057\u305f\u8b5c\u9762\u306e\u753b\u50cf\u30d5\u30a1\u30a4\u30eb\u3092MIDI\u306b\u5909\u63db\u3067\u304d\u305f\u3089\u3068\u601d\u3063\u3066\u8a66\u3057\u3066\u3044\u307e\u3059\u3002\u304b\u3064\u3066\u4f7f\u3063\u3066\u3044\u305ffinale\u306b\u3082\u305d\u306e\u6a5f\u80fd\u304c\u3042\u3063\u305f\u306e\u3067\u3059\u304c\u3001\u79c1\u304c\u4f7f\u7528\u3057\u3066\u3044\u305f\u7248\u304c2012\u7248\u3068\u53e4\u304b\u3063\u305f\u306e\u304b\u3042\u307e\u308a\u6b63\u78ba\u306a\u5909\u63db\u306f\u3067\u304d\u306a\u304b\u3063\u305f\u3067\u3059 &hellip; <a href=\"https:\/\/plaza.umin.ac.jp\/~OIO\/cornetto\/2025\/04\/06\/%e7%94%bb%e5%83%8f%e3%81%ae%e8%ad%9c%e9%9d%a2%e3%82%92midi%e3%83%95%e3%82%a1%e3%82%a4%e3%83%ab%e3%81%b8%e5%a4%89%e6%8f%9b\/\" class=\"more-link\"><span class=\"more-button\">\u7d9a\u304d\u3092\u8aad\u3080<span class=\"screen-reader-text\">\u753b\u50cf\u306e\u8b5c\u9762\u3092MIDI\u30d5\u30a1\u30a4\u30eb\u3078\u5909\u63db<\/span><\/span><\/a><\/p>\n","protected":false},"author":5,"featured_media":2869,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[1],"tags":[],"class_list":["post-2861","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-1"],"jetpack_featured_media_url":"https:\/\/i0.wp.com\/plaza.umin.ac.jp\/~OIO\/cornetto\/wp-content\/uploads\/sites\/2\/2025\/04\/0b27181254ef8d7dfe8ec3f0a339bfb3.jpeg?fit=1024%2C1024&ssl=1","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/plaza.umin.ac.jp\/~OIO\/cornetto\/wp-json\/wp\/v2\/posts\/2861","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/plaza.umin.ac.jp\/~OIO\/cornetto\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/plaza.umin.ac.jp\/~OIO\/cornetto\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/plaza.umin.ac.jp\/~OIO\/cornetto\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/plaza.umin.ac.jp\/~OIO\/cornetto\/wp-json\/wp\/v2\/comments?post=2861"}],"version-history":[{"count":8,"href":"https:\/\/plaza.umin.ac.jp\/~OIO\/cornetto\/wp-json\/wp\/v2\/posts\/2861\/revisions"}],"predecessor-version":[{"id":2875,"href":"https:\/\/plaza.umin.ac.jp\/~OIO\/cornetto\/wp-json\/wp\/v2\/posts\/2861\/revisions\/2875"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/plaza.umin.ac.jp\/~OIO\/cornetto\/wp-json\/wp\/v2\/media\/2869"}],"wp:attachment":[{"href":"https:\/\/plaza.umin.ac.jp\/~OIO\/cornetto\/wp-json\/wp\/v2\/media?parent=2861"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/plaza.umin.ac.jp\/~OIO\/cornetto\/wp-json\/wp\/v2\/categories?post=2861"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/plaza.umin.ac.jp\/~OIO\/cornetto\/wp-json\/wp\/v2\/tags?post=2861"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}