田村研の画像処理ライブラリを利用したサンプルプログラムを掲載します.あらかじめリンク先に示してある通りにライブラリを適切な場所にダウンロードしておく必要があります.
画像ファイルを読み込んで,処理結果を書き出すプログラムの雛型です.色を反転するだけの単純な例を示します.
赤字の部分が田村研ライブラリが提供するキーワードです.青部分は実行時間を計測する部分,緑部分が画像を画面表示するための部分です.必要なければ,青と緑の部分は削除してください.
次のプログラムをMyImageProcessing.javaとして保存
//MyImageProcessing.java
//色を反転するプログラム例
import jp.ac.nit.tamura_lab.ImageProcessing.*; //田村研ライブラリのインポート
public class MyImageProcessing{
public static void main(String [] args){
/*入力画像の読み込みと結果画像の領域確保*/
CIELabImage inImage = new CIELabImage("test.jpg"); //入力画像の読み込み
CIELabImage outImage = new CIELabImage(inImage); //inImageと同じサイズの空画像
long start = System.currentTimeMillis(); //画像処理開始時間記録
/*画像処理の例*/
IPL.copyImage(inImage, outImage); //コピーして
outImage.inverseColor(); //色反転
long stop = System.currentTimeMillis(); //画像処理終了時間記録
System.out.println("実行にかかった時間は " + (stop - start) + " ミリ秒です。");
/*結果の保存*/
outImage.writeImage("png","output.png"); //処理結果をファイルに保存
/* 入力画像の表示 */
ImageFrame frame1 = new ImageFrame(inImage,2.5f); //2.5倍サイズで表示
frame1.setTitle("入力画像"); //ウィンドウのタイトルバーの表示
frame1.setVisible(true); //画面に表示
frame1.setExitFlag(true); //ウィンドウが閉じられたら終了
/* 出力画像の表示 */
ImageFrame frame2 = new ImageFrame(outImage,2.5f); //2.5倍サイズで表示
frame2.setTitle("出力画像"); //ウィンドウのタイトルバーの表示
frame2.setLocationByPlatform(true); //前のウィンドウとちょっと位置をずらす
frame2.setVisible(true); //画面に表示
frame2.setExitFlag(true); //ウィンドウが閉じられたら終了
}
カメラから動画を取り込んで処理する雛型です.動画といっても,一枚一枚カメラから静止画を取得して,連続して処理することになります.このため,カメラから画像を取得する部分と画像処理部分をforループで繰り返し実行するだけで,基本的な画像処理手続きは静止画処理と変わりません.
最初のカメラを起動する部分で使用しているVideoDeviceクラスや,VideoDevice2クラスはハードウェアに依存する部分です.現在はLinuxで使用するための専用クラスとなっています.ネットワークカメラ用のNetCamはWindowsからでも利用可能です.
このサンプルプログラムを実行するには,田村研ライブラリとカメラ用ライブラリとインタフェースクラスを適切な場所にダウンロードしておく必要があります.
次のプログラムをCamera.javaとして保存
import jp.ac.nit.tamura_lab.ImageProcessing.*;
/**
* 動画処理のサンプルグラム
*
* @author tamura
*/
public class Camera {
public static void main(final String[] args) {
/* カメラ画像の解像度指定 */
final int width = 320; //幅
final int height = 240; //高さ
/* カメラ起動(次の3つのカメラのうちどれか1つだけ選択すること) */
/* v4l対応のUSBカメラ(QV-4000,QVP-61,VX-1000) */
//VideoDevice camera = new VideoDevice("/dev/video0",width, height);
/* v4l2対応のUSBカメラ用(QV-4000,QVR-13) */
//VideoDevice2 camera = new VideoDevice2("/dev/video0",width, height,172,32,32,32,1);
/* ネットカメラ用 */
NetCam camera = new NetCam("air.tamura-lab.nit.ac.jp", width, height, "Standard");
new Thread(camera).start(); //選択したカメラを起動
/* 表示ウィンドウ初期化 */
ImageFrame frame0 = new ImageFrame(width,height,2.5f); //2.5倍で表示
frame0.setTitle("カメラ画像"); //ウィンドウのタイトルバーの表示
frame0.setVisible(true); //画面に表示
ImageFrame frame1 = new ImageFrame(width,height,2.5f); //2.5倍で表示
frame1.setTitle("出力画像"); //ウィンドウのタイトルバーの表示
frame1.setLocationByPlatform(true); //前のウィンドウとちょっと位置をずらす
frame1.setVisible(true); //画面に表示
/* 使用する画像の領域確保 */
RGBImage inImage = new RGBImage(width, height); //カメラの入力画像領域
RGBImage outImage = new RGBImage(inImage); //結果画像の格納場所
long start = System.currentTimeMillis(); //開始時間記録
/* カメラ処理のループ */
long i;
for(i = 0;; i++) {
camera.getPixelsSynchronized(inImage); //カメラから画像を一枚取得
frame0.repaint(inImage); //表示ウィンドウ0番に入力表示
/* 画像処理部分 (目的に応じてinImageを処理してoutImageへ格納すること) */
IPL.copyImage(inImage, outImage); //とりあえずコピー
outImage.inverseColor(); //色を反転
frame1.repaint(outImage); //表示ウィンドウ1番に結果表示
if(frame0.isClose()||frame1.isClose()) //表示ウィンドウが閉じられたら終了
break;
}
/*終了時の処理*/
long stop = System.currentTimeMillis(); //終了時間記録
System.out.println("処理速度は " + (float)i/(stop - start)*1000f + "枚/秒でした");
System.out.println("画像を保存して終了します");
inImage.writeImage("png", "input.png"); //最後の入力画像の書き込み
outImage.writeImage("png", "output.png"); //最後の結果画像の書き込み
System.exit(0); //終了(cameraスレッドも停止)
}
}
ステレオカメラから動画を取り込んで処理する雛型です.通常の動画処理とほとんど同じですが,右カメラ画像と左カメラ画像の両方を同時に取得して処理する点だけが異なります.
このサンプルプログラムを実行するには,田村研ライブラリとカメラ用ライブラリとインタフェースクラスを適切な場所にダウンロードしておく必要があります.
次のプログラムをStereo.javaとして保存
import jp.ac.nit.tamura_lab.ImageProcessing.*;
/**
* ステレオカメラのサンプルグラム
*
* @author tamura
*/
public class Stereo {
public static void main(final String[] args) {
/* カメラ画像の解像度指定 */
final int width = 320;
final int height = 240;
/* カメラ起動 */
StereoVideo camera = new StereoVideo("test.ini",width, height,25,0);//test.iniは設定ファイル
//なくても起動可
new Thread(camera).start(); //ステレオカメラ起動
/* 表示ウィンドウ初期化 */
ImageFrame frame0 = new ImageFrame(width,height,2.5f); //2.5倍で表示
frame0.setTitle("右画像"); //ウィンドウのタイトルバーの表示
frame0.setVisible(true); //画面に表示
ImageFrame frame1 = new ImageFrame(width,height,2.5f); //2.5倍で表示
frame1.setTitle("左画像"); //ウィンドウのタイトルバーの表示
frame1.setLocationByPlatform(true); //前のウィンドウとちょっと位置をずらす
frame1.setVisible(true); //画面に表示
/* 使用する画像の領域の確保 */
RGBImage RImage = new RGBImage(width*2,height);
RGBImage LImage = new RGBImage(width*2,height);
long start = System.currentTimeMillis(); //開始時間記録
/* カメラ処理のループ */
long i;
for(i = 0;; i++) {
camera.getPixelsSynchronized(LImage,RImage); //カメラ画像を取得
/* 画像処理 */
frame0.repaint(RImage); //右表示
frame1.repaint(LImage); //左表示
if(frameR.isClose()||frameL.isClose()) //閉じられたら終了
break;
}
/*終了時の処理*/
long stop = System.currentTimeMillis(); //終了時間記録
System.out.println("処理速度は " + (float)i/(stop - start)*1000f + "枚/秒でした");
System.out.println("終了します");
System.exit(0); //終了(cameraスレッドも停止)
}
}