|
自分の勉強した内容(プログラミング等)
に関するメモなどを綴る日記
× [PR]上記の広告は3ヶ月以上新規記事投稿のないブログに表示されています。新しい記事を書く事で広告が消えます。
まぁjavaといってもProcessingになるんですが
人間の髪の毛(というか黒い物体)にロックオンカーソル を合わせると反応するプログラム作ってみました まぁ課題のためなんですけどね でもまぁ出来が悪いなぁというのが自分の正直な感想です ちなみに以下のプログラム import processing.video.*; Capture img; PImage result; int label[]; int cnt = 0; int label_num; int L_BASE = 50; int timer=0; float thresh = 30.0; boolean lockflag; void setup(){ colorMode(RGB); size(480, 320); img = new Capture(this, width, height, 30); result = createImage(width, height, RGB); label = new int[width*height]; } void draw(){ if (img.available()) { img.read(); //二値化 binalize(img, label); //ラベリング処理 label_num = labeling(img, label, L_BASE); println(label_num); image(img,0,0); timer++; } rectMode(CENTER); stroke(0,0,255); line(width/4,0,width/4,height); line(width*3/4,0,width*3/4,height); if(label[width*mouseY+mouseX]>50) lockflag=true; else lockflag=false; PFont ft = loadFont("MS-PGothic-48.vlw"); textFont(ft,40); if(lockflag==true){ text("LOCK ON!",20,50); }else if(lockflag==false)text("LOST",20,50); noFill(); if(lockflag==true){ stroke(255,0,0); ellipse(mouseX,mouseY,40,40); }else if(lockflag==false){ stroke(0,0,255); } ellipse(mouseX,mouseY,20,20); ellipse(mouseX,mouseY,30,30); textFont(ft,15); stroke(0,0,255); text(""+mouseX+","+mouseY,mouseX+5,mouseY+5); line(mouseX,0,mouseX,result.width); line(0,mouseY,result.width,mouseY); if(mousePressed){ if(lockflag==true){ textFont(ft,40); fill(255,0,0); text("HIT!",200,200); ellipse(mouseX,mouseY,200,200); }else if(lockflag == false){ textFont(ft,40); fill(0,0,255); text("MISS!",200,200); } stroke(0,0,255); lockflag = false; } } //2値化 void binalize(PImage img_, int[] label_){ for(int j=0;j } else{ label_[img_.width*j+i] = 0; } } } } //ラベリング int labeling(PImage img_, int[] label_, int label_num){ for(int i=0;i if(label_num>=img_.width*img_.height){ println("ERROR!! TOO MANY LABELS."); } label_[i]=label_num; for(;;){ int cnt=0; for(int j=img_.width+1;j if(label_[j+img_.width]==1){ label_[j+img_.width]=label_num; cnt++;} if(label_[j+img_.width-1]==1){ label_[j+img_.width-1]=label_num; cnt++;} if(label_[j-1]==1){ label_[j-1]=label_num; cnt++;} if(label_[j-img_.width-1]==1){ label_[j-img_.width-1]=label_num; cnt++;} if(label_[j-img_.width]==1){ label_[j-img_.width]=label_num; cnt++;} if(label_[j-img_.width+1]==1){ label_[j-img_.width+1]=label_num; cnt++;} if(label_[j+1]==1){ label_[j+1]=label_num; cnt++;} if(label_[j+img_.width+1]==1){ label_[j+img_.width+1]=label_num; cnt++;} } } if(cnt==0) break; } label_num++; } } return label_num; } ちなみにこれ、ラベリングのところなどは先生が作ったソースのこぴぺなんですよね・・・ あぁ情けない・・・ PR
画像処理の授業で書いているprocessingのソース
画像を分割して、各部分ごとに異なる比率でコンボりゅーションをしようとしているのですが、今のところ上手くいってないんですよね 来週までにどうにかせねば・・・ import processing.video.*; int fream = 3; int offset = 0; Capture video; PImage [] img = new PImage[fream]; PImage result; float[][] c_matrix = {{0,-1,0}, {0,1,0}, {0,0,0}}; void setup(){ size(640,480); colorMode(RGB); video = new Capture(this,width,height,10); for(int i=0;i img[i] = createImage(width,height,RGB); } result = createImage(width,height,RGB); } void draw(){ if(video.available()){ offset++; if(offset == fream){ offset=0; } video.read(); result.loadPixels(); for (int y = 0; y < video.height; y++) { for (int x = 0; x < video.width; x++) { img[offset].pixels[y*video.width + x] = video.pixels[y*video.width + x]; } } for (int y = 0; y < video.height; y++) { for (int x = 0; x < video.width; x++) { float r_sum = 0.0,g_sum = 0.0,b_sum = 0.0; for(int w = 0; w< 3;w++){ for(int h = 0;h < 3;h++){ int tmp_x = x + h -1; int tmp_y = y + w -1; int tmp = img[offset].width * tmp_y + tmp_x; tmp = constrain(tmp,0,img[offset].pixels.length-1); r_sum += (red(img.pixels[tmp]) * c_matrix[w][h]); g_sum += (green(img.pixels[tmp]) * c_matrix[w][h]); b_sum += (blue(img.pixels[tmp]) * c_matrix[w][h]); } } r_sum = constrain(r_sum,0,255); g_sum = constrain(g_sum,0,255); b_sum = constrain(b_sum,0,255); color c = color(255-r_sum,255-g_sum,255-b_sum); int tmp = x + y * img.width; result.pixels[tmp] = c; if((offset+1+y*frame_num/480) } } |
カレンダー
フリーエリア
最新トラックバック
プロフィール
HN:
矢部竜太
性別:
非公開
ブログ内検索
|