Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

MAT is empty after doing rotate the image

Hi All i had developed the following function to rotate an image with specified angle

void rotate(InputArray _src, OutputArray _dst, double angle,bool bKeepOldSize = false)
{
    Mat src = _src.getMat();
    Mat dst = _dst.getMat();
    Point2f pt(src.cols/2., src.rows/2.);    
    Mat r = getRotationMatrix2D(pt, angle, 1.0);
    Size ImgSize = Size(src.cols, src.rows);

    //Calculate the new image size if required
    cv::Rect bbox = cv::RotatedRect(pt,src.size(), angle).boundingRect();
    if(bKeepOldSize == false)
    {
        // adjust transformation matrix and destination matrix
        r.at<double>(0,2) += bbox.width/2.0 - pt.x;
        r.at<double>(1,2) += bbox.height/2.0 - pt.y;
        ImgSize = bbox.size();
        dst.create(ImgSize,src.type());     
    }

    warpAffine(src, dst, r, ImgSize,INTER_LANCZOS4,BORDER_CONSTANT,Scalar(0));
    imwrite("c:\\temp\\tempdeskew.tif",dst);

}

at the end of the function , the rotated image is saved correctly , but when i'm return to the caller function Matrix data is empty why ?

here it the code which generate the empty Mat

//read the input image
Mat orgMat = cv::imread(strInputFile,CV_LOAD_IMAGE_UNCHANGED);      
Mat greyMat;    
Mat blackAndWhiteMat;
Mat DeskewedMat;
//If image is color (3 channels) convert to Gray    
//If image is Gray or Black and white (1 channel) just clone
int iChannelsNum = orgMat.channels();   
if (iChannelsNum == 1)
{
    greyMat = orgMat.clone();
}
else if (iChannelsNum == 3)
{
    cvtColor(orgMat, greyMat, CV_BGR2GRAY);
}   
else if(iChannelsNum == 4)
{
    cvtColor(orgMat, greyMat, CV_BGRA2GRAY);
}
else
{
    //unknown number of channels
    return UNSUPPORTED_CHANNELS_NUMBER;     
}

//Convert the gray image to black and white
threshold(greyMat ,blackAndWhiteMat,128,255,CV_THRESH_BINARY);
rotate(blackAndWhiteMat,DeskewedMat,-8.0);//Problem here!!! DeskewedMat is empty, while the Mat is correct and saved inside rotate function

Why the refrence for OutArray "DeskewedMat" not filled with the rotated Mat ?!D:\Work\OCR\Sakhr\MyOpenCVTester\MyOpenCVTester\Samples\skewed\m8.jpg