Ask Your Question
1

MAT is empty after doing rotate the image

asked 2016-03-06 07:29:58 -0500

essamzaky gravatar 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

edit retag flag offensive close merge delete

1 answer

Sort by » oldest newest most voted
2

answered 2016-03-06 08:26:34 -0500

berak gravatar image

the problem is here:

Mat dst = _dst.getMat();

dst is a copy , it is not synched with _dst, as you expected. instead, you should work with _dst:

void rotate(InputArray _src, OutputArray _dst, double angle,bool bKeepOldSize = false)
{
    Mat src = _src.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());     // allocate larger Mat here
    }
    // pass it to warpAffine:
    warpAffine(src, _dst, r, ImgSize,INTER_LANCZOS4,BORDER_CONSTANT,Scalar(0));
}

// test code:
int main()
{
    Mat letters = imread("deskew.jpg",0);
    threshold(letters,letters,128,255,CV_THRESH_BINARY);

    Mat output;
    rotate(letters,output,-8.0);

    imwrite("output.png",output);
    cerr << output.size() << endl;
    return 0;
}
// cönsöle output:
[676 x 379]

image description

edit flag offensive delete link more

Comments

1

Thanks @berak , it works

essamzaky gravatar imageessamzaky ( 2016-03-06 08:34:53 -0500 )edit

Question Tools

1 follower

Stats

Asked: 2016-03-06 07:29:58 -0500

Seen: 890 times

Last updated: Mar 06 '16