I want to add multiple images to a single texture. Example background size 1024x1024.
I want to resize and put some images on 1024x1024 texture to specific position. I can blend and even offset images on each other when their sizes are same. When different I get error on vs2019.
I did something like this:
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgcodecs/imgcodecs.hpp>
#include <opencv2/imgproc.hpp>
#include "imageOperations.h"
#include <list>
using namespace cv;
using namespace std;
struct imageStruct {
string path;
string name;
short channels;
bool isPowerOfTwo;
Mat image;
Size dimensions;
};
//this works for offseting when blending images with same size
void OffsetImage(Mat& image, cv::Scalar bordercolour, int xoffset, int yoffset)
{
int xoffset_ = xoffset * -1;
int yoffset_ = yoffset * -1;
Mat offsetImage = Mat::zeros(image.size(), image.type());
Mat padded = Mat(image.rows + 2 * abs(yoffset_), image.cols + 2 * abs(xoffset_), CV_8UC3, bordercolour);
image.copyTo(padded(Rect(abs(xoffset_), abs(yoffset_), image.cols, image.rows)));
image = Mat(padded, Rect(abs(xoffset_) + xoffset_, abs(yoffset_) + yoffset_, image.cols, image.rows));
}
...
int main()
{
std::list<imageStruct>imageList;
imageStruct img;
img.image = imread("C:/Users/gomi/source/repos/UEFlipbookPacker/Debug/cover.jpg");
Size imgsize = img.image.size();
img.dimensions = imgsize;
INT2 new_img_size = resize(ImageResizeMethod::width, 512, INT2{ img.dimensions.width,img.dimensions.height });
resize(img.image, img.image, { new_img_size.x, new_img_size.y },0,0,cv::INTER_NEAREST);
imageStruct img2;
img2.image = imread("C:/Users/gomi/source/repos/UEFlipbookPacker/Debug/cover_blend.jpg");
Size img2size = img2.image.size();
img2.dimensions = img2size;
INT2 new_img2_size = resize(ImageResizeMethod::width, 512, INT2{ img2.dimensions.width,img2.dimensions.height });
resize(img2.image, img2.image, { new_img2_size.x, new_img2_size.y }, 0, 0, cv::INTER_NEAREST);
imageList.push_back(img);
imageList.push_back(img2);
//imshow("Display Window", ???final_img???);
waitKey(0);
return 0;
}
I produce many textures into one texture and in UE4 that looks like this:
First image is the thing I want to do with openCV
I manually do this process I produce many textures so I need to put them to gether into power of two textures.
I do not know how to put two or more different images in different sizes onto a third texture with different size. How to do it?
UPDATE 1:
After searching for an hour I found a solution I manipulated pixel data like this:
struct imageStruct {
string path;
string name;
short channels;
bool isPowerOfTwo;
Mat image;
Size dimensions;
};
bool OverlayTwoImages(imageStruct src_img, imageStruct target_img, INT2 offset)
{
for (int x = 0; x < src_img.image.cols; x++)
{
for (int y = 0; y < src_img.image.rows; y++)
{
if (x < target_img.image.cols && y < target_img.image.rows) {
src_img.image.at<Vec3b>(Point(x + offset.x, y + offset.y))[2] = target_img.image.at<Vec3b>(Point(x, y))[2];
src_img.image.at<Vec3b>(Point(x + offset.x, y + offset.y))[1] = target_img.image.at<Vec3b>(Point(x, y))[1];
src_img.image.at<Vec3b>(Point(x + offset.x, y + offset.y))[0] = target_img.image.at<Vec3b>(Point(x, y))[0];
}
}
}
return true;
}
Usage:
OverlayTwoImages(img, img2, { 100,100 });