Exemple #1
0
void splitVerticalLineIntoDigits(IplImage *source, IplImage *array[9]) {
    
    int countOfSquares = 0;
    
    int start = 0;
    int stop = 0;
    
    int i = 0;
    do {
        double val = cvGet2D(source, i, source->width/2).val[0];
        if (val == 0.0)
            start = i;
        i++;
    } while (!start);
    
    i = source->height-1;
    do {
        double val = cvGet2D(source, i, source->width/2).val[0];
        if (val == 0.0)
            stop = i;
        i--;
    } while (!stop);

    int prevLineY = start;

    for (int i = start ; i < stop; i++) {
        if (i - prevLineY > (stop-start)/ 9) {
            CvRect rect = cvRect(0, prevLineY, source->width, i - prevLineY);
            prevLineY = i;
            IplImage *resultImage = cvCreateImage(cvSize(rect.width, rect.height),IPL_DEPTH_8U,1);
            GetSubImage(source, resultImage, rect);
            array[countOfSquares++] = resultImage;
            i+=2; //to avoid one-pixel black lines
        }
        CvRect rect = cvRect(0, prevLineY, source->width, stop - prevLineY);
        IplImage *resultImage = cvCreateImage(cvSize(rect.width, rect.height),IPL_DEPTH_8U,1);
        GetSubImage(source, resultImage, rect);
        array[countOfSquares] = resultImage;
    }
}
Exemple #2
0
void splitSudokuIntoVerticalStripes(IplImage *source, IplImage *stripes[9]) {
    IplImage *img_bw = cvCreateImage(cvGetSize(source),IPL_DEPTH_8U,1);
    cvCvtColor(source, img_bw, CV_RGB2GRAY);
            
    cvThreshold(img_bw, img_bw, 125, 0, CV_THRESH_TOZERO);

    //IplImage *closedImage = closeImage(img_bw);

    int cutThreshold = source->height * 0.3;
    int averageRowWidth = source->width / 9 * 0.7;
    int currentLineLength;
    int countOfStripes = 0;
    int start = 0;
    
    for (int i = 0; i < source->width; i++) {
        double val = cvGet2D(img_bw, 5, i).val[0];
        if (val == 0.0) {
            start = i; break;
        }
    }

    if (start > source->width / 9) {
        start = 0;
    }
    int prevLineX = start;

    for (int i = start ; i < source->width; i++) {
        currentLineLength = 0;
        for (int j = 0 ; j < source->height; j++) {
            double val = cvGet2D(img_bw, j, i).val[0];
            if (val == 0.0) {
                currentLineLength++; 
            }
        }
        
        int magicCropGap = 5;
        if (currentLineLength >= cutThreshold) {
            if (i - prevLineX < averageRowWidth) {
                continue;
            }
            CvRect rect = cvRect(prevLineX + magicCropGap, 0, i - prevLineX - magicCropGap, source->height);
            prevLineX = i;
            IplImage *resultImage = cvCreateImage(cvSize(rect.width, rect.height),IPL_DEPTH_8U,1);
            GetSubImage(img_bw, resultImage, rect);
            stripes[countOfStripes++] = resultImage;
            if (countOfStripes >= 9) {
                return;
            }
            i+=2; //to avoid one-pixel black lines
        }
    }
}
Exemple #3
0
IplImage* cropGarbageFromTopAndBottom(IplImage *source) {
    IplImage *img_bw = cvCreateImage(cvGetSize(source),IPL_DEPTH_8U,1);
    cvCvtColor(source, img_bw, CV_RGB2GRAY);

    int startY = 0, stopY = source -> height;
    
    //top
    for (int i = 1 ; i < source->height / 2; i++) {
        int lenghtsTop = 0;
        for (int j = 0 ; j < source->width; j++) {
            double val = cvGet2D(img_bw, i, j).val[0];
            if (val == 0.0) {
                lenghtsTop++; 
            }
        }
        if (lenghtsTop == 0) {
            startY = i;
            break;
        }
    }
    
    //bottom
    for (int i = source->height /2 ; i < source->height - 1; i++) {
        int lenghtsBottom = 0;
        for (int j = 0 ; j < source->width; j++) {
            double val = cvGet2D(img_bw, i, j).val[0];
            if (val == 0.0) {
                lenghtsBottom++; 
            }
        }
        if (lenghtsBottom == 0) {
            stopY = i + 1;
            break;
        }
    }
    
    CvRect rect = cvRect(0, startY, source->width, stopY - startY);
    IplImage *cropped_dest = cvCreateImage(cvSize(rect.width, rect.height),IPL_DEPTH_8U,1);
    GetSubImage(img_bw, cropped_dest, rect);
    return cropped_dest;
}
Exemple #4
0
void ezImage::AllocateImageData()
{
  m_subImages.SetCount(m_uiNumMipLevels * m_uiNumFaces * m_uiNumArrayIndices);

  int uiDataSize = 0;

  bool bCompressed = ezImageFormat::GetType(m_format) == ezImageFormatType::BLOCK_COMPRESSED;
  ezUInt32 uiBitsPerPixel = ezImageFormat::GetBitsPerPixel(m_format);

  for (ezUInt32 uiArrayIndex = 0; uiArrayIndex < m_uiNumArrayIndices; uiArrayIndex++)
  {
    for (ezUInt32 uiFace = 0; uiFace < m_uiNumFaces; uiFace++)
    {
      for (ezUInt32 uiMipLevel = 0; uiMipLevel < m_uiNumMipLevels; uiMipLevel++)
      {
        SubImage& subImage = GetSubImage(uiMipLevel, uiFace, uiArrayIndex);

        subImage.m_uiDataOffset = uiDataSize;

        if (bCompressed)
        {
          ezUInt32 uiBlockSize = 4;
          subImage.m_uiRowPitch = 0;
          subImage.m_uiDepthPitch = GetNumBlocksX(uiMipLevel) * GetNumBlocksY(uiMipLevel) * uiBlockSize * uiBlockSize * uiBitsPerPixel / 8;
        }
        else
        {
          subImage.m_uiRowPitch = GetWidth(uiMipLevel) * uiBitsPerPixel / 8;
          subImage.m_uiDepthPitch = GetHeight(uiMipLevel) * subImage.m_uiRowPitch;
        }

        uiDataSize += subImage.m_uiDepthPitch * GetDepth(uiMipLevel);
      }
    }
  }

  m_data.SetCount(uiDataSize + 16);
}
Exemple #5
0
IplImage* cropImage(IplImage *source) {
    int startY = 0, stopY = source -> height, startX = 0, stopX = source->width;
    
    //top
    for (int i = 1 ; i < source->height / 2; i++) {
        int lenghtsTop = 0;
        for (int j = 0 ; j < source->width; j++) {
            double val = cvGet2D(source, i, j).val[0];
            if (val == 0.0) {
                lenghtsTop++; 
            }
        }
        if (lenghtsTop != 0) {
            startY = i - 1;
            break;
        }
    }
//    
    //bottom
    for (int i = source->height - 2 ; i > source->height /2; i--) {
        int lenghtsBottom = 0;
        for (int j = 0 ; j < source->width; j++) {
            double val = cvGet2D(source, i, j).val[0];
            if (val == 0.0) {
                lenghtsBottom++; 
            }
        }
        if (lenghtsBottom != 0) {
            stopY = i + 1;
            break;
        }
    }
    
    //left
    for (int i = 1 ; i < source->width / 2; i++) {
        int lenghtsLeft = 0;
        for (int j = 0 ; j < source->height; j++) {
            double val = cvGet2D(source, j, i).val[0];
            if (val == 0.0) {
                lenghtsLeft++; 
            }
        }
        if (lenghtsLeft != 0) {
            startX = i - 1;
            break;
        }
    }
    
    //right
    for (int i = source->width - 3; i > source->width / 2; i--) {
        int lenghtsRight = 0;
        for (int j = 0 ; j < source->height; j++) {
            double val = cvGet2D(source, j, i).val[0];
            if (val == 0.0) {
                lenghtsRight++; 
            }
        }
        if (lenghtsRight != 0) {
            stopX = i + 1;
            break;
        }
    }
    CvRect rect = cvRect(startX, startY, stopX - startX, stopY - startY);
    IplImage *cropped_dest = cvCreateImage(cvSize(rect.width, rect.height),IPL_DEPTH_8U,1);
    GetSubImage(source, cropped_dest, rect);
    return cropped_dest;

}