www.pudn.com > pornfilter.zip > pornfilter.cpp, change:2007-07-24,size:35492b


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <math.h>
#include <float.h>
#include <limits.h>
#include <time.h>
#include <ctype.h>
#include <fstream>
#include "cv.h"
#include "highgui.h"

/* use clock() function insted of time() */
#define time( arg ) (((double) clock()) / CLOCKS_PER_SEC)

//-------------------------------------------------------------------------------------
static CvMemStorage* storage = 0;
static CvHaarClassifierCascade* cascade = 0;
static CvMemStorage* storageface = 0;
static CvHaarClassifierCascade* cascadeface = 0;

static CvMemStorage* storage1 = 0;
static CvHaarClassifierCascade* cascade1 = 0;
static CvMemStorage* storage2 = 0;
static CvHaarClassifierCascade* cascade2 = 0;
static CvMemStorage* storage3 = 0;
static CvHaarClassifierCascade* cascade3 = 0;
static CvMemStorage* storage4 = 0;
static CvHaarClassifierCascade* cascade4 = 0;

double detect_and_draw( IplImage* image,int ind);

const char* cascade_namebreast =
    "/home/c4krud/Desktop/programPA/haarcascades/cascadebreast1981Pos_25Stage.xml";
const char* cascade_nameface =
    "/home/c4krud/Desktop/programPA/haarcascades/haarcascade_frontalface_alt_tree.xml";

const char* cascade_1 =
    "/home/c4krud/Desktop/programPA/haarcascades/cascadehand.xml";
const char* cascade_2 =
    "/home/c4krud/Desktop/programPA/haarcascades/cascadepenet01.xml";
const char* cascade_3 =
    "/home/c4krud/Desktop/programPA/haarcascades/cascadepussy.xml";
const char* cascade_4 =
    "/home/c4krud/Desktop/programPA/haarcascades/cascadeanal1.xml";

//-----------------------------------------

double getScore (IplImage *img){

//=================================================================
// Mr Bakr Alsharif second winner 
// Inovation Award (PHPclass) August 2006
	
//**How does it work ?
//**===================
//**The idea of this method is to scan the image pixel by pixel
//**and test if the color of each pixel is between a human skin
//**color range, then it returns the percent of found pixels to
//**the number of pixels in the picture.
//**Before scanning we divide the picture into shapes;

//**    ---------------------------------
//**    |               1               |
//**    |   -------------------------   |
//**    |   |       |       |       |   |
//**    |   |   ----|       |----   |   |
//**    |   |   |   |       |   |   |   |
//**    |   |   |   |       |   |   |   |
//**    |   |   |   |       |   |   |   |
//**    | 1 | 2 | 3 |   4   | 3 | 2 | 1 |
//**    |   |   |   |       |   |   |   |
//**    |   |   |   |       |   |   |   |
//**    |   |   |   |       |   |   |   |
//**    |   |   |   |       |   |   |   |
//**    |   |   |   |       |   |   |   |
//**    |   --------|       |--------   |
//**    |           |       |           |
//**    ---------------------------------

//**As long as you go into the middle of the picture, the color score
//**will be higher.
//**The idea behind this calculation; that almost all human pictures
//**are centered and focusing on the person who is in the picture.
//=========================================================================
	int i,j;
	double score=0;

	for (i=0;i<img->width;i++)
		for (j=0;j<img->height;j++){
			if(((uchar*)(img->imageData + img->widthStep*j))[i*3]!=0 && ((uchar*)(img->imageData + img->widthStep*j))[i*3+1]!=0 && ((uchar*)(img->imageData + img->widthStep*j))[i*3+2]!=0)
			{
				if( i>=(img->width/8 + img->width/4) && j>=(img->height/4) && i<=(img->width - (img->width/8 + img->width/4)) && j<=img->height )
				{
					score+=3;
				}
				else if( i<=img->width/8 || i>=(img->width - img->width/8) || j<=img->height/8 || j>=(img->height - img->height/8) )
				{
					score+=0.10;
				}
				else if( i<=img->width/4 || i>=(img->width - img->width/4) || j<=img->height/4 || j>=(img->height - img->height/8) )
				{
					score+=0.40;
				}
				else
				{
					score+=1.50;
				}
			}
		}
	score=score*100/(img->width*img->height);
	if(score>100) score=100;

	return score;
}

unsigned char maxRGB(unsigned char red,unsigned char green,unsigned char blue){
	unsigned char maks=0;
	if(red>=green && red>=blue)
		maks=red;
	else if(green>=blue)
		maks=green;
	else
		maks=blue;

	return maks;
}

unsigned char minRGB(unsigned char red,unsigned char green,unsigned char blue){
	unsigned char min=0;
	if(red<=green && red<=blue)
		min=red;
	else if(green<=blue)
		min=green;
	else
		min=blue;

	return min;
}



