Last active
August 29, 2015 14:02
-
-
Save gucchan22/29062c0f4028682cab03 to your computer and use it in GitHub Desktop.
画像処理 中間発表課題
This file contains 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
import processing.net.*; | |
import java.util.*; | |
Client voice_client; | |
PImage target_img, edge_img; | |
PFont font; | |
void setup() { | |
voice_client = new Client(this, "127.0.0.1", 10500); | |
target_img = loadImage("sample.jpg"); | |
size(target_img.width * 2, target_img.height * 3); | |
edge_img = createImage(target_img.width, target_img.height, RGB); | |
font = createFont("uzura_font", 140); | |
background(0); | |
edge_img.loadPixels(); | |
for(int h = 0; h < target_img.height; h++) { | |
for(int w = 0; w < target_img.width; w++) { | |
color c = target_img.pixels[h * target_img.width + w]; | |
float mid = (red(c) + green(c) + blue(c)) / 3.0; | |
edge_img.pixels[h * target_img.width + w] = target_img.pixels[h * target_img.width + w]; | |
target_img.pixels[h * target_img.width + w] = color(mid, mid, mid); | |
} | |
} | |
edge_img.updatePixels(); | |
target_img.updatePixels(); | |
image(target_img, 0, 0); | |
float[][] laplacian_matrix = {{-1.0,-1.0,-1.0},{-1.0,7.2,-1.0},{-1.0,-1.0,-1.0}}; | |
for(int i = 0; i < 3; i++) | |
for(int j = 0; j < 3; j++) laplacian_matrix[i][j] = laplacian_matrix[i][j] / 9.0; | |
for(int h = 1; h < edge_img.height - 1; h++) { | |
for(int w = 1; w < edge_img.width - 1; w++) { | |
float filter_r = 0.0, filter_g = 0.0, filter_b = 0.0; | |
for(int k = -1; k < 2; k++) { | |
for(int j = -1; j < 2; j++) { | |
color edge_col = edge_img.pixels[(h + k) * edge_img.width + (w + j)]; | |
filter_r += (float)red(edge_col) * laplacian_matrix[k + 1][j + 1]; | |
filter_g += (float)green(edge_col) * laplacian_matrix[k + 1][j + 1]; | |
filter_b += (float)blue(edge_col) * laplacian_matrix[k + 1][j + 1]; | |
} | |
} | |
float filter_mid = (filter_r + filter_g + filter_b) / 3.0; | |
edge_img.pixels[h * edge_img.width + w] = filter_mid > 90 ? color(0xff,0xff,0xff) : color(0x00,0x00,0x00); | |
} | |
} | |
image(edge_img, edge_img.width, 0); | |
} | |
HashMap<String,Integer> analyze_font(ArrayList<String> audio_al) { | |
HashMap<String, Integer> res_table = new HashMap<String,Integer>(); | |
for(int i = 0; i < audio_al.size(); i++) { | |
PGraphics pg = createGraphics(128, 128); | |
pg.beginDraw(); | |
pg.background(0); | |
pg.textFont(font); | |
pg.textSize(130.0f); | |
pg.text(audio_al.get(i), 15,100); | |
pg.endDraw(); | |
PImage tmp_img = pg.get(); | |
pg.dispose(); | |
tmp_img.save("data/tmp/tmp_" + i + ".jpg"); | |
} | |
for(int i = 0; i < audio_al.size(); i++) { | |
int count = 0; | |
PImage load_tmp_img = loadImage("data/tmp/tmp_" + i + ".jpg"); | |
for(int th = 0; th < load_tmp_img.height; th++) { | |
for(int tw = 0; tw < load_tmp_img.width; tw++) { | |
color tmp_col = load_tmp_img.pixels[th * load_tmp_img.width + tw]; | |
if((red(tmp_col) + green(tmp_col) + blue(tmp_col)) > ((0xff * 3) / 2)) count += 1; | |
} | |
} | |
res_table.put(audio_al.get(i), new Integer((int)((count / pow(128,2)) * 100))); | |
} | |
return res_table; | |
} | |
ArrayList<String> getStringFromAudioInterface() { | |
ArrayList<String> al = new ArrayList<String>(); | |
if(voice_client.available() > 0) { | |
String[] splited = split(voice_client.readString(), "WORD"); | |
for(int i = 0; i < splited.length; i++) { | |
String[] str_list = split(splited[i], "*"); | |
al.add(str_list[1]); | |
} | |
} | |
return al; | |
} | |
void draw() { | |
ArrayList<String> sounds = getStringFromAudioInterface(); | |
HashMap<String,Integer> analyze_res = analyze_font(sounds); | |
/* | |
print("Debug\n"); | |
for(String k : analyze_res.keySet()) { | |
print(k + " => " + analyze_res.get(k) + "% | "); | |
} | |
*/ | |
String[][] text_map = new String[target_img.height][target_img.width]; | |
ArrayList<Integer> sort_tmp = new ArrayList<Integer>(); | |
ArrayList<ArrayList<String>> sorted = new ArrayList<ArrayList<String>>(); | |
for(String dk : analyze_res.keySet()) sort_tmp.add(analyze_res.get(dk)); | |
Collections.sort(sort_tmp); | |
for(int i = 0; i < sort_tmp.size(); i++) { | |
for(String k : analyze_res.keySet()) { | |
if(analyze_res.get(k) == sort_tmp.get(i)) { | |
ArrayList<String> tmp_li = new ArrayList<String>(); | |
tmp_li.add(String.valueOf(i)); | |
tmp_li.add(k); | |
tmp_li.add(String.valueOf(analyze_res.get(k))); | |
sorted.add(tmp_li); | |
} | |
} | |
} | |
/* | |
print("Debug:\n"); | |
for(int i = 0; i < sorted.size(); i++) { | |
print("[" + sorted.get(i).get(0) + "," + sorted.get(i).get(1) + "," + sorted.get(i).get(2) + "]"); | |
} | |
print("\n\n"); | |
*/ | |
float[] pixel_ratio = new float[analyze_res.size()]; | |
for(int i = 0; i < analyze_res.size(); i++) pixel_ratio[i] = 255 * (1.0 / (i + 6.0)); | |
/* | |
print("Debug:\n"); | |
for(int i = 0; i < pixel_ratio.length; i++) print(pixel_ratio[i] + " | "); | |
print("\n\n"); | |
*/ | |
for(int h = 0; h < target_img.height; h++) { | |
for(int w = 0; w < target_img.width; w++) { | |
if(edge_img.pixels[h * edge_img.width + w] == color(0xff,0xff,0xff)) { | |
text_map[h][w] = sorted.get(0).get(1); | |
} else if(text_map[h][w] == null) { | |
float min_v = 0; | |
int min_idx = 0; | |
color sample = target_img.pixels[h * edge_img.width + w]; | |
for(int i = 1; i < pixel_ratio.length - 1; i++) { | |
min_v = Math.min(Math.abs(red(sample) - pixel_ratio[0]),Math.abs(red(sample) - pixel_ratio[i])); | |
} | |
for(int i = 1; i < pixel_ratio.length - 1; i++) if(Math.abs(pixel_ratio[i] - red(sample)) == min_v) min_idx = i; | |
int rnd = int(random(1,2)); | |
text_map[h][w] = sorted.get(min_idx + rnd).get(1); | |
} | |
} | |
} | |
String output_txt = ""; | |
for(int h = 0; h < target_img.height; h++) { | |
for(int w = 0; w < target_img.width; w++) { | |
output_txt += " " + text_map[h][w] + " "; | |
} | |
output_txt += "\n"; | |
} | |
textSize(2); | |
text(output_txt, 0,0); | |
} |
This file contains 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
cd dictation-kit-v4.3.1-osx && ./configure && make | |
sudo make install | |
./bin/julius -C main.jconf -C am-gmm.jconf -module 10500 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment