![](/img/trans.png)
[英]Creating bigger image out of small image Mat [opencv, background subtraction]
[英]Tiling a small image into a bigger one with OpenCV
假设我有一个 200x200 像素的图像。 我想要一个 800x800 像素的版本,我基本上复制了 200x200 的图像并填充了 800x800 的图像(将较小的图像平铺到较大的图像中)。
你会如何在 openCV 中做到这一点? 看起来很简单,但我不知道如何创建另一个与模式相同类型的 cv::Mat,但具有更大的尺寸(画布尺寸),或者是否可以采用原始的 200x200 像素图像和增加它的行和列,然后只需使用循环将角粘贴到图像的其余部分。
顺便说一句,我正在使用 openCV 2.3。 我已经对固定尺寸的图像进行了相当多的处理,但是在增加矩阵的尺寸方面我有点无能为力。
如果您对使用 OpenCV 进行平铺感兴趣, 请查看这篇文章。
仅供参考-@karlphillip 回复中的博客,基本思想是使用cvSetImageROI
和cvResetImageROI
。 两者都是 C API。
在以后的版本中,例如 v2.4 和 3.x,可以定义具有所需位置和尺寸的Rect
,并将所需部分称为img(rect)
。
C API 中的示例(函数式):
cvSetImageROI(new_img, cvRect(tlx, tly, width, height);
cvCopy(old_img, new_img);
cvResetImageROI(new_img);
在使用类的 C++ API 中:
Mat roi(new_img, Rect(tlx, tly, width, height));
roi = old_img; // or old_img.clone()
C++ 中的另一种方式(复制图像):
old_img.copyTo(new_img(Rect(tlx, tly, width, height)))
您可以使用重复功能:
def tile_image(img, tile):
height, width, channels = img.shape
tile_width, tile_height, tile_channel = tile.shape
x_counts = int(width / tile_width) + 1
y_counts = int(height / tile_height) + 1
tiled = np.tile(tile,(y_counts,x_counts,1))
return tiled[0:height, 0:width]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.