double skinColorDetect (IplImage *img,char *filename) {
	int i,j,k;
	double t = (double)cvGetTickCount();
	unsigned char red,green,blue,minrgb,maxrgb;
	double basis;
    	double Rc;
    	double Gc;
    	double Bc;
	double score, jr1,jr2,jr3,jr4,jr5,jr6,jr7;
	double jarak1,jarak2,jarak3,jarak4,jarak5,jarak6,jarak7,jarak8,dekat,ttljrk=0.0,rata2jrk=0.0;
	
	for (i=0;i<img->width;i++)
		for (j=0;j<img->height;j++) {
			// Get RGB value
			blue=((uchar*)(img->imageData + img->widthStep*j))[i*3];
			green=((uchar*)(img->imageData + img->widthStep*j))[i*3+1];
			red=((uchar*)(img->imageData + img->widthStep*j))[i*3+2];
			/*	
			//-------------------------------------------------------------//
		   	// *Non-adaptive skin color classifier:                        //
		   	// *M. Wimmer, B. Radig, Adaptive Skin Color Classificator,    //
		    	//*GVIP 05 Conference, 19-21 December 2005, CICC, Cairo, Egypt //
			//-------------------------------------------------------------//
		    	basis = red+green+blue;
            		Rc = red / basis;
            		Gc = green / basis;
            		Bc = blue / basis;
			
            		if(Rc>0.35 && Rc<5 && Gc>0.2 && Gc<0.7 && basis>200)
            		{
				//Set white value
				//((uchar*)(img->imageData + img->widthStep*j))[i*3]=255;
				//((uchar*)(img->imageData + img->widthStep*j))[i*3+1]=255;
				//((uchar*)(img->imageData + img->widthStep*j))[i*3+2]=255;
			}
            		else
            		{
				//Set black value
                		((uchar*)(img->imageData + img->widthStep*j))[i*3]=0;
				((uchar*)(img->imageData + img->widthStep*j))[i*3+1]=0;
				((uchar*)(img->imageData + img->widthStep*j))[i*3+2]=0;
            		}*/
			//=========================================================
			//nilai centroid
			//   222.4170  162.4908  135.5461    2.4502 
			//   182.7663  147.9558  133.0358    3.8295 
			//   252.3019  251.0121  250.4771    3.4623 
			//   204.4063  130.8821  100.1726    1.9747 
			//   236.5702  220.5669  213.9223    3.1636 
			//   149.2732  112.6145   99.3186    4.9751 
			//   217.5385  189.2097  177.3213    3.5050
			//---------------
			//  188.6291  157.0669  144.7189 (157+113+165+248+132+201)/6=169 
			//  149.6810  113.1501   99.9194 (144+99+139+247+102+191)/6=153 
			//  223.8361  165.7625  139.4314 
			//  251.0897  248.6413  247.6374 
			//  201.2857  132.3263  102.8959 
			//  225.3899  201.6247  191.5199 

			//=======================================================
			basis = red+green+blue;
            		Rc = red / basis;
            		Gc = green / basis;
            		Bc = blue / basis;
/*
//			jarak1 = sqrt( ((red-188)*(red-188)) + ((green-157)*(green-157)) + ((blue-144)*(blue-144)) );489
			jarak1 = sqrt( ((Rc-0.384458078)*(Rc-0.384458078)) + ((Gc-0.321063395)*(Gc-0.321063395)) + ((Bc-0.294478528)*(Bc-0.294478528)) );
			//jarak1 = exp(-(jarak1))*(4*3.14*3.14);
			dekat = jarak1;
			k=1;jarak1 = sqrt( ((Rc-0.405396474)*(Rc-0.405396474)) + ((Gc-0.325460599)*(Gc-0.325460599)) + ((Bc-0.269142927)*(Bc-0.269142927)) );
			//jarak1 = exp(-(jarak1))*(4*3.14*3.14);
			dekat = jarak1;
			k=1;
jarak2 = sqrt( ((Rc-0.371039925)*(Rc-0.371039925)) + ((Gc-0.326312688)*(Gc-0.326312688)) + ((Bc-0.302647388)*(Bc-0.302647388)) );
			//jarak2 = exp(-(jarak2))*(4*3.14*3.14);
			if(dekat > jarak2) {
				dekat = jarak2;
				k=2;
			}
jarak3 = sqrt( ((Rc-0.333883744)*(Rc-0.333883744)) + ((Gc-0.333114374)*(Gc-0.333114374)) + ((Bc-0.333001882)*(Bc-0.333001882)) );
			//jarak3 = exp(-(jarak3))*(4*3.14*3.14);
			if(dekat > jarak3) {
				dekat = jarak3;
				k=3;
			}
jarak4 = sqrt( ((Rc-0.417606452)*(Rc-0.417606452)) + ((Gc-0.326878749)*(Gc-0.326878749)) + ((Bc-0.255514798)*(Bc-0.255514798)) );
			//jarak4 = exp(-(jarak4))*(4*3.14*3.14);
			if(dekat > jarak4) {
				dekat = jarak4;
				k=4;
			}
jarak5 = sqrt( ((Rc-0.444889694)*(Rc-0.444889694)) + ((Gc-0.324491661)*(Gc-0.324491661)) + ((Bc-0.230618645)*(Bc-0.230618645)) );
			//jarak5 = exp(-(jarak5))*(4*3.14*3.14);
			if(dekat > jarak5) {
				dekat = jarak5;
				k=5;
			}
jarak6 = sqrt( ((Rc-0.389843336)*(Rc-0.389843336)) + ((Gc-0.32800205)*(Gc-0.32800205)) + ((Bc-0.282154614)*(Bc-0.282154614)) );
			//jarak6 = exp(-(jarak6))*(4*3.14*3.14);
			if(dekat > jarak6) {
				dekat = jarak6;
				k=6;
			}
jarak7 = sqrt( ((Rc-0.547831219)*(Rc-0.547831219)) + ((Gc-0.311247637)*(Gc-0.311247637)) + ((Bc-0.140921144)*(Bc-0.140921144)) );
			//jarak7 = exp(-(jarak7))*(4*3.14*3.14);
			if(dekat > jarak7) {
				dekat = jarak7;
				k=7;
			}
jarak8 = sqrt( ((Rc-0.503735395)*(Rc-0.503735395)) + ((Gc-0.324719069)*(Gc-0.324719069)) + ((Bc-0.171545535)*(Bc-0.171545535)) );
			//jarak8 = exp(-(jarak8))*(4*3.14*3.14);
			if(dekat > jarak8) {
				dekat = jarak8;
				k=8;
			}
			//jarak2 = sqrt( ((red-149)*(red-149)) + ((green-113)*(green-113)) + ((blue-99)*(blue-99)) );361
			jarak2 = sqrt( ((Rc-0.412742382)*(Rc-0.412742382)) + ((Gc-0.313019391)*(Gc-0.313019391)) + ((Bc-0.274238227)*(Bc-0.274238227)) );
			//jarak2 = exp(-(jarak2))*(4*3.14*3.14);
			if(dekat > jarak2) {
				dekat = jarak2;
				k=2;
			}
			//jarak3 = sqrt( ((red-223)*(red-223)) + ((green-165)*(green-165)) + ((blue-139)*(blue-139)) );527
			jarak3 = sqrt( ((Rc-0.423149905)*(Rc-0.423149905)) + ((Gc-0.313092979)*(Gc-0.313092979)) + ((Bc-0.263757116)*(Bc-0.263757116)) );
			//jarak3 = exp(-(jarak3))*(4*3.14*3.14);
			if(dekat > jarak3) {
				dekat = jarak3;
				k=3;
			}
			//jarak4 =sqrt( ((red-251)*(red-251)) + ((green-248)*(green-248)) + ((blue-247)*(blue-247)) );746
			jarak4 =sqrt( ((Rc-0.336461126)*(Rc-0.336461126)) + ((Gc-0.332439678)*(Gc-0.332439678)) + ((Bc-0.331099196)*(Bc-0.331099196)) );
			//jarak4 = exp(-(jarak4))*(4*3.14*3.14);
			if(dekat > jarak4) {
				dekat = jarak4;
				k=4;
			}
			//jarak5 =sqrt( ((red-201)*(red-201)) + ((green-132)*(green-132)) + ((blue-102)*(blue-102)) );435
			jarak5 =sqrt( ((Rc-0.462068966)*(Rc-0.462068966)) + ((Gc-0.303448276)*(Gc-0.303448276)) + ((Bc-0.234482759)*(Bc-0.234482759)) );
			//jarak5 = exp(-(jarak5))*(4*3.14*3.14);
			if(dekat > jarak5) {
				dekat = jarak5;
				k=5;
			}
			//jarak6 =sqrt( ((red-225)*(red-225)) + ((green-201)*(green-201)) + ((blue-191)*(blue-191)) );617
			jarak6 =sqrt( ((Rc-0.364667747)*(Rc-0.364667747)) + ((Gc-0.325769854)*(Gc-0.325769854)) + ((Bc-0.309562399)*(Bc-0.309562399)) );
			//jarak6 = exp(-(jarak6))*(4*3.14*3.14);
			if(dekat > jarak6) {
				dekat = jarak6;
				k=6;
			}
			//jarak7 =sqrt( ((red-217)*(red-217)) + ((green-189)*(green-189)) + ((blue-177)*(blue-177)) );583
			jarak7 =sqrt( ((Rc-0.403345725)*(Rc-0.403345725)) + ((Gc-0.324185249)*(Gc-0.324185249)) + ((Bc-0.303602058)*(Bc-0.303602058)) );
			//jarak7 = exp(-(jarak7))*(4*3.14*3.14);
			if(dekat > jarak7) {
				dekat = jarak7;
				k=7;
			}*/
			/*if(dekat > 33.1) {
				printf( "jarak dekat = %d\n", dekat);
			}*/

/*jarak1 = sqrt( ((Rc-0.388556684)*(Rc-0.388556684)) + ((Gc-0.328120849)*(Gc-0.328120849)) + ((Bc-0.283322466)*(Bc-0.283322466)) );
			jarak1 = exp(-(jarak1))*(4*3.14*3.14);
			dekat = jarak1;*/
/*
jarak1 = sqrt( ((Rc-0.386841728)*(Rc-0.386841728)) + ((Gc-0.326272107)*(Gc-0.326272107)) + ((Bc-0.286886164)*(Bc-0.286886164)) );
			//jarak1 = exp(-(jarak1))*(4*3.14*3.14);
			dekat = jarak1;
			k=1;
jarak2 = sqrt( ((Rc-0.451730434)*(Rc-0.451730434)) + ((Gc-0.32625019)*(Gc-0.32625019)) + ((Bc-0.222019376)*(Bc-0.222019376)) );
			//jarak2 = exp(-(jarak2))*(4*3.14*3.14);
			if(dekat > jarak2) {
				dekat = jarak2;
				k=2;
			}
jarak3 = sqrt( ((Rc-0.41413298)*(Rc-0.41413298)) + ((Gc-0.325549478)*(Gc-0.325549478)) + ((Bc-0.260317542)*(Bc-0.260317542)) );
			//jarak3 = exp(-(jarak3))*(4*3.14*3.14);
			if(dekat > jarak3) {
				dekat = jarak3;
				k=3;
			}
jarak4 = sqrt( ((Rc-0.533394775)*(Rc-0.533394775)) + ((Gc-0.317751513)*(Gc-0.317751513)) + ((Bc-0.148853712)*(Bc-0.148853712)) );
			//jarak4 = exp(-(jarak4))*(4*3.14*3.14);
			if(dekat > jarak4) {
				dekat = jarak4;
				k=4;
			}
jarak5 = sqrt( ((Rc-0.334369947)*(Rc-0.334369947)) + ((Gc-0.33295698)*(Gc-0.33295698)) + ((Bc-0.332673074)*(Bc-0.332673074)) );
			//jarak5 = exp(-(jarak5))*(4*3.14*3.14);
			if(dekat > jarak5) {
				dekat = jarak5;
				k=5;
			}
			if(dekat>0.04 || red>254 || green>249 || blue>248) {
				//Set black value
              		        ((uchar*)(img->imageData + img->widthStep*j))[i*3]=0;
				((uchar*)(img->imageData + img->widthStep*j))[i*3+1]=0;
				((uchar*)(img->imageData + img->widthStep*j))[i*3+2]=0;
    			}
			*/
			//======================================================
			//**PEER, P., KOVAC, J., AND SOLINA, F. 2003.        *//
			//** Human skin colour clustering                    *//
			//**for face detection. In submitted to EUROCON 2003 *//
			//**International Conference on Computer as a Tool.  *//
			//r > 95 AND g > 40 AND b > 10 AND                   *//
			//max{r,g,b} - min{r,g,b} > 15 AND                   *//
			//|r-g| > 15 AND r > g AND r > b                     *//
			//======================================================
						
			minrgb=minRGB(red,green,blue);
			maxrgb=maxRGB(red,green,blue);
			if( (red>95 && green>40 && blue>20 && (maxrgb-minrgb)>15 && abs(red-green)>15 && red>green && red>blue && green>blue && red<(green+150) && red<(blue+150) && green<(blue+150) ) /*|| (red>220 && green>210 && blue>170 && abs(red-green)<=15 && red>blue && green>blue)*/ )
			{
				//Set white value
				//((uchar*)(img->imageData + img->widthStep*j))[i*3]=255;
				//((uchar*)(img->imageData + img->widthStep*j))[i*3+1]=255;
				//((uchar*)(img->imageData + img->widthStep*j))[i*3+2]=255;
			}
            		else
            		{
				//Set black value
              		        ((uchar*)(img->imageData + img->widthStep*j))[i*3]=0;
				((uchar*)(img->imageData + img->widthStep*j))[i*3+1]=0;
				((uchar*)(img->imageData + img->widthStep*j))[i*3+2]=0;
            		}
		}
		
		score = getScore(img);
	
		
		t = (double)cvGetTickCount() - t;

		//printf( "detection time = %gms\n", t/((double)cvGetTickFrequency()*1000.) );

		/*for(i=0;i<50;i++){
		cvNamedWindow(filename, 1 );
		cvShowImage(filename, img);
		cvWaitKey(5); 
		}
		cvDestroyWindow(filename);
		cvReleaseImage( &img);*/
	
		return score;
	
}


