Example #1
0
// [[Rcpp::export]]   
CharacterVector map_cell_types_to_char(IntegerVector t){
  
  // 0: "n"
  // 1: "s"
  // 2: "b"
  // 3: "str"
  // 4: "e"
  // 9: "h"

  size_t n = t.size();
  CharacterVector t_res(n);

  for(size_t i = 0; i < n; i++){
    
    if(IntegerVector::is_na(t[i])){
      t_res[i] = NA_STRING;
    }else if(t[i] == 0){
      t_res[i] = "n";
    }else if(t[i] == 1){
      t_res[i] = "s";
    }else if(t[i] == 2){
      t_res[i] = "b";
    }else if(t[i] == 3){
      t_res[i] = "str";
    }else if(t[i] == 4){
      t_res[i] = "e";
    }else{
      t_res[i] = "s";
    }
    
  }
  
  return t_res;
  
}
Example #2
0
// [[Rcpp::export]]   
IntegerVector map_cell_types_to_integer(CharacterVector t){
  
  // 0: "n"
  // 1: "s"
  // 2: "b"
  // 3: "str"
  // 4: "e"
  // 9: "h"
  
  size_t n = t.size();
  IntegerVector t_res(n);

  for(size_t i = 0; i < n; i++){
    
    if(CharacterVector::is_na(t[i])){
      t_res[i] = NA_INTEGER;
    }else if(t[i] == "n"){
      t_res[i] = 0;
    }else if(t[i] == "s"){
      t_res[i] = 1;
    }else if(t[i] == "b"){
      t_res[i] = 2;
    }else if(t[i] == "str"){
      t_res[i] = 3;
    }else if(t[i] == "e"){
      t_res[i] = 4;
    }
    
  }
  
  return t_res;
  
}
/* Detection is for single scale detection
   Input:
           1. im: A grayscale image in height x width.
           2. ext: The pre-defined HOG extractor.
           3. threshold: The constant threshold to control the prediction.
   Output:
           1. bbox: The predicted bounding boxes in this format (n x 5 matrix):
                                    x11 y11 x12 y12 s1
                                    ... ... ... ... ...
                                    xi1 yi1 xi2 yi2 si
                                    ... ... ... ... ...
                                    xn1 yn1 xn2 yn2 sn
                    where n is the number of bounding boxes. For the ith 
                    bounding box, (xi1,yi1) and (xi2, yi2) correspond to its
                    top-left and bottom-right coordinates, and si is the score
                    of convolution. Please note that these coordinates are
                    in the input image im.
*/
Mat Detector::Detection(const Mat& im, HOGExtractor& ext, const float threshold) {

	/* TODO 4: 
       Obtain the HOG descriptor of the input im. And then convolute the linear
	   detector on the HOG descriptor. You may put the score of convolution in 
	   the structure "convolutionScore".
	*/
	Mat HOGDes = ext.ExtractHOG(im);
	Mat convolutionScore(HOGDes.size[0], HOGDes.size[1], CV_32FC1, Scalar(0));

	// Begin TODO 4
	Mat detector = GetDetector();
	vector<cv::Mat> channels_HOGDes(36);
	vector<cv::Mat> channels_detector(36);

	split(HOGDes, channels_HOGDes);
	split(detector, channels_detector);

	vector<cv::Mat>::iterator iter1;
	vector<cv::Mat>::iterator iter2;
	for (iter1 = channels_HOGDes.begin(), iter2 = channels_detector.begin();
		iter1 != channels_HOGDes.end(),iter2!=channels_detector.end(); iter1++, iter2++)
	{
		Mat t_res(HOGDes.size[0], HOGDes.size[1], CV_32FC1, Scalar(0));
		filter2D( (*iter1), t_res, (*iter1).depth(), (*iter2) );
		convolutionScore += t_res;
	}
//	cout << "HoGsize= " << HOGDes.size[0] << ", " << HOGDes.size[1] << endl;
//	cout << "Detectorsize= " << detector.size[0] << ", " << detector.size[1] << endl;
/*	for (int i; i < HOGDes.size[0]; i++)
	{
		for (int j; j < HOGDes.size[0]; j++)
		{
			cout << convolutionScore.at<float>(i,j) << ", ";
		}
		cout << endl;
	}
	cout << endl;*/
	// End TODO 4
	
	/* TODO 5: 
       Select out those positions where the score is above the threshold. Here,
	   the positions are in ConvolutionScore. To output the coordinates of the
	   bounding boxes, please remember to recover the positions to those in the
	   input image. Please put the predicted bounding boxes and their scores in
	   the below structure "bbox".
	*/
	Mat bbox;
	
	// Begin TODO 5
	float cells = ext.GetCells();
	float blocks = ext.GetBlocks();
	float maxScore = -5.0;
	for ( int i = 0; i < convolutionScore.size[0]; i++ )
	{
		for ( int j = 0; j < convolutionScore.size[1]; j++ )
		{
			if (convolutionScore.at<float>(i, j)>maxScore)
			{
				maxScore = convolutionScore.at<float>(i, j);
			}
			if (convolutionScore.at<float>(i, j) > threshold)
			{
				float x1, y1, x2, y2, score;
				score = convolutionScore.at<float>(i, j);
				x1 = (int)((float)i - ((float)detector.size[0] / 2)+0.5) * cells;
				y1 = (int)((float)j - ((float)detector.size[1] / 2) + 0.5) * cells;
				x2 = (int)((float)i + ((float)detector.size[0] / 2) + 0.5) * cells;
				y2 = (int)((float)j + ((float)detector.size[1] / 2) + 0.5) * cells;
				Mat t_bbox = (Mat_<float>(1, 5) << x1, y1, x2, y2, score);

				if (bbox.size[0]!=0)
				{
					vconcat(bbox, t_bbox, bbox);
				}
				else
				{
					bbox = t_bbox;	
					//bbox = Mat(t_bbox);
				}
			}
		}
	}
	cout << "Max Score = " << maxScore << endl;
	// End TODO 5
//	cout << "Here bbox = " <<bbox.rows<<", "<<bbox.cols<< endl;
	return bbox;
}