プログラムの説明は、IPFundamentals から確認してください。
By Cryst.
/*
Copyright (c) 2011, Cryst.
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted
provided that the following conditions are met:
* Redistributions of source code must
retain the above copyright notice, this list of
conditions and the
following disclaimer.
* Redistributions in binary form must
reproduce the above copyright notice, this list of
conditions and the
following disclaimer in the documentation and/or other materials
provided with the
distribution.
* Neither the name of the Cryst nor the
names of its contributors may be used to endorse or
promote products
derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
IS" AND ANY EXPRESS OR
IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY
AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
THE COPYRIGHT HOLDER OR
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER
IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
OTHERWISE) ARISING IN ANY WAY OUT
OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
DAMAGE.
*/
/*
Copyright (c) 2011, Cryst.
All rights reserved.
ソースコード形式かバイナリ形式か、変更するかしないかを問わず、以下の条件を満たす場合に
限り、再頒布および使用を許可します。
*
ソースコードを再頒布する場合、上記の著作権表示、本条件一覧、および 下記免責条項を
含めること。
*
バイナリ形式で再頒布する場合、頒布物に付属のドキュメント等の資料に、上記の著作権
表示、本条件一覧、および下記免責条項を含めること。
*
書面による特別の許可なしに、本ソフトウェアから派生した製品の宣伝または販売促進に、
Cryst の名前またはコントリビューターの名前を使用してはならない。
本ソフトウェアは、著作権者およびコントリビューターによって「現状のまま」提供されており、
明示黙示を問わず、商業的な使用可能性、および特定の目的に対する適合性に関する暗黙の保証
も含め、またそれに限定されない、いかなる保証もありません。著作権者もコントリビューター
も、事由のいかんを問わず、損害発生の原因いかんを問わず、かつ責任の根拠が契約であるか
厳格責任であるか(過失その他の)不法行為であるかを問わず、仮にそのような損害が発生する
可能性を知らされていたとしても、本ソフトウェアの使用によって発生した(代替品または代用
サービスの調達、使用の喪失、データの喪失、利益の喪失、業務の中断も含め、またそれに限定
されない)直接損害、間接損害、偶発的な損害、特別損害、懲罰的損害、または結果損害につい
て、一切責任を負わないものとします。
*/
////////////////////////////////////////////////////////////////////////////////
// 画像処理関数の定義部
// コンパイル方法の例:gcc -O3 -Wall -mno-cygwin -m486 -o labeling labeling.c
//
gcc -O3 -Wall -m486 -o labeling labeling.c
/** bmp ファイルを読み込み8連結のラベリング処理をするプログラム C 言語 ソースコード
使い方:
1、labeling BMP_file_name_in [BMP_file_name_out]
コマンドラインにBMPファイル名を指定して起動する。
起動時に、出力ファイル名が省略された場合は、
画像処理関数名に bmp の拡張子を付けた名前で保存される
*/
////////////////////////////////////////////////////////////////////////////////
#include <stdio.h>
#include <stdlib.h>
#include <string.h> //
for void *memset(void *DST, int C, size_t LENGTH);
#pragma pack(push,1)
typedef struct PIXEL { unsigned char B, G, R, A; } PIXEL ;
#pragma pack(pop)
#pragma pack(push,2)
typedef struct HEADER {
unsigned short
bfType; // 0x4D42 BMP identification 0x42='B',
0x4D='M'
unsigned int
bfSize; // ??
unsigned short bfReserved1; // 0
unsigned short bfReserved2; // 0
unsigned int
bfOffBits; //
54 sizeof(HEADER)
unsigned int
biSize;
// 40 sizeof(biSize ~ biClrImportant)
signed
int
biWidth;
// lx
signed
int
biHeight;
// ly
unsigned short
biPlanes;
// 1
unsigned short
biBitCount; // 32
unsigned int
biCompression; //
0
unsigned int
biSizeImage; // ??
signed
int biXPelsPerMeter; // 0
signed
int biYPelsPerMeter; // 0
unsigned int
biClrUsed; // 0
unsigned int
biClrImportant; //
0
} HEADER ;
#pragma pack(pop)
#define
BMPID 0x4D42
#define BMPIFHSIZE 40
void **alloc2D( void *data, int sizeOfUnit, int lx, int ly )
{ // 連続データを、配列データとしてアクセス可能にする
register void **d, *base=data ;
register int i, s=sizeOfUnit, llx=lx, lly=ly ;
if( (data==NULL) ||
(sizeOfUnit<1) || (lx<1) || (ly<1) ) return NULL ;
if( (d=(void **)malloc(ly*sizeof(void
*))) == NULL ) return NULL ;
for( i=0; i<lly; i++ ) d[i] =
(void *)((unsigned char *)base + llx*s*i) ;
return d ;
}
/////////////////////////////
新規追加共通関数 /////////////////////////////
int saveRawLabelConf( char *fname, int lx, int ly )
{/** ラベルデータの大きさを記録。BMPファイルのヘッダー形式の流用
このヘッダーと、ラベルデータをバイナリーデータとして結合させると
見かけ上は、BMPファイルと同じにフォーマットになる。
簡易的データの確認方法
copy /B labeling.cfg + labeling.lbl
labeling.bmp
*/
HEADER h = {BMPID, lx*abs(ly)*sizeof(PIXEL)+sizeof(HEADER), 0, 0,
sizeof(HEADER),
BMPIFHSIZE, lx, ly, 1, 32, 0, lx*abs(ly)*sizeof(PIXEL), 0, 0, 0, 0} ;
register FILE *fp ;
if( (fp=fopen(fname, "wb")) == NULL ) {
return -1;
} else if ( fwrite( &h,
sizeof(HEADER), 1, fp ) != 1 ) {
fclose(fp) ; return -1;
} else fclose(fp) ;
return 0;
}
int saveRawLabelData( char *fname, void *b, int lx, int ly )
{/** ラベルデータを1ラベルを4バイトの符号付整数として保存する。
座標情報は、擬似カラーBMP(32ビット形式)を参照する事。*/
register FILE *fp ;
if( (fp=fopen(fname, "wb")) == NULL ) {
return -1;
} else if ( fwrite( b,
lx*ly*sizeof(int), 1, fp ) != 1 ) {
fclose(fp) ; return -1;
} else fclose(fp) ;
return 0;
}
unsigned int int2PseudoColor(unsigned int v)
{/** ラベルデータを、擬似カラーデータに変換します。
この変換は可逆変換なので、
擬似カラーデータから、元のラベルデータを逆算して求める事が出来ます。 */
register unsigned int r=0, g=0, b=0 ;
if( (v&0x00ffffff) == 0 ) return
0 ;
if( v & 0x000001 ) b |= 0x80 ;
if( v & 0x000002 ) g |= 0x80 ;
if( v & 0x000004 ) r |= 0x80 ;
if( v & 0x000008 ) b |= 0x40 ;
if( v & 0x000010 ) g |= 0x40 ;
if( v & 0x000020 ) r |= 0x40 ;
if( v & 0x000040 ) b |= 0x20 ;
if( v & 0x000080 ) g |= 0x20 ;
if( v & 0x000100 ) r |= 0x20 ;
if( v & 0x000200 ) b |= 0x10 ;
if( v & 0x000400 ) g |= 0x10 ;
if( v & 0x000800 ) r |= 0x10 ;
if( v & 0x001000 ) b |= 0x08 ;
if( v & 0x002000 ) g |= 0x08 ;
if( v & 0x004000 ) r |= 0x08 ;
if( v & 0x008000 ) b |= 0x04 ;
if( v & 0x010000 ) g |= 0x04 ;
if( v & 0x020000 ) r |= 0x04 ;
if( v & 0x040000 ) b |= 0x02 ;
if( v & 0x080000 ) g |= 0x02 ;
if( v & 0x100000 ) r |= 0x02 ;
if( v & 0x200000 ) b |= 0x01 ;
if( v & 0x400000 ) g |= 0x01 ;
if( v & 0x800000 ) r |= 0x01 ;
return (r <<16) | (g
<< 8) | b ;
}
void inline displacement( int *lbl, int a, int b, int len )
{/**
同一のラベル番号を持つべき領域が異なるラベル番号を持つているので、小さい値に統一する */
register int *l=lbl, fair=a<b?a:b, misguided=a<b?b:a, i,
s=len ;
for( i=0; i<=s; i++ ) if(
l[i]==misguided ) l[i]=fair ;
}
#define IMAGEPROCESSING labeling // 利用手順 その1:関数名を定義する
int IMAGEPROCESSING(PIXEL *src, PIXEL *dst, int wlx, int wly)
{ /** 利用手順
その2:ここに画像処理用の関数を作成する。他の部分は変更せず流用
読み込んだ2値画像の連結性解析(ラベリング)を行い、結果と擬似カラー画像のの作成。
読み込んだ画像は、0を背景、0以外の値(数値自体は無視)は対象物と認識する
(カラー画像を入力しても、0とそれ以外の2値画像として処理する事に注意する事) */
#define ucr unsigned char
register int x, y, lx=wlx, ly=wly, i, l=lx*ly, label=1, st=1 ;
register int *s=(int *)src, *d=(int *)dst, llx=lx+2, lly=ly+2,
llxy=llx*lly ;
register ucr *img=(ucr *)malloc(llx*lly*sizeof(ucr))
;
//
作業用画像領域の確保 ((wlx+2)*(wly+2))画素分
処理フローの簡略化用
register int *wk =(int *)malloc(llx*lly*sizeof(int))
;
// ラベルデータ用領域の確保
((wlx+2)*(wly+2))画素分 作業用画像領域対応
register ucr **sa=(ucr **)alloc2D(img, sizeof(ucr), llx, lly) ; // 作業用画像領域を配列としてアクセス可能にする処理
register int **da=(int **)alloc2D( wk, sizeof(int), llx, lly) ; // ラベルデータ用領域を配列としてアクセス可能にする処理
if( (s==NULL) || (d==NULL) || (lx<1) || (ly<1) ||
(img==NULL) ||
(wk==NULL) || (sa==NULL) || (da==NULL) ) goto GOOD_BY ;
memset( img, 0, llxy*sizeof(ucr) )
; // 画像データエリアの初期化
本来の画素数(wlx*wly)より左右上下に1画素大きな領域
memset( wk, 0, llxy*sizeof(int) )
; //
labelingエリアの初期化 作業用領域の画素数(大きさ)は、((wlx+2)*(wly+2))
for( y=1, i=0; y<=ly; y++ ) for(
x=1; x<=lx; sa[y][x]=s[i++]==0?0:255, x++ ) ; // 画像を2値化し、作業用領域((wlx+2)*(wly+2))に格納
for( y=1; y<=ly; y++ ) {//
作業用領域中の2値画像を対象としたラベリング処理を行い結果を作業用のラベリングデータ領域に格納
for( x=1; x<=lx; x++ ) {//
8連結の記述のある行(4箇所)をコメントアウトす
れば、4連結のラベリング処理
if( sa[y][x]!=0 ) { //
対象物を構成する画素が見つかった
if(sa[y][x-1]!=0){
da[y][x] = da[ y ][x-1] ; //
4連結の連結性解析部
if(da[y-1][ x ]!=0
&& da[y][x]!=da[y-1][ x ]) displacement((int *)wk,
da[y][x], da[y-1][ x ], llx*y+x) ; // 4連結でのコンフリクト処理
if(da[y-1][x+1]!=0
&& da[y][x]!=da[y-1][x+1]) displacement((int *)wk,
da[y][x], da[y-1][x+1], llx*y+x) ; // 8連結でのコンフリクト処理
} else
if(sa[y-1][x]!=0){ da[y][x] = da[y-1][ x
] ; // 4連結の連結性解析部
} else
if(sa[y-1][x-1]!=0){ da[y][x] = da[y-1][x-1] ; // 8連結の連結性解析部
if(da[y-1][x+1]!=0
&& da[y][x]!=da[y-1][x+1]) displacement((int *)wk,
da[y][x], da[y-1][x+1], llx*y+x) ; // 8連結でのコンフリクト処理
} else
if(sa[y-1][x+1]!=0){ da[y][x] = da[y-1][x+1] ; // 8連結の連結性解析部
}
else
da[y][x] = label++ ;
// 新規ラベル番号の割り当て(処理済みのデータとは連結性を持たない)
}
}
}
for( y=1, i=0; y<=ly; y++ ) for(
x=1; x<=lx; d[i++]=da[y][x++] ) ; //
本来のバッファーにデータを格納(wlx*wly)
memset( wk, 0, label*sizeof(int) )
; // for( i=0;
i<label; wk[i++]=0 ) ; と同じ処理結果
for( i=0; i<l; wk[d[i++]]=1 )
;
// 使用されて有効なラベル番号の抽出
for( i=1, x=1, wk[0]=0; i<label;
i++ ) if( wk[i]!=0 ) wk[i] = x++ ; //
再番処理用のルックアップテーブルの作成
for( i=0; i<l; d[i]=wk[d[i]], i++
) ; //
虫食い状態のラベル番号を、連続したラベル番号に変換
#define
CFGFINALFNAMEIS(fname)
#fname
//
labeling.cfg -> "labeling.cfg"
#define CFGEXTRACTING(fname) CFGFINALFNAMEIS(fname.cfg) //
labeling
-> labeling.cfg
#define CFGNAMEIS(fname)
CFGEXTRACTING(fname)
// IMAGEPROCESSING
-> labeling
saveRawLabelConf(
CFGNAMEIS(IMAGEPROCESSING), lx, ly ) ; //
ラベルデータのサイズ情報を保存します。
#define
LBLFINALFNAMEIS(fname)
#fname
//
labeling.lbl -> "labeling.lbl"
#define LBLEXTRACTING(fname)
LBLFINALFNAMEIS(fname.lbl) //
labeling
-> labeling.lbl
#define LBLNAMEIS(fname)
LBLEXTRACTING(fname)
//
IMAGEPROCESSING -> labeling
saveRawLabelData(
LBLNAMEIS(IMAGEPROCESSING), d, lx, ly ) ; //
ラベルデータ(dst)を保存します。
for( i=0; i<l; i++ )
d[i]=int2PseudoColor(d[i]) ; // labelエリアのラベル番号を擬似カラー画像データに変換
st = 0 ;
GOOD_BY: free(da) ; free(sa) ; free(wk) ; free(img) ;
return st ;
} // 利用手順
その3:プログラムを保存し、コンパイル後、処理画像を指定して起動
////////////////////////////////////////////////////////////////////////////////
// 以下 上記関数を実際に利用する為に必要な処理の記載部
//
// 画像データ(カラーBMPファイル)のアクセスや保存処理を定義した共通部
// windowsの 24,23ビット無圧縮形式BMPファイル対応版
// 通常ヘッダーファイルは多重読み込みに対応しているので何度インクルドしても良い
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
int saveDataAsBMP32( char *fname, void *b, int lx, int ly )
{/** 処理結果を 32ビット形式の BMPファイルとして書き出す。
*/
HEADER h = {BMPID, lx*abs(ly)*sizeof(PIXEL)+sizeof(HEADER), 0, 0,
sizeof(HEADER),
BMPIFHSIZE, lx, ly, 1, 32, 0, lx*abs(ly)*sizeof(PIXEL), 0, 0, 0, 0} ;
FILE *fp ;
if( (fp=fopen(fname, "wb")) == NULL ) {
fprintf( stderr, "File '%s' cannot create.\n", fname );
return -1;
} else if ( fwrite( &h,
sizeof(HEADER), 1, fp ) != 1 ) {
fclose(fp) ; return -1;
} else if ( fwrite( b, h.biSizeImage, 1,
fp ) != 1 ) {
fclose(fp) ; return -1;
} else fclose(fp) ;
return 0;
}
void raw2pixel(void *raw, void *pix, int lenX, int lenY, int BitCount)
{
register char *src=(char *)raw,
*dst=(char *)pix, dummy ;
register int x, y, lx=lenX,
ly=lenY, byteParLine=(lx*(BitCount/8)+3)&(~3) ;
register int i, l=lx*ly,
*s=(int *)raw, *d=(int *)pix ;
if( BitCount!=24 && BitCount!=32 ) for( y=0;
y<ly; y++ )
for( x=0, dummy=0; x<lx; *d++=dummy++, x++ ) ;
else if( BitCount == 32 ) for( i=0;
i<l; *d++=*s++, i++ ) ; //
same as d[i]=s[i], i++
else {//
BitCount == 24
for( y=0; y<ly; y++ ) {
src = (char *)raw + byteParLine * y ;
for( x=0; x<lx; x++ ) {
*dst++ = *src++ ; // B
*dst++ = *src++ ; // G
*dst++ = *src++ ; // R
*dst++ = 0 ;
}
}
}
}
int main(int argc, char *argv[])
{
HEADER
h ;
struct stat fileInfo ;
register FILE *fp ;
register char *raw=NULL, *image=NULL,
*fname=NULL ;
register PIXEL *source=NULL, *destination=NULL ;
register int lx, ly, size, st=1
;
if ( argc != 2 && argc
!= 3 ) { // 画像データをメモリーに読み込む
fprintf( stderr, "usage: %s 32bit_format_bmp [output_BMP_name]\n",
argv[0] );
goto GOOD_BY ;
} else if ( (fp=fopen(argv[1],"rb")) ==
NULL ) {
fprintf( stderr, "'%s' open error.\n", argv[1]
);
goto GOOD_BY ;
} else if ( fstat(fileno(fp),
&fileInfo) != 0 ) {
fprintf( stderr, "'%s' stat error.\n", argv[1] ); fclose(fp)
; goto GOOD_BY ;
} else if( fileInfo.st_size <
sizeof(HEADER) ) {
fprintf( stderr, "'%s' file size erro. too small(%d Byte)\n", argv[1],
(int)fileInfo.st_size );
fclose(fp) ; goto GOOD_BY ;
} else if( (raw=(char
*)malloc(fileInfo.st_size)) == NULL ) {
fprintf( stderr, "Couldn't allocate memory.\n" ) ; fclose(fp) ; goto
GOOD_BY ;
} else if ( fread(raw, fileInfo.st_size,
1, fp) != 1 ) {
fprintf( stderr, "'%s' read error.\n", argv[1] ); fclose(fp)
; goto GOOD_BY ;
} else fclose(fp) ;
//
画像データの確認:(画像データの確認と、作業用環境の構築)
if( (h=*(HEADER *)raw).bfType!=BMPID ||
h.bfReserved1!=0 || h.bfReserved2!=0 ) {
fprintf( stderr, "%s' is not a BMP file.\n", argv[1] );
goto GOOD_BY ;
} else if( h.bfOffBits!=sizeof(HEADER) || h.biSize!=BMPIFHSIZE ||
h.biPlanes!=1 || (h.biBitCount!=24 && h.biBitCount!=32)
||
h.biCompression!=0 ) {
fprintf( stderr, "The BMP file, '%s', is not a 32(24)bit
non-compression format.\n", argv[1] );
goto GOOD_BY ;
} else if ( fileInfo.st_size<(h.biSizeImage+h.bfOffBits) ||
h.biWidth*abs(h.biHeight)*(h.biBitCount/8)>h.biSizeImage ) {
fprintf( stderr, "Bad BMP file format.\n" );
fprintf( stderr, "%d, %d, %d, %d, %d\n", (int)fileInfo.st_size,
h.biWidth, abs(h.biHeight), h.biWidth*abs(h.biHeight)*(h.biBitCount/8),
h.biSizeImage );
goto GOOD_BY ;
} else {
lx = h.biWidth ;
ly = abs(h.biHeight) ; //
座標系が 左上原点でも 左下原点でも処理に支障は無く、必要なのは大きさのみ
size = lx * ly * sizeof(PIXEL) ;
}
//
作業用環境の構築:(画像データの確認と、作業用環境の構築)
if( (destination=(PIXEL *)malloc(size))
== NULL ) {
fprintf( stderr, "Couldn't allocate memory. destination\n" ) ; goto
GOOD_BY ;
} else if( (source=(PIXEL
*)malloc(size)) == NULL ) {
fprintf( stderr, "Couldn't allocate memory. source\n" )
; goto GOOD_BY ;
} else { //
読み込んだ画像を処理バッファーに格納(PIXEL形式への変換を含む)等
#define
FINALFNAMEIS(foname)
#foname
// "MyFunction.bmp"
#define EXTRACTING(foname) FINALFNAMEIS(foname.bmp) // MyFunction.bmp
#define FNAMEIS(foname)
EXTRACTING(foname)
// IMAGEPROCESSING
-> MyFunction
fname = argc==2 ? FNAMEIS(IMAGEPROCESSING) : argv[2] ;
image = (char *)raw + sizeof(HEADER) ;
raw2pixel(image, source, lx, ly, h.biBitCount) ;
}
//
実際の画像処理と、処理結果の保存
st = IMAGEPROCESSING(source,
destination, lx, ly) ; //
ここで画像処理ルーチンを呼び出す
if( st == 0 ) saveDataAsBMP32( fname,
destination, lx, h.biHeight) ; //
biHeight (not ly)
else fprintf( stderr, "Image processing
is not completed.\n" ) ;
GOOD_BY: // 後始末とプログラムの終了
free(destination) ;
free(source) ;
free(raw) ;
return st ;
}