예제 #1
0
void Camera::Render(Scene scene)
{
  // Compute upper left corner of frame
  Point UpperLeftCorner(CameraRay.Location);
  UpperLeftCorner = UpperLeftCorner.Translate(CameraRay.Direction.Normalize() * FrameDistance); // Currently in center of frame
  Vector UpUnit = UprightDir.Normalize();
  UpperLeftCorner = UpperLeftCorner.Translate(UpUnit*(Height / 2)); // Currently in top center of frame
  // Compute the perpendicular, leftward facing vector and move the point to the upper left corner
  Vector LeftUnit = UprightDir.Cross(CameraRay.Direction).Normalize() * -1;
  UpperLeftCorner = UpperLeftCorner.Translate(LeftUnit*(Width / 2));

  Vector HorzInc = LeftUnit * (-1 * Width/ static_cast<double>(Resolution.width)); // Progress to the right
  Vector VertInc = UpUnit * (-1 * Height/ static_cast<double>(Resolution.height)); // Progress downwards

  // Position upper left corner to be in the center of the pixel
  UpperLeftCorner = UpperLeftCorner.Translate((HorzInc * (0.5)) + (VertInc * (0.5)));

  Point PixelLocation(0,0,0);
  // Iterate over the image space and render each occupied pixel
  for (int YPixel = 0; YPixel < Resolution.height; YPixel++)
  {
    for (int XPixel = 0; XPixel < Resolution.width; XPixel++)
    {
      PixelLocation = UpperLeftCorner.Translate((VertInc * YPixel) + (HorzInc * XPixel));
      Ray CameraToPixel(CameraRay.Location, CameraRay.Location.FromThisToThat(PixelLocation));

      cv::Vec3b PixelValue = CastRay(scene, CameraToPixel);

      Image.at<cv::Vec3b>(YPixel, XPixel) = PixelValue;
    }
  }
}
예제 #2
0
void SobelEdge::Filter(unsigned char** input, int col, int row)
{
    int i = 0, j = 0;
    float tempAngle = 0;
    m_ppEdgeMap = (unsigned char**)CreateMatrix(col+2, row+2, -1, -1, sizeof(unsigned char), &m_lPointerOffset);
    if(m_bSaveAsOriginal)
    {
        m_ppOriginalEdgeMap = (unsigned char**)CreateMatrix(col+2, row+2, -1, -1, sizeof(unsigned char), &m_lPointerOffset);
    }
    AllocateMemory(col, row, 0, 0);
    float **tempEdge = NULL;
	float **tempEdge1 = NULL;

    long long tempPointerOffset;
    tempEdge = (float**)CreateMatrix(col+2, row+2, -1, -1, sizeof(float), &tempPointerOffset);
	tempEdge1 = (float**)CreateMatrix(col+2, row+2, -1, -1, sizeof(float), &tempPointerOffset);

	float max = 0.0, min = 50000000;

	if(m_fLowerThreshold > m_fUpperThreshold)
	{
		std::cerr<<"Lower threshold is bigger than the upper threshold!"<<std::endl;
		return;
	}

    for(i=0;i<row;i++)
    {
        for(j=0;j<col;j++)
        {
            // Simple Sobel filter kernels.
            float temp = 0.0, temp1 = 0.0, temp2 = 0.0;
            temp = (float)(input[i-1][j-1]*m_SobelOperator1[0][0] + input[i-1][j]*m_SobelOperator1[0][1] + input[i-1][j+1]*m_SobelOperator1[0][2] + 
                        input[i][j-1]*m_SobelOperator1[1][0]   + input[i][j]*m_SobelOperator1[1][1]   + input[i][j+1]*m_SobelOperator1[1][2] + 
                        input[i+1][j-1]*m_SobelOperator1[2][0] + input[i+1][j]*m_SobelOperator1[2][1] + input[i+1][j+1]*m_SobelOperator1[2][2]);

            temp1 = (float)(input[i-1][j-1]*m_SobelOperator2[0][0] + input[i-1][j]*m_SobelOperator2[0][1] + input[i-1][j+1]*m_SobelOperator2[0][2] +
                        input[i][j-1]*m_SobelOperator2[1][0]   + input[i][j]*m_SobelOperator2[1][1]   + input[i][j+1]*m_SobelOperator2[1][2] +
                        input[i+1][j-1]*m_SobelOperator2[2][0] + input[i+1][j]*m_SobelOperator2[2][1] + input[i+1][j+1]*m_SobelOperator2[2][2]);

            temp2 = temp*temp + temp1*temp1;

            // Preparation for converting float type to unsigned char for saving unthresholded edgemap.
			if(temp2>max)
			{
				max = temp2;
			}
			if(temp2<min)
			{
				min = temp2;
			}
			tempEdge[i][j] = temp2;

            tempAngle = atan2(temp, temp1);

            m_ppGradientDirection[i][j] = tempAngle;
        }
    }
    // Converting float to unsigned char for saving.
    Suppress(m_iColumns, m_iRows, tempEdge, tempEdge1);
	if(m_bSaveAsOriginal)
	{
		for(i=0;i<row;i++)
		{
			for(j=0;j<col;j++)
			{
				m_ppOriginalEdgeMap[i][j] = (unsigned char)(255.0-(tempEdge1[i][j]/(max-min)*255.0)); // Edge is black
			}
		}
	}
    for(i=0;i<row;i++)
	{
		for(j=0;j<col;j++)
		{
			if(tempEdge1[i][j]>m_fUpperThreshold)
            {
                m_ppEdgeMap[i][j] = 0; // Edge is black
#ifdef _DEBUG					
                m_statistic++;
#endif
            }
			else if(tempEdge1[i][j]<=m_fUpperThreshold && tempEdge1[i][j]>m_fLowerThreshold)
			{
				m_MaybePixels.push_back(PixelLocation(j, i)); // Remember maybe-pixels
			}
            else
            {
                m_ppEdgeMap[i][j] = 255;
            }
		}
	}
	Resolveambiguity(); // Resolve maybe-pixels

    tempEdge += tempPointerOffset;
    if(tempEdge != NULL)
    {
        free(tempEdge);
    }
	tempEdge1 += tempPointerOffset;
    if(tempEdge1 != NULL)
    {
        free(tempEdge1);
    }
#ifdef _DEBUG
    std::cout<<"Edge Pixels: "<<m_statistic<<std::endl;
#endif
}