#include <stdio.h>
#include <cv.h>
#include <highgui.h>

void mythreshold(IplImage *src,IplImage *dst,int thresh){
	unsigned char *psrc , *pdst;
	int i,j;
	
	for(i = 0; i < src->height ; i++){ //
		// 画像の走査線1ライン(行)の先頭を一時変数へセーブ
		// psrcは元画像の行の先頭,pdstは保存先先の行の先頭
		psrc = (unsigned char *)src->imageData + i * src->widthStep;
		pdst = (unsigned char *)dst->imageData + i * dst->widthStep;
		for(j = 0; j< src->widthStep; j+=3){
			//blue
			pdst[j] = (psrc[j] > thresh)? 255: 0; 
			//green
			pdst[j+1] = (psrc[j+1] > thresh)? 255: 0;
			// red
			pdst[j+2] = (psrc[j+2] > thresh)? 255: 0;
		}
	}
}
int main(int argc,char *argv[]){
	IplImage *image = 0,*output;

	image =cvLoadImage("lena.jpg",1);
	
	output = cvCreateImage(cvGetSize(image) ,
			       image->depth,image->nChannels);
	//閾値処理
	mythreshold(image,output,150);
	
	//処理結果を表示
	cvNamedWindow("output",1);
	cvShowImage("output",output);
	cvWaitKey(0);

	return 0;
}