Ask Your Question
1

How to blend a horizontal and vertical image?

asked 2017-09-03 06:42:22 -0600

linengmiao gravatar image

updated 2017-09-03 09:21:17 -0600

Hello

I have those two images:

image description

image description

I'd like to blend the overlapping parts in order to obtain an image like this (but where the corners are blended):

image description

On the last image you can see that the 2 parts of my image are overlapping, while I actually like to blend that region.

And this is the result i obtained so far by using the code below : image description

I actually want to obtain a continuous smooth image.

How can this be achieved without creating artifacts?

Thanks

//g++ stitcher2.cpp $(pkg-config --cflags --libs opencv) -o main

#include <opencv2/opencv.hpp>
#include "opencv2/core.hpp"
#include "opencv2/imgcodecs.hpp"
#include "opencv2/imgproc.hpp"
#include "opencv2/highgui.hpp"
#include "opencv2/aruco.hpp"
#include "opencv2/aruco/dictionary.hpp"
#include "opencv2/calib3d.hpp"

#include <sstream>
#include <iostream>
#include <fstream>

using namespace std;
using namespace cv;


int main(void)
{

    Mat horizontalImg = imread("/home/a/Desktop/part1.png");
    Mat verticalImg = imread("/home/a/Desktop/part2.png");
    Mat output = Mat::zeros(horizontalImg.cols, verticalImg.rows, CV_8UC3);

    //imshow("a", horizontalImg);
    //imshow("b", verticalImg);
    //waitKey(0);

    int widthOverlap = 149;
    int heightOverlap = 152;
    Vec3b white = (255, 255, 255);

    for(int i=0;i<verticalImg.rows;i++)
    {
        for(int j=0;j<horizontalImg.cols;j++)
        {
            if(i<heightOverlap)
            {
                if(j<widthOverlap)
                {
                    output.at<Vec3b>(i,j) = (0.5 * horizontalImg.at<Vec3b>(i,j)) + (0.5 * verticalImg.at<Vec3b>(i,j));

                }
                else
                {
                    output.at<Vec3b>(i,j) = horizontalImg.at<Vec3b>(i,j);
                }
            }
            if(j<widthOverlap)
            {
                if(i>heightOverlap)
                {
                   output.at<Vec3b>(i,j) = verticalImg.at<Vec3b>(i,j);
                }                
            }
            if(j>widthOverlap)
            {
                if(i>heightOverlap)
                {
                   output.at<Vec3b>(i,j) = white;
                }                
            }

        }
    }

    imshow("final result", output);
    waitKey(0);


    return 0;
}
edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
1

answered 2017-09-03 09:51:33 -0600

updated 2017-09-03 09:59:12 -0600

try this code to get something like the image below. ( it uses single image )

image description

if you uncomment //flip(horizontalImg, horizontalImg, 1);

you will get something like the image below ( it seems more logical to me ) image description

#include "opencv2/core.hpp"
#include "opencv2/imgcodecs.hpp"
#include "opencv2/imgproc.hpp"
#include "opencv2/highgui.hpp"

#include <iostream>

using namespace std;
using namespace cv;

int main(int argc, char** argv)
{
    Mat horizontalImg = imread(argv[1]);
    //flip(horizontalImg, horizontalImg, 1);
    Mat output = horizontalImg.t();

    copyMakeBorder(output, output, 0, 0, 0, output.rows - output.cols, BORDER_CONSTANT, Scalar(255, 255, 255));
    Rect rect(0, 0, horizontalImg.cols, horizontalImg.rows);
    Mat mask(horizontalImg.rows, horizontalImg.cols, CV_8UC1, Scalar(255));
    vector<Point> pts = { Point(0,0), Point(mask.rows,mask.rows), Point(0,mask.rows) };

    fillConvexPoly(mask, pts, Scalar(0));

    horizontalImg.copyTo(output(rect),mask);
    imshow("source", horizontalImg);

    imshow("final result", output);
    waitKey(0);

    return 0;
}
edit flag offensive delete link more

Comments

Exactly what I needed. Thank you very much!

linengmiao gravatar imagelinengmiao ( 2017-09-03 13:56:41 -0600 )edit

you are welcome

sturkmen gravatar imagesturkmen ( 2017-09-03 14:20:16 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2017-09-03 06:42:22 -0600

Seen: 1,047 times

Last updated: Sep 03 '17