Ejemplo n.º 1
0
Rect haarPatternDetection(CascadeClassifier classifier, Mat image, int imageWidthforDetection, Rect parentRect=Rect()) {
    double imageSizeScale = 1.0*image.size().width*1.0/image.size().height;
    Mat color_img = image.clone();
    Mat face_img, gray_img;
    imresize(color_img, imageWidthforDetection,face_img);
    cvtColor(face_img, gray_img, CV_BGR2GRAY);
    equalizeHist(gray_img, gray_img);
    vector<Rect> faceRectsVector;
    classifier.detectMultiScale(gray_img, faceRectsVector,1.2,3,
                                CV_HAAR_FIND_BIGGEST_OBJECT | CV_HAAR_SCALE_IMAGE );
    
    Rect faceRect(0,0,0,0);
    
    for (int i = 0; i < faceRectsVector.size(); i++) {
        drawBox(face_img, faceRectsVector[i]);
        if (faceRect.area() < faceRectsVector[i].area())
            faceRect = faceRectsVector[i];
    }
    drawBox(face_img, faceRect);
    //    imshow("rect",face_img);
    double x = 1.0*faceRect.x/(double)imageWidthforDetection;
    double y = 1.0*faceRect.y/(double)cvRound(imageWidthforDetection/imageSizeScale);
    double w = 1.0*faceRect.width/(double)imageWidthforDetection;
    double h = 1.0*faceRect.height/(double)cvRound(imageWidthforDetection/imageSizeScale);
    
    int bx = cvRound(x*image.size().width) ;;//+ parentRect.tl().x;
    int by = cvRound(y*image.size().height) ;//+ parentRect.tl().y;
    int bw = cvRound(w*image.size().width);
    int bh = cvRound(h*image.size().height);
    
    Rect biggerRect(bx,by,bw,bh);
    biggerRect += parentRect.tl();
    
    return biggerRect;
}
Ejemplo n.º 2
0
void Slic::build_pyramid(const vector<unsigned char>& img, int levels) {
#ifdef SLIC_DEBUG
	unsigned long t1, t2;
	t1 = now_us();
	printf("Slic::build_pyramid ");
#endif    
	// build the pyramid for the image data
	pyramid = vector<vector<unsigned char> >(levels);
	int n = rows[0] * cols[0] * chan;
	pyramid[0] = vector<unsigned char>(n);
	copy(img.begin(), img.begin() + n, pyramid[0].begin());

	for (int i = 1; i < levels; ++i) {
		imresize(pyramid[i], pyramid[i - 1], rows[i - 1], cols[i - 1], rows[i], cols[i]);
	}
	// build the pyramid for assignments:
	assignments_pyramid = vector<vector<int> >(levels);
	for (int i = 0; i < levels; ++i) {
		assignments_pyramid[i] = vector<int>(rows[i] * cols[i], 0);
	}
#ifdef SLIC_DEBUG
	t2 = now_us();
	printf("elapsed: %f\n", (t2 - t1) / 1000.0);
#endif
}
Ejemplo n.º 3
0
void MyProcessingClass::ResizeImage()
{
    /*
     * *
     * function [newIm,zoom]=ResizeImage(Im,oldSizeOfROI,newSizeOfROI)

    zoom = newSizeOfROI/oldSizeOfROI;
    [height,width,deep] = size(Im);

    newIm = imresize(Im,[height*zoom width*zoom]);
end
     */
    float zoom = m_new_size_of_roi / m_old_size_of_roi;

    size(m_input_image, m_image_size, m_image_depth);
    m_input_image = imresize(m_input_image,Sizei(m_input_image.size().height * zoom, m_input_image.size().width * zoom));
}
Ejemplo n.º 4
0
void findEyeCenterByColorSegmentation(const Mat& image, Point2f & eyeCoord, float coordinateWeight, int kmeansIterations, int kmeansRepeats, int blurSize)  {
    
    Mat img, gray_img;
    Mat colorpoints, kmeansPoints;
    
    img = equalizeImage(image);
    
    medianBlur(img, img, blurSize);
    cvtColor(image, gray_img, CV_BGR2GRAY);
    gray_img = imcomplement(gray_img);
    vector<Mat> layers(3);
    split(img, layers);
    for (int i = 0 ; i < layers.size(); i++) {
        layers[i] = layers[i].reshape(1,1).t();
    }
    hconcat(layers, colorpoints);
    
    // add coordinates
    colorpoints.convertTo(colorpoints, CV_32FC1);
    Mat coordinates = matrixPointCoordinates(img.rows,img.cols,false) *coordinateWeight;
    hconcat(colorpoints, coordinates, kmeansPoints);
    
    Mat locIndex(img.size().area(),kmeansIterations,CV_32FC1,Scalar::all(-1));
    linspace(0, img.size().area(), 1).copyTo(locIndex.col(0));
    Mat index_img(img.rows,img.cols,CV_32FC1,Scalar::all(0));
    Mat bestLabels, centers, clustered , colorsum , minColorPtIndex;
    for(int it = 1 ; it < kmeansIterations ; it++) {
        if (kmeansPoints.rows < 2) {
            break;
        }
        kmeans(kmeansPoints,2,bestLabels,TermCriteria(CV_TERMCRIT_EPS+CV_TERMCRIT_ITER, kmeansRepeats, 0.001),kmeansRepeats,KMEANS_PP_CENTERS,centers);
        reduce(centers.colRange(0, 3), colorsum, 1, CV_REDUCE_SUM);
        
        if (colorsum.at<float>(0) < colorsum.at<float>(1)) {
            
            findNonZero(bestLabels==0, minColorPtIndex);
        }
        else {
            findNonZero(bestLabels==1, minColorPtIndex);
        }
        
        minColorPtIndex = minColorPtIndex.reshape(1).col(1);
        
        for (int  i = 0; i <minColorPtIndex.rows ; i ++) {
            locIndex.at<float>(i,it) = locIndex.at<float>(minColorPtIndex.at<int>(i),it-1);
        }
        Mat temp;
        for (int  i = 0; i <minColorPtIndex.rows ; i ++) {
            temp.push_back(kmeansPoints.row(minColorPtIndex.at<int>(i)));
        }
        temp.copyTo(kmeansPoints);
        temp.release();
        for (int i = 0 ; i < minColorPtIndex.rows ; i ++) {
            int r, c;
            ind2sub(locIndex.at<float>(i,it), index_img.cols, index_img.rows, r, c);
            index_img.at<float>(r,c) +=1;
        }
    }
    //    imagesc("layered",mat2gray(index_img));
    Mat layerweighted_img = index_img.mul(index_img);
    layerweighted_img = mat2gray(layerweighted_img);
    gray_img.convertTo(gray_img, CV_32FC1,1/255.0);
    Mat composed  = gray_img.mul(layerweighted_img);
    float zoomRatio = 5.0f;
    Mat zoomed;
    imresize(composed, zoomRatio, zoomed);
    Mat score = calculateImageSymmetryScore(zoomed);
//    imagesc("score", score);
    Mat scoresum;
    reduce(score.rowRange(0, zoomed.cols/6), scoresum, 0, CV_REDUCE_SUM,CV_32FC1);
//    plotVectors("scoresum", scoresum.t());
    double minVal , maxVal;
    Point minLoc, maxLoc;
    minMaxLoc(scoresum,&minVal,&maxVal,&minLoc,&maxLoc);
    float initialHC = (float)maxLoc.x/zoomRatio;
    line(zoomed, Point(maxLoc.x,0), Point(maxLoc.x,zoomed.rows-1), Scalar::all(255));
//    imshow("zoomed", zoomed);
    int bestx = 0,bestlayer = 0;
    Mat bestIndex_img = index_img >=1;
    minMaxLoc(index_img,&minVal,&maxVal,&minLoc,&maxLoc);
    for (int i = 1 ; i<=maxVal; i++) {
        Mat indexlayer_img = index_img >=i;
        medianBlur(indexlayer_img, indexlayer_img, 5);
        erode(indexlayer_img, indexlayer_img, blurSize);
        erode(indexlayer_img, indexlayer_img, blurSize);
        indexlayer_img = removeSmallBlobs(indexlayer_img);
        
        indexlayer_img = fillHoleInBinary(indexlayer_img);
        indexlayer_img = fillConvexHulls(indexlayer_img);
        Mat score = calculateImageSymmetryScore(indexlayer_img);
        Mat scoresum;
        reduce(score.rowRange(0, indexlayer_img.cols/6), scoresum, 0, CV_REDUCE_SUM,CV_32FC1);
        minMaxLoc(scoresum,&minVal,&maxVal,&minLoc,&maxLoc);
        if (abs(maxLoc.x - initialHC) < abs(bestx - initialHC)) {
            
            if (sum(indexlayer_img)[0]/255 < indexlayer_img.size().area()/5*2 &&
                sum(indexlayer_img)[0]/255 > indexlayer_img.size().area()/6) {
                bestx = maxLoc.x;
                bestlayer = i;
                bestIndex_img = indexlayer_img.clone();
                
            }
            
        }
    }
    
    Point2f massCenter = findMassCenter_BinaryBiggestBlob(bestIndex_img);
    
    
    eyeCoord =  Point2f(initialHC,massCenter.y);
}
Ejemplo n.º 5
0
Masek::IMAGE* Masek::canny(IMAGE *im, double sigma, double scaling, 
						   double vert, double horz, filter *gradient, filter *orND)
{

	filter  gaussian, *newim;
	int i, j;
	int hsize[2];
	int rows, cols;
	double xscaling, yscaling;
	double *h, *v, *d1, *d2, X, Y, begin, end;

	xscaling = vert;
	yscaling = horz;

	hsize[0] = (int)(6*sigma+1);
	hsize[1] = (int)(6*sigma+1);  // % The filter size.

	gaussian.hsize[0] = hsize[0];
	gaussian.hsize[1] = hsize[1];
	gaussian.data = (double*) malloc(sizeof(double)*hsize[0]*hsize[1]);

	/*gaussian = fspecial('gaussian',hsize,sigma);
	im = filter2(gaussian,im);        % Smoothed image.*/
	
    CREATEGAUSS (hsize, sigma, &gaussian);
	
	newim = filter2(gaussian,im);
	
	newim = imresize(newim, scaling);
	
	rows = newim->hsize[0];
	cols = newim->hsize[1];

	h = (double*)malloc(sizeof(double)*rows*cols);

	for (i = 0; i<rows; i++)
	{
		for (j = 0; j<cols; j++)
		{
			if (j == 0)
				*(h+i*cols+j) = (newim->data[i*cols+1]);
			else if (j == cols-1)
				*(h+i*cols+j) = (-newim->data[i*cols+j-1]);
			else
				*(h+i*cols+j) = (newim->data[i*cols+j+1]-newim->data[i*cols+j-1]);
			
		}
	}

	v = (double*)malloc(sizeof(double)*rows*cols);

	for (i = 0; i<rows; i++)
	{
		for (j = 0; j<cols; j++)
		{
			if (i == 0)
				*(v+i*cols+j) = (newim->data[(i+1)*cols+j]);
			else if (i == rows-1)
				*(v+i*cols+j) = -newim->data[(i-1)*cols+j];
			else
				*(v+i*cols+j) = (newim->data[(i+1)*cols+j]-newim->data[(i-1)*cols+j]);
			
		}
	}

	d1 = (double*)malloc(sizeof(double)*rows*cols);
	d2 = (double*)malloc(sizeof(double)*rows*cols);

	for (i = 0; i<rows; i++)
	{
		for (j = 0; j<cols; j++)
		{
			if (i == rows-1 || j == cols-1)
				begin = 0;
			else
				begin = newim->data[(i+1)*cols+j+1];
			
			if (i == 0 || j == 0)
				end = 0;
			else
				end = newim->data[(i-1)*cols+j-1];
					
			*(d1+i*cols+j) = begin-end;
			
		}
	}

	for (i = 0; i<rows; i++)
	{
		for (j = 0; j<cols; j++)
		{
			if (i == 0 || j == cols-1)
				begin = 0;
			else
				begin = newim->data[(i-1)*cols+j+1];
			
			if (i == rows-1 || j == 0)
				end = 0;
			else
				end = newim->data[(i+1)*cols+j-1];
					
			*(d2+i*cols+j) = begin-end;
			
		}
	}

	orND->data = (double*)malloc(sizeof(double)*rows*cols);
	orND->hsize[0] = rows;
	orND->hsize[1] = cols;

	gradient->data = (double*)malloc(sizeof(double)*rows*cols);
	gradient->hsize[0] = rows;
	gradient->hsize[1] = cols;

	
	for (i = 0; i<rows*cols;i++)
	{
        X = (h[i]+(d1[i]+d2[i])/2.0)*xscaling;
		
		Y = (v[i]+(d1[i]-d2[i])/2.0)*yscaling;

		gradient->data[i] = sqrt(X*X+Y*Y);
	
		orND->data[i] = atan2(-Y, X);
		
		/*if (i == 14)
		{
		printf("h is %f, d1 is %f, d2 is %f, v is %f xscaling is %f, yscaling is %f\n", h[i], d1[i], d2[i], v[i], xscaling, yscaling);
		printf("X is %f, Y is %f\n", X, Y);
		printf("orND is %f\n", orND->data[i]);
		}
*/
					
		if (orND->data[i]<0)
			orND->data[i]+=PI;
		
//		if (i == 14)
//			printf("orND is %f\n", orND->data[i]);		
		
		orND->data[i] = (orND->data[i]/PI)*180;
//		if (i == 14)
//			printf("orND is %f\n", orND->data[i]);		
		
		
	}
	
	free (gaussian.data);
	free(d1);
	free(d2);
	free(h);
	free(v);

	free(newim->data);
	free(newim);

	return im;
}