I dont know why, but I solved my problem by spliting the saved mat and merging .
int width = 512;
int height = 512;
uchar* data_encode = checkResized.data;
cv::Mat b(height,width,CV_8UC1,&data_encode[0]);
cv::Mat g(height,width,CV_8UC1,&data_encode[(heightwidth)sizeof(uchar)]);
cv::Mat r(height,width,CV_8UC1,&data_encode[(2heightwidth)*sizeof(uchar)]);
test.push_back(b);
test.push_back(g);
test.push_back( r );
cv::Mat out ;
cv::merge(test,out);
The final output out is correct.
For your reference.