get Original image from ROI image in opencv

守給你的承諾、 提交于 2020-01-23 16:43:27

问题


is it possible to get back original image from image ROI? for example say we have

cv::Mat image = imread("image.jpg", 0);
cv::Mat imageROI = image(0, 0, 100, 100);
myFunction(imageROI);

and in myFunction I want to work with original image. is there any way to convert imageROI to original image when we don't access the original image?


回答1:


Just incase anybody looks at this question you can actually do this

cv::Mat mat = ...

cv::Size  size;
cv::Point offset;
// find original image size, and get offset of roi
mat.locateROI(size, offset);
// put image back to original size;
mat.adjustROI(offset.y, size.height - mat.rows, offset.x, size.width- mat.cols);



回答2:


I don't know if I understood the question exactly like you think, but if you ask if let's say we have header

void myFunc(cv::Mat &m);
// .... later on
cv::Mat image = imread("image.jpg", 0);
cv::Mat imageROI = image(0, 0, 100, 100);
myFunction(imageROI);
// .... later on myFuncDefinition
void myFunc(cv::Mat &m) {
    // some code
    // here you would like to have an original image, right?
}

So the answer for that is no and the proof is by simplicity: why want you to design opencv api in such way to make it possible store unnecessary data? If you do

cv::Mat imageROI = image(0, 0, 100, 100);

by purpose you would like to forgot about entire image and you are particulary interested in some ROI. Mat container is designed in such way to copy only matrix 'headers' and not matrix content. So if you do cv::Mat imageROI = image(0, 0, 100, 100) perhaps the matrix content (ie image data) might be stored somewhere in memory (because roi is the part of it, so by optimalization purposes it might no be deleted even is original image variable went out of scope), but your matrix header changed. Namely, from pointing to (0, 0, imageWisth, imageHeight) to (0, 0, 100, 100) and there's no way to bring it back just using variable m.

Why don't pass additional parameter as a reference?



来源:https://stackoverflow.com/questions/22498373/get-original-image-from-roi-image-in-opencv

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!