BugReader
BugReader

Reputation: 91

Show two images overlapping each other in openCV

In Matlab there is a possibility to show two images overlapping each other, which is quite useful to show how two images are aligned with respect to each other.

For instance look to following code:

img1 = zeros(100,100);
img1(20:50, 10:40) = 255;
img2 = zeros(100, 100);
img2(35:65, 35:65) = 255;
imshowpair(img1, img2);

which creates following image:

enter image description here

Is there any built-in function or any way in openCV library to do so (in c++)?

Upvotes: 1

Views: 3919

Answers (3)

BugReader
BugReader

Reputation: 91

This is the code which is working when you want to use openCV library only in debug mode:

void showImagePairs(const cv::Mat &img1, const cv::Mat &img2) {

    std::vector<cv::Mat> channels;
    cv::Mat imgPair;

    channels.push_back(img2);
    channels.push_back(img1);
    channels.push_back(img2);

    cv::merge(&channels[0], channels.size(), imgPair);

    cv::imshow("imgPair", imgPair);

    int c = cvWaitKey(40);

}

Upvotes: 0

zeFrenchy
zeFrenchy

Reputation: 6591

You want to use addWeighted as documented here

Mat img1 = Mat(100,100,CV_8UC3,Scalar::all(0));
Mat img2 = Mat(100,100,CV_8UC3,Scalar::all(0));
rectangle(img1, Rect(20,10,50,40), Scalar(0,255,0), -1);
rectangle(img2, Rect(35,35,65,65), Scalar(255,0,255), -1);
Mat result;
addWeighted(img1, 0.5, img2, 0.5, 0.0, result);
imshow("SO question", result);
waitKey(10);

Upvotes: 3

beaker
beaker

Reputation: 16791

I haven't used imshowpair so I don't know the full range of what it does, but in this case it looks like it's simply taking the first image as the green channel and the second image as the blue and red channels:

cv::Mat img1 = cv::Mat::zeros(100, 100, CV_8U);
rectangle(img1, cv::Rect(10,20,30,30), 255, CV_FILLED);

cv::Mat img2 = cv::Mat::zeros(100, 100, CV_8U);
rectangle(img2, cv::Rect(35,35,30,30), 255, CV_FILLED);

std::vector<cv::Mat> channels;
cv::Mat imgPair;

channels.push_back(img2);
channels.push_back(img1);
channels.push_back(img2);

merge(channels, imgPair);

imshow("imgPair", imgPair);
cv::waitKey(0);

Output:

imgPair result

Upvotes: 2

Related Questions