Copying a portion of an IplImage into another Iplimage (that is of same size is the source)

佐手、 提交于 2019-12-24 11:47:04

问题


I have a set of mask images that I need to use everytime I recognise a previously-known scene on my camera. All the mask images are in IplImage format. There will be instances where, for example, the camera has panned to a slightly different but nearby location. this means that if I do a template matching somewhere in the middle of the current scene, I will be able to recognise the scene with some amount of shift of the template in this scene. All I need to do is use those shifts to adjust the mask image ROIs so that they can be overlayed appropriately based on the template-matching. I know that there are functions such as:

cvSetImageROI(Iplimage* img, CvRect roi)
cvResetImageROI(IplImage* img);

Which I can use to set crop/uncrop my image. However, it didn't work for me quit the way I expected. I would really appreciate if someone could suggest an alternative or what I am doing wrong, or even what I haven't thought of!

**I must also point out that I need to keep the image size same at all times. The only thing that will be different is the actual area of interest in the image. I can probably use the zero/one padding to cover the unused areas.


回答1:


I believe a solution without making too many copies of the original image would be:

// Make a new IplImage
IplImage* img_src_cpy = cvCreateImage(cvGetSize(img_src), img_src->depth, img_src->nChannels);

// Crop Original Image without changing the ROI
for(int rows = roi.y; rows < roi.height; rows++) {
    for(int cols = roi.x; rows < roi.width; cols++) {        
        img_src_cpy->imageData[(rows-roi.y)*img_src_cpy->widthStep + (cols-roi.x)] = img_src[rows*img_src + cols];
    }
{

//Now copy everything to the original image OR simply return the new image if calling from a function
cvCopy(img_src_cpy, img_src); // OR return img_src_cpy;

I tried the code out on itself and it is also fast enough for me (executes in about 1 ms for 332 x 332 Greyscale image)



来源:https://stackoverflow.com/questions/15157461/copying-a-portion-of-an-iplimage-into-another-iplimage-that-is-of-same-size-is

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