将一个IplImage的一部分复制到另一个Iplimage中(源是相同大小的) [英] Copying a portion of an IplImage into another Iplimage (that is of same size is the source)
问题描述
每当我识别相机上的先前已知场景时,都需要使用一组遮罩图像.所有的蒙版图像均为IplImage格式.例如,在某些情况下,摄像机会摇摄到稍微不同但附近的位置.这意味着,如果我在当前场景的中间某处进行模板匹配,则能够通过在该场景中对模板进行一定程度的移动来识别场景.我需要做的就是使用这些偏移来调整蒙版图像的ROI,以便可以基于模板匹配来适当地覆盖它们.我知道有一些功能,例如:
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.
推荐答案
我认为不对原始图像进行过多复制的解决方案是:
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;
我对代码本身进行了尝试,它对我来说也足够快(对于332 x 332灰度图像,执行时间约为1毫秒)
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)
这篇关于将一个IplImage的一部分复制到另一个Iplimage中(源是相同大小的)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!