double skinTextureDetermine (IplImage *image, int xshift, int yshift, double* coMatrix, char *filename) {
	
	int i,j,x,y,total=0;
	unsigned char gray1,gray2;
	CvPoint ipt;	
   	int color_tab; 
	double score;
	double t = (double)cvGetTickCount();
	int width=image->width, height=image->height;
	IplImage *img=cvCreateImage( cvSize( width, height ), 8, 1 );
	IplImage *hasil=cvCreateImage( cvSize( 256, 256 ), 8, 1 );
	IplImage *inputA=cvCreateImage( cvSize( 256, 256 ), 8, 1 );
	IplImage *inputB=cvCreateImage( cvSize( 256, 256 ), 8, 1 );

	IplImage *img1=cvCreateImage( cvSize( 256, 256 ), 8, 3 ); 
	cvZero( img1 ); 

	cvCvtColor(image, img, CV_BGR2GRAY);

	for (i=0;i<256;i++)
		for (j=0;j<256;j++)
			coMatrix[i*256+j]=0;
			
	for (i=0;i<width;i++) {	
		for (j=0;j<height;j++){	
			if(!((j+xshift >= width) || (j+xshift < 0) || (i+yshift >=height) || (i+yshift < 0))) {
				gray1=((uchar*)(img->imageData + img->widthStep*j))[i];	
				gray2=((uchar*)(img->imageData + img->widthStep*(j+yshift)))[(i+xshift)];
				coMatrix[(gray1)*256+(gray2)]++;
				total++;
			}		
		}
	}	

	
	//coccurence matrix form
 /*   for (i=0;i<256;i++){
		for (j=0;j<256;j++){
			if(coMatrix[i*256+j]!=0){
				if(coMatrix[i*256+j]>=20)
					color_tab=1;
				else if(coMatrix[i*256+j]<20 && coMatrix[i*256+j]>10)
					color_tab=2;
				else color_tab=3;
          		ipt.x = i;
          		ipt.y = j;
				if(color_tab==1)
          			cvCircle( img1, ipt, 2, CV_RGB(0,255,0), CV_FILLED ,8,0);
				else if(color_tab==2)
          		    cvCircle( img1, ipt, 2, CV_RGB(0,0,255), CV_FILLED ,8,0);
				else
          		    cvCircle( img1, ipt, 2, CV_RGB(255,0,0), CV_FILLED ,8,0);
        	 }	
		}
	 }	

	cvRectangle(img1,cvPoint(120,120),cvPoint(240,240),CV_RGB(255,0,255),1,8,0);*/

	//cooc matrix form
	
	for (i=0;i<width;i++) {	
		for (j=0;j<height;j++){
	 		if(!((j+xshift >= width) || (j+xshift < 0) || (i+yshift >=height) || (i+yshift < 0))) {
				gray1=((uchar*)(img->imageData + img->widthStep*j))[i];	
				gray2=((uchar*)(img->imageData + img->widthStep*(j+yshift)))[(i+xshift)];
			}
			for (x=120;x<240;x++){
				for (y=120;y<240;y++){
					if(x==gray1 && y==gray2)
					{
						if(coMatrix[x*256+y]!=0){
	          				((uchar*)(image->imageData + image->widthStep*j))[i*3]=255;
							((uchar*)(image->imageData + image->widthStep*j))[i*3+1]=255;
							((uchar*)(image->imageData + image->widthStep*j))[i*3+2]=255;			
	        				coMatrix[x*256+y]--;     
						}
					}		
				}
			}		
		}
	}

/*	for (i=0;i<256;i++){
		for (j=0;j<256;j++){
		//	printf("%lg ",coMatrix[i*256+j]);
			coMatrix[i*256+j]=(double)coMatrix[i*256+j]/(double)total;
		}
	//printf("=====\n");
	}*/
	
	for (i=0;i<width;i++) {	
		for (j=0;j<height;j++){
			if(((uchar*)(image->imageData + image->widthStep*j))[i*3]!=255 &&
				((uchar*)(image->imageData + image->widthStep*j))[i*3+1]!=255 &&
				((uchar*)(image->imageData + image->widthStep*j))[i*3+2]!=255){
				((uchar*)(image->imageData + image->widthStep*j))[i*3]=0;
				((uchar*)(image->imageData + image->widthStep*j))[i*3+1]=0;
				((uchar*)(image->imageData + image->widthStep*j))[i*3+2]=0;	
			}	
		 }
	}

	score = getScore(image);
	//cvCvtColor(image, inputA, CV_BGR2GRAY);
	//cvCvtColor(imgSR, inputB, CV_BGR2GRAY);

	//cvAnd( inputA, inputB, hasil, 0 );
	//cvErode(hasil,hasil,NULL,1);
    //cvDilate(hasil,hasil,NULL,1);
	//cvvSaveImage ("hasil.jpg", hasil);


	//cvNamedWindow("skin detection(physical)", 1 );
	//cvNamedWindow("human skin", 1 );
	//cvShowImage("skin detection(physical)", imgSR);
	//cvShowImage("human skin", hasil);
        //cvWaitKey(0); 
      
	//cvNamedWindow( "Co-occurence matrix", 1 );
	//cvShowImage( "Co-occurence matrix", img1);
	for(i=0;i<50;i++){
		cvNamedWindow( filename, 1 );
		cvShowImage( filename, image);
		cvWaitKey(5); 
	}
	
	
	t = (double)cvGetTickCount() - t;
 //   printf( "detection time = %gms\n", t/((double)cvGetTickFrequency()*1000.) );

	//cvWaitKey(0); 
	//cvDestroyWindow(filename);
	cvReleaseImage(&image);//cvReleaseImage( &img1);
	//cvDestroyWindow("skin detection(physical)" );
 	//cvReleaseImage( &imgSR );cvReleaseImage( &hasil );
	return score;
}
	
