画像の二値化、腐食、操作、閉操作と開操作

5314 ワード

画像の二値化は主に最大分散アルゴリズム(大津アルゴリズム,otsuアルゴリズムとも呼ばれる)に用いられる.
腐食(erosion)と膨張(dilation)は関連資料を調べることができる.
開く操作は、まず画像を腐食してから膨張します.閉操作は正反対です.
以下のコードは24ビットbmp画像にのみ適用されます.
#include 
#include 
#include 
#include 
#include 
#include 
using namespace std;
typedef unsigned char BYTE;
typedef unsigned short WORD;
typedef unsigned int DWORD;

//       ;
typedef struct  tagBITMAPFILEHEADER{
//	WORD bfType;//    ,         
	DWORD bfSize;//    
	WORD bfReserved1;//   
	WORD bfReserved2;//   
	DWORD bfOffBits;//                 
}BITMAPFILEHEADER;

typedef struct tagBITMAPINFOHEADER{
	DWORD biSize;//     
	DWORD biWidth;//    
	DWORD biHeight;//    
	WORD biPlanes;//    ,   1
	WORD biBitCount;//     
	DWORD  biCompression; //    
	DWORD  biSizeImage; //         
	DWORD  biXPelsPerMeter; //     
	DWORD  biYPelsPerMeter; //     
	DWORD  biClrUsed; //          
	DWORD  biClrImportant; //          
}BITMAPINFOHEADER; //       

//    
typedef struct tagIMAGEDATA
{
	BYTE blue;
	BYTE green;
	BYTE red;
}DATA;


BITMAPFILEHEADER strHead;
BITMAPINFOHEADER strInfo;
WORD bfType;
int h,w,size;
vector > binarization;

void otsu(vector >& src);
void printImage(vector > src, string filename);
vector > erosion(vector > src);
vector > dilation(vector > src);

int main(){
	FILE *fpi;
	fpi=fopen("input.bmp","rb");
	if(fpi != NULL){
		//       
		fread(&bfType,1,sizeof(WORD),fpi);
		if(0x4d42!=bfType) {
			cout< > erosionData = erosion(binarization);
		printImage(erosionData,"erosion.bmp");

		vector > dilationData = dilation(binarization);
		printImage(dilationData,"dilation.bmp");

		vector > openingData = dilation(erosionData);
		printImage(openingData,"opening.bmp");

		vector > closingData = erosion(dilationData);
		printImage(closingData,"closing.bmp");


	}
	else
	{
		cout< > src, string filename){
	DATA* output = new DATA[size];
	for(int i=0; i >& srcData){
	int slideWindow = 4;	//let the sildewindow be 1/16 image size
	int winH=h/slideWindow, winW=w/slideWindow;
	int modified=70;
	float histogram[256];
	float avgValue=0;
	int threshold;  
	for(int n = 0; n < slideWindow; n++){
		for(int m = 0; m < slideWindow; m++){
			
			memset(histogram,0,sizeof(histogram)); 
			for(int i = n*winH; i<:min i="" for="" j="m*winW;" histogram="" avgvalue="0;" size="" threshold="0;" float="" maxvariance="0;" ww="0," u="0;" t="avgValue" variance="t" if=""> maxVariance)   
        		{    
            		maxVariance = variance;    
            		threshold = i;    
        		}    
    		}    
    		threshold-=modified;
    		for(int i = n*winH; i<:min i="" for="" j="m*winW;" cout="" if="">=threshold){
 						// cout< > erosion(vector > src){
	vector > modified(h+2);
	for(int i = 0; i < modified.size(); i++)
		modified[i].resize(w+2);
	for(int i = 0; i < modified.size();i++){
		if(i==0||i==modified.size()-1){
			for(int j = 0; j < modified[i].size(); j++)
				modified[i][j]=255;
		}
		else{
			for(int j = 0; j < modified[i].size();j++){
				if(j==0||j==modified[i].size()-1)
					modified[i][j]=255;
				else
					modified[i][j]=src[i-1][j-1];
			}
		}
	}

	vector > result(h);
	for(int i = 0; i < result.size(); i++)
		result[i].resize(w);
	for(int i = 0; i < h; i++){
		for(int j = 0; j < w; j++){
			int sum = modified[i][j]+modified[i][j+1]+modified[i][j+2]
			+modified[i+1][j]+modified[i+1][j+1]+modified[i+1][j+2]
			+modified[i+2][j]+modified[i+2][j+1]+modified[i+2][j+2];
			if(sum>0)
				result[i][j]=255;
			else
				result[i][j]=0;
		}
	}
	return result;
}

vector > dilation(vector > src){
	vector > modified(h+2);
	for(int i = 0; i < modified.size(); i++)
		modified[i].resize(w+2);
	for(int i = 0; i < modified.size();i++){
		if(i==0||i==modified.size()-1){
			for(int j = 0; j < modified[i].size(); j++)
				modified[i][j]=255;
		}
		else{
			for(int j = 0; j < modified[i].size();j++){
				if(j==0||j==modified[i].size()-1)
					modified[i][j]=255;
				else
					modified[i][j]=src[i-1][j-1];
			}
		}
	}

	vector > result(h);
	for(int i = 0; i < result.size(); i++)
		result[i].resize(w);
	for(int i = 0; i < h; i++){
		for(int j = 0; j < w; j++){
			int sum = modified[i][j]+modified[i][j+1]+modified[i][j+2]
			+modified[i+1][j]+modified[i+1][j+1]+modified[i+1][j+2]
			+modified[i+2][j]+modified[i+2][j+1]+modified[i+2][j+2];
			if(sum/9==255)
				result[i][j]=255;
			else
				result[i][j]=0;
		}
	}
	return result;
}