int main(int argc, char* argv[] )
{	
	 
	IplImage *imgColor, *imgColRe;
	
	//--------------------------------
	double totaltime;
	int hits=0, missed=0;
    	int totalHits=0, totalMissed=0;
	char filename[1000];
	double score;
	char* argumen=NULL;
	//--------------------------------
	// variables for object detection
	IplImage *imgsmall,*imgorig;
	const char* input_name;
	cascade = (CvHaarClassifierCascade*)cvLoad( cascade_namebreast, 0, 0, 0 );
	storage = cvCreateMemStorage(0);
	cascadeface = (CvHaarClassifierCascade*)cvLoad( cascade_nameface, 0, 0, 0 );
	storageface = cvCreateMemStorage(0);
cvClearMemStorage( storageface );
	cascade1= (CvHaarClassifierCascade*)cvLoad( cascade_1, 0, 0, 0 );
	storage1 = cvCreateMemStorage(0);
	cascade2 = (CvHaarClassifierCascade*)cvLoad( cascade_2, 0, 0, 0 );
	storage2 = cvCreateMemStorage(0);
	cascade3 = (CvHaarClassifierCascade*)cvLoad( cascade_3, 0, 0, 0 );
	storage3 = cvCreateMemStorage(0);
	cascade4 = (CvHaarClassifierCascade*)cvLoad( cascade_4, 0, 0, 0 );
	storage4 = cvCreateMemStorage(0);
	//----------------------------------------------------------------------------
    	if(argc!=4)
		printf("command: ./pornfilter <-object -image>|-video>\n");
	else if(argc==4){
//object detection ( includes : face, breast )
	   if(!strcmp(argv[1],"-object")){
	     if(!strcmp(argv[2],"-image")){
		cvNamedWindow("result", 1 );
	

		printf( "+=================================================+========+========+========+\n" );
		printf( "|            File Name                            | score  |  Hits  | Missed |\n" );
         	printf( "+=================================================+========+========+========+\n" );
		for(int i=0;i!=-1;i++){
			imgColor=0;
		//sprintf(filename, "/home/c4krud/vista/Users/NEC/Videos/boke'/outputExtract/AA_%d.bmp",i);
		//sprintf(filename, argv[3]);
		//sprintf(filename, "/home/c4krud/Desktop/negatif/neg%d.bmp",i);
		//sprintf(filename, "/home/c4krud/Desktop/programPA/negative/neg%d.jpg",i);
	
     		sprintf(filename, "/home/c4krud/Desktop/testing1/test (%d).bmp",i);
		//sprintf(filename, "/home/c4krud/Desktop/training/train%d.bmp",i);
			imgColor = cvLoadImage( filename,1 );
			if(imgColor==0)continue;
	
	
			imgorig=cvCloneImage(imgColor);
			/*if(imgorig->width<300 || imgorig->height<300)
			{
				imgsmall=cvCreateImage(cvSize(imgorig->width*2,imgorig->height*2),IPL_DEPTH_8U,3);
				//imgsmall=cvCreateImage(cvSize(imgorig->width/4,imgorig->height/4),IPL_DEPTH_8U,3);
				cvResize(imgorig,imgsmall,CV_INTER_LINEAR);
				if(imgColor)
				{
						
					score=detect_and_draw(imgsmall,i);
					cvReleaseImage(&imgsmall);
				}
			}*/
			/*else if(imgorig->width>600 || imgorig->height>600)
			{
				imgsmall=cvCreateImage(cvSize(imgorig->width*2,imgorig->height*2),IPL_DEPTH_8U,3);
				imgsmall=cvCreateImage(cvSize(imgorig->width/2,imgorig->height/2),IPL_DEPTH_8U,3);
				cvResize(imgorig,imgsmall,CV_INTER_LINEAR);
				if(imgColor)
				{
						
					score=detect_and_draw(imgsmall,i);
					cvReleaseImage(&imgsmall);
				}
			} */
               
			//else if( imgColor )
        		//{
            			score=detect_and_draw( imgorig,i);
           			// cvWaitKey(0);
            			cvReleaseImage( &imgorig );
       			//}
        	if(score>=45){hits=1;missed=0;}
			 else {missed=1;hits=0;}

		
			 totalHits+=hits;
			 totalMissed+=missed;
		printf( "|%s|%lg |%6d  |%6d  |\n", filename, score, hits, missed );
             	printf( "+-------------------------------------------------+--------+--------+--------+\n" );
			
                fflush( stdout );
      		}
		printf( "|%s|        |%6d  |%6d  |\n", "Total",totalHits, totalMissed);
    		printf( "+=================================================+========+========+========+\n" );

    	   }
    
//	cvDestroyWindow("result");
//===================================================================
//=========================================================================================
//video detect
	  if(!strcmp(argv[2],"-video")){	
	   CvCapture* capture = 0;
    	   IplImage *frame, *frame_copy = 0;
    	
           capture = cvCaptureFromAVI(argv[3]); 

    	   cvNamedWindow( "videoBF", 1 );


    	   if( capture )
    	   {
			
        	  for(;;)
        	  {
			//for(int z=0;z<10;z++)
           	        if( !cvGrabFrame( capture ))
                		break;
			
            			frame = cvRetrieveFrame( capture );
            		if( !frame )
              		        break;
            		if( !frame_copy )
                		frame_copy = cvCreateImage( cvSize(frame->width,frame->height),
                                            IPL_DEPTH_8U, frame->nChannels );
            		if( frame->origin == IPL_ORIGIN_TL )
                		cvCopy( frame, frame_copy, 0 );
            		else
                		cvFlip( frame, frame_copy, 0 );
            
            		score=detect_and_draw( frame_copy,-1 );

            		if( cvWaitKey( 10 ) >= 0 )
                		break;
       		 }

        	cvReleaseImage( &frame_copy );
        	cvReleaseCapture( &capture );
    	   }
    	  }
	  }
   //     cvDestroyWindow("videoBF");
//=========================================================================================	
	}

	return 0;
}


double detect_and_draw( IplImage* img ,int ind)
{
	int donedetect=0;
    static CvScalar colors[] = 
    {
        {{0,0,255}},
        {{0,128,255}},
        {{0,255,255}},
        {{0,255,0}},
        {{255,128,0}},
        {{255,255,0}},
        {{255,0,0}},
        {{255,0,255}},
	{{0,0,0}}
    };
	
    char filename[1000];
    double scale = 1;
	double totaltime=0;
	double score=0;
    IplImage* gray = cvCreateImage( cvSize(img->width,img->height), 8, 1 );
    IplImage* small_img = cvCreateImage( cvSize( cvRound (img->width/scale),
                         cvRound (img->height/scale)),
                     8, 1 );
		IplImage* imgclone = cvCreateImage( cvSize(img->width,img->height), 8, 3); 
     imgclone=cvCloneImage(img);     
    int i;

    //cvCvtColor( img, gray, CV_BGR2GRAY );
    //cvResize( gray, small_img, CV_INTER_LINEAR );
   // cvEqualizeHist( small_img, small_img );
    cvClearMemStorage( storage );
    cvClearMemStorage( storageface );

    cvClearMemStorage( storage1 );
    cvClearMemStorage( storage2 );
    cvClearMemStorage( storage3 );
    cvClearMemStorage( storage4 );

    if( cascade )
    {
        double t = (double)cvGetTickCount();
        //CvSeq* breast = cvHaarDetectObjects( small_img, cascade, storage,
                       //                     1.1, 2, CV_HAAR_DO_CANNY_PRUNING,
                         //                 cvSize(0, 0 ));
		CvSeq* breast = cvHaarDetectObjects( img, cascade, storage,
                                           1.2, 2);
        t = (double)cvGetTickCount() - t;
        printf( "detection time = %gms\n", t/((double)cvGetTickFrequency()*1000.) );
        for( i = 0; i < (breast ? breast->total : 0); i++ )
        {
            CvRect* r = (CvRect*)cvGetSeqElem( breast, i );
            CvPoint center;
            int radius;
            center.x = cvRound((r->x + r->width*0.5)*scale);
            center.y = cvRound((r->y + r->height*0.5)*scale);
            radius = cvRound((r->width + r->height)*0.3*scale);
            cvCircle( img, center, radius, colors[0], 3, 8, 0 );

            //CvPoint pt1, pt2;
            // Find the dimensions of the face,and scale it if necessary
            //pt1.x = r->x*scale;
            //pt2.x = (r->x+r->width)*scale;
            //pt1.y = r->y*scale;
            //pt2.y = (r->y+r->height)*scale;
            // Draw the rectangle on the image
            //cvRectangle( img, pt1, pt2, colors[i%8], 3, 8, 0 );
	}
		totaltime+=t;
    }

	
	/*if( cascadeface )
    {
        double t = (double)cvGetTickCount();
        CvSeq* face = cvHaarDetectObjects( small_img, cascadeface, storageface,
                                            1.2, 2 );
	//	CvSeq* face1 = cvHaarDetectObjects( small_img, cascade, storage,
       //                                    1.2, 1 );
        t = (double)cvGetTickCount() - t;
        printf( "detection time = %gms\n", t/((double)cvGetTickFrequency()*1000.) );
        for( i = 0; i < (face ? face->total : 0); i++ )
        {
            CvRect* r = (CvRect*)cvGetSeqElem( face, i );
            CvPoint center;
            int radius;
            center.x = cvRound((r->x + r->width*0.5)*scale);
            center.y = cvRound((r->y + r->height*0.5)*scale);
            radius = cvRound((r->width + r->height)*0.5*scale);
            cvCircle( img, center, radius, colors[3], 3, 8, 0 );
        }
		totaltime+=t;
    }*/



	//------------
	/*if( cascade1 )
    {
        double t = (double)cvGetTickCount();
        CvSeq* satu = cvHaarDetectObjects( img, cascade1, storage1,
                                            1.2, 2 );
	
        t = (double)cvGetTickCount() - t;
        printf( "detection time = %gms\n", t/((double)cvGetTickFrequency()*1000.) );
        for( i = 0; i < (satu ? satu->total : 0); i++ )
        {
            CvRect* r = (CvRect*)cvGetSeqElem( satu, i );
            CvPoint center;
            int radius;
            center.x = cvRound((r->x + r->width*0.5)*scale);
            center.y = cvRound((r->y + r->height*0.5)*scale);
            radius = cvRound((r->width + r->height)*0.3*scale);
            cvCircle( img, center, radius, colors[2], 3, 8, 0 );
        }
		totaltime+=t;
    }*/

	/*if( cascade2 )
    {
        double t = (double)cvGetTickCount();
        CvSeq* dua = cvHaarDetectObjects( small_img, cascade2, storage2,
                                            1.2, 2 );
	                               
        t = (double)cvGetTickCount() - t;
        printf( "detection time = %gms\n", t/((double)cvGetTickFrequency()*1000.) );
        for( i = 0; i < (dua ? dua->total : 0); i++ )
        {
            CvRect* r = (CvRect*)cvGetSeqElem( dua, i );
            CvPoint center;
            int radius;
            center.x = cvRound((r->x + r->width*0.5)*scale);
            center.y = cvRound((r->y + r->height*0.5)*scale);
            radius = cvRound((r->width + r->height)*0.5*scale);
            cvCircle( img, center, radius, colors[2], 3, 8, 0 );
        }
		totaltime+=t;
    }*/

	/*if( cascade3)
    {
        double t = (double)cvGetTickCount();
        CvSeq* tiga = cvHaarDetectObjects( img, cascade3, storage3,
                                            1.2, 2 );
	                                 
        t = (double)cvGetTickCount() - t;
        printf( "detection time = %gms\n", t/((double)cvGetTickFrequency()*1000.) );
        for( i = 0; i < (tiga ? tiga->total : 0); i++ )
        {
            CvRect* r = (CvRect*)cvGetSeqElem( tiga, i );
            CvPoint center;
            int radius;
            center.x = cvRound((r->x + r->width*0.5)*scale);
            center.y = cvRound((r->y + r->height*0.5)*scale);
            radius = cvRound((r->width + r->height)*0.3*scale);
            cvCircle( img, center, radius, colors[1], 3, 8, 0 );
        }
		totaltime+=t;
    }*/

/*	if( cascade4 )
    {
        double t = (double)cvGetTickCount();
        CvSeq* empat= cvHaarDetectObjects( small_img, cascade4, storage4,
                                            1.2, 2 );
	
        t = (double)cvGetTickCount() - t;
        printf( "detection time = %gms\n", t/((double)cvGetTickFrequency()*1000.) );
        for( i = 0; i < (empat ? empat->total : 0); i++ )
        {
            CvRect* r = (CvRect*)cvGetSeqElem( empat, i );
            CvPoint center;
            int radius;
            center.x = cvRound((r->x + r->width*0.5)*scale);
            center.y = cvRound((r->y + r->height*0.5)*scale);
            radius = cvRound((r->width + r->height)*0.5*scale);
            cvCircle( img, center, radius, colors[5], 3, 8, 0 );
        }
		totaltime+=t;
    }*/
//-------------------------------------
	score=skinColorDetect(img,"haha");

//============================
/*if( cascadetbreastdeface )
    {
        double t = (double)cvGetTickCount();
        CvSeq* face = cvHaarDetectObjects( small_img, cascadeface, storageface,
                                            1.2, 2  );
	  for( i = 0; i < (face ? face->total : 0); i++ )
        {
            CvRect* r = (CvRect*)cvGetSeqElem( face, i );
            CvPoint center;
            int radius;
            center.x = cvRound((r->x + r->width*0.5)*scale);
            center.y = cvRound((r->y + r->height*0.5)*scale);
            radius = cvRound((r->width + r->height)*0.5*scale);
             if(((uchar*)(img->imageData + img->widthStep*center.y))[center.x*3]!=0
		&& ((uchar*)(img->imageData + img->widthStep*center.y))[center.x*3+1]!=0
		&& ((uchar*)(img->imageData + img->widthStep*center.y))[center.x*3+2]!=0)
            	cvCircle( img, center, radius, colors[8], -1, 8, 0 );
	    else
		cvCircle( img, center, radius, colors[8], -1, 8, 0 );
        }
		
    }*///green
score = getScore(img);
if( cascade )
    {
        double t = (double)cvGetTickCount();
        CvSeq* breast =  cvHaarDetectObjects( imgclone, cascade, storage,
                                           1.2, 2);
      

	for( i = 0; i < (breast ? breast->total : 0); i++ )
        {
            CvRect* r = (CvRect*)cvGetSeqElem( breast, i );
            CvPoint center;
            int radius;
            center.x = cvRound((r->x + r->width*0.5)*scale);
            center.y = cvRound((r->y + r->height*0.5)*scale);
            radius = cvRound((r->width + r->height)*0.3*scale);
		
	    if(((uchar*)(img->imageData + img->widthStep*center.y))[center.x*3]!=0
		&& ((uchar*)(img->imageData + img->widthStep*center.y))[center.x*3+1]!=0
		&& ((uchar*)(img->imageData + img->widthStep*center.y))[center.x*3+2]!=0){
            	cvCircle( img, center, radius, colors[0], 3, 8, 0 );
		donedetect=1;
		}
	    //else
		//cvCircle( img, center, radius, colors[6], 3, 8, 0 );
	}
    }
	
	
/*if( cascade3)
    {
        double t = (double)cvGetTickCount();
        CvSeq* tiga = cvHaarDetectObjects( imgclone, cascade3, storage3,
                                            1.2, 2 );
	                                 
        t = (double)cvGetTickCount() - t;
        printf( "detection time = %gms\n", t/((double)cvGetTickFrequency()*1000.) );
        for( i = 0; i < (tiga ? tiga->total : 0); i++ )
        {
            CvRect* r = (CvRect*)cvGetSeqElem( tiga, i );
            CvPoint center;
            int radius;
            center.x = cvRound((r->x + r->width*0.5)*scale);
            center.y = cvRound((r->y + r->height*0.5)*scale);
            radius = cvRound((r->width + r->height)*0.3*scale);
            if(((uchar*)(img->imageData + img->widthStep*r->y))[center.x*3]!=0
		&& ((uchar*)(img->imageData + img->widthStep*r->y))[center.x*3+1]!=0
		&& ((uchar*)(img->imageData + img->widthStep*r->y))[center.x*3+2]!=0){
            	cvCircle( img, center, radius, colors[1], 3, 8, 0 );
		donedetect=1;
	   }		
	    //else
		//cvCircle( img, center, radius, colors[6], 3, 8, 0 );
        }
		totaltime+=t;
    }*/

/*if( cascade1 )
    {
        double t = (double)cvGetTickCount();
        CvSeq* satu = cvHaarDetectObjects( imgclone, cascade1, storage1,
                                            1.2, 2 );
	
        t = (double)cvGetTickCount() - t;
        printf( "detection time = %gms\n", t/((double)cvGetTickFrequency()*1000.) );
        for( i = 0; i < (satu ? satu->total : 0); i++ )
        {
            CvRect* r = (CvRect*)cvGetSeqElem( satu, i );
            CvPoint center;
            int radius;
            center.x = cvRound((r->x + r->width*0.5)*scale);
            center.y = cvRound((r->y + r->height*0.5)*scale);
            radius = cvRound((r->width + r->height)*0.3*scale);
            if(((uchar*)(img->imageData + img->widthStep*center.y))[center.x*3]!=0
		&& ((uchar*)(img->imageData + img->widthStep*center.y))[center.x*3+1]!=0
		&& ((uchar*)(img->imageData + img->widthStep*center.y))[center.x*3+2]!=0)
            	cvCircle( img, center, radius, colors[2], 3, 8, 0 );
        }
		totaltime+=t;
    }*/




//============================

	printf( "total detection time = %gms\n", totaltime/((double)cvGetTickFrequency()*1000.) );
	if(ind>=0)	
		for(i=0;i<100;i++){
	
			cvShowImage( "result", img );
			cvWaitKey(5); 
	
		}
       
	else
        	cvShowImage( "videoBF", img );

	/*if(donedetect==1){ 
 
	sprintf(filename, "/home/c4krud/Desktop/pornnegh/det%d.jpg",ind); 
	cvSaveImage(filename,img); 
		}else{ 
		sprintf(filename, "/home/c4krud/Desktop/nonpornnegh/nondet%d.jpg",ind); 
	cvSaveImage(filename,img); 
		}*/
	//sprintf(filename, "/home/c4krud/Desktop/detskin/cokMethod_%d.jpg",ind);
	//cvvSaveImage(filename,img);
        cvReleaseImage( &gray );
        cvReleaseImage( &small_img );

	return score;
}