I'm new to opencv and i searched on the internet if there is an example of how to merge two images, but didin't found anything good to help me. Can someone help me with some indications or a small code to understand ? thanks in advance
From the comments to the question, you said:
I dont want to blend half from the first picture with the other half from the second. I just waint to print both images, one near the other one
So, starting from these images:
You want this result?
Note that if both images have the same height, you won't see the black background.
Code:
#include <opencv2\opencv.hpp>
using namespace cv;
int main()
{
// Load images
Mat3b img1 = imread("path_to_image_1");
Mat3b img2 = imread("path_to_image_2");
// Get dimension of final image
int rows = max(img1.rows, img2.rows);
int cols = img1.cols + img2.cols;
// Create a black image
Mat3b res(rows, cols, Vec3b(0,0,0));
// Copy images in correct position
img1.copyTo(res(Rect(0, 0, img1.cols, img1.rows)));
img2.copyTo(res(Rect(img1.cols, 0, img2.cols, img2.rows)));
// Show result
imshow("Img 1", img1);
imshow("Img 2", img2);
imshow("Result", res);
waitKey();
return 0;
}
I have concatenated vertically two image in C# (OpenCvSharp) : (images can be different size)
private Mat VerticalConcat(Mat image1, Mat image2)
{
var smallImage = image1.Cols < image2.Cols ? image1 : image2;
var bigImage = image1.Cols > image2.Cols ? image1 : image2;
Mat combine = Mat.Zeros(new OpenCvSharp.CPlusPlus.Size(Math.Abs(image2.Cols - image1.Cols), smallImage.Height), image2.Type());
Cv2.HConcat(smallImage, combine, combine);
Cv2.VConcat(bigImage, combine, combine);
return combine;
}
Related
Good Day! I'm using imwrite command to save the image below after cropping them in OpenCV (C++) but it seems like it included the black portion surrounding it in writing. All I want is to save the cropped one. Please help.
Here's my code
Mat mask,draft,res;
int nPixels;
char c=0;
while(true && c!='q') {
imshow("SAMPLE", img);
if(!roi.isSet())
roi.set("SAMPLE");
if (roi.isSet()) {
roi.createMask(img.size());
mask = roi.getMask();
res = mask & img.clone();
imwrite("masked.png",res);
imshow("draft", res);
}
c = waitKey(1);
}
Here is an example how to crop an image and save the croped image (see comment from api55). Maybe that helps you.
cv::Mat img = cv::imread("Path/To/Image/image.png", cv::IMREAD_GRAYSCALE);
if(image.empty())
return -1;
cv::Rect roi(0, 0, 100, 100); // define roi here as x0, y0, width, height
cv::Mat cropedImg(img, roi);
cv::imwrite("Path/To/Save/Location/cropedImage.png", cropedImg);
If I have mask like
And I have a image( the size is same to the mask) like
I want to hightlight the mask in the image. If I'm in other Language,I just
As you can see, the result image have a transparent red show the mask. I hope implement this in OpenCV. So I write this code
#include <opencv.hpp>
using namespace cv;
using namespace std;
int main() {
Mat srcImg = imread("image.jpg");
Mat mask = imread("mask.jpg", IMREAD_GRAYSCALE)>200;
for(int i=0;i<srcImg.rows;i++)
for(int j=0;j<srcImg.cols;j++)
if(mask.at<uchar>(i, j)==255)
circle(srcImg, Point(j,i), 3, Scalar(0, 0, 128,128));
imshow("image",srcImg);
waitKey();
return 0;
}
But as you see, I use a alpha value in Scalar, but it is not a transparent red.
Maybe this is due to the srcImg just have 3 channels. I have two question about this
How to hightlight the mask with a transparent red(even the image just have 3 channels)?
I have to draw circle pixel by pixel to do this thing?
#include<opencv2/core.hpp>
#include<opencv2/imgproc.hpp>
#include<opencv2/highgui.hpp>
using namespace cv;
int main(int argc, char** argv)
{
Mat srcImg = imread("image.png");
Mat mask = imread("mask.png", IMREAD_GRAYSCALE) > 200;
Mat red;
cvtColor(mask, red, COLOR_GRAY2BGR);
red = (red - Scalar(0, 0, 255)) / 2;
srcImg = srcImg - red;
imshow("image", srcImg);
waitKey();
return 0;
}
I've written this in python but you can easily port it to C++. Assuming that your source and mask images are CV_8UC3 images:
src = cv2.imread("source.png", -1)
mask = cv2.imread("mask.png", -1)
# convert mask to gray and then threshold it to convert it to binary
gray = cv2.cvtColor(mask, cv2.COLOR_BGR2GRAY)
ret, binary = cv2.threshold(gray, 40, 255, cv2.THRESH_BINARY)
# find contours of two major blobs present in the mask
im2,contours,hierarchy = cv2.findContours(binary, cv2.RETR_LIST, cv2.CHAIN_APPROX_NONE)
# draw the found contours on to source image
for contour in contours:
cv2.drawContours(src, contour, -1, (255,0,0), thickness = 1)
# split source to B,G,R channels
b,g,r = cv2.split(src)
# add a constant to R channel to highlight the selected area in reed
r = cv2.add(b, 30, dst = b, mask = binary, dtype = cv2.CV_8U)
# merge the channels back together
cv2.merge((b,g,r), src)
I have a question which i am unable to resolve. I am taking difference of two images using OpenCV. I am getting output in a seperate Mat. Difference method used is the AbsDiff method. Here is the code.
char imgName[15];
Mat img1 = imread(image_path1, COLOR_BGR2GRAY);
Mat img2 = imread(image_path2, COLOR_BGR2GRAY);
/*cvtColor(img1, img1, CV_BGR2GRAY);
cvtColor(img2, img2, CV_BGR2GRAY);*/
cv::Mat diffImage;
cv::absdiff(img2, img1, diffImage);
cv::Mat foregroundMask = cv::Mat::zeros(diffImage.rows, diffImage.cols, CV_8UC3);
float threshold = 30.0f;
float dist;
for(int j=0; j<diffImage.rows; ++j)
{
for(int i=0; i<diffImage.cols; ++i)
{
cv::Vec3b pix = diffImage.at<cv::Vec3b>(j,i);
dist = (pix[0]*pix[0] + pix[1]*pix[1] + pix[2]*pix[2]);
dist = sqrt(dist);
if(dist>threshold)
{
foregroundMask.at<unsigned char>(j,i) = 255;
}
}
}
sprintf(imgName,"D:/outputer/d.jpg");
imwrite(imgName, diffImage);
I want to bound the difference part in a rectangle. findContours is drawing too many contours. but i only need a particular portion. My diff image is
I want to draw a single rectangle around all the five dials.
Please point me to right direction.
Regards,
I would search for the highest value for i index giving a non black pixel; that's the right border.
The lowest non black i is the left border. Similar for j.
You can:
binarize the image with a threshold. Background will be 0.
Use findNonZero to retrieve all points that are not 0, i.e. all foreground points.
use boundingRect on the retrieved points.
Result:
Code:
#include <opencv2/opencv.hpp>
using namespace cv;
int main()
{
// Load image (grayscale)
Mat1b img = imread("path_to_image", IMREAD_GRAYSCALE);
// Binarize image
Mat1b bin = img > 70;
// Find non-black points
vector<Point> points;
findNonZero(bin, points);
// Get bounding rect
Rect box = boundingRect(points);
// Draw (in color)
Mat3b out;
cvtColor(img, out, COLOR_GRAY2BGR);
rectangle(out, box, Scalar(0,255,0), 3);
// Show
imshow("Result", out);
waitKey();
return 0;
}
Find contours, it will output a set of contours as std::vector<std::vector<cv::Point> let us call it contours:
std::vector<cv::Point> all_points;
size_t points_count{0};
for(const auto& contour:contours){
points_count+=contour.size();
all_points.reserve(all_points);
std::copy(contour.begin(), contour.end(),
std::back_inserter(all_points));
}
auto bounding_rectnagle=cv::boundingRect(all_points);
I have tried this code I gotten here. Its for displaying multiple images on a single window for C++. I have included the opencv 3.0 library on the program as well. Below is the code. I am trying to load 2 images but only the first one (1.jpg) appears but when i put image2 to be equal to cv::imread("1.jpg"); two images of 1.jpg appears. I am really new to this and I dont understand where i am going wrong here. I hope someone can help me. Thank you.
int main(int argc, char *argv[])
{
// read an image
cv::Mat image1= cv::imread("1.jpg");
cv::Mat image2= cv::imread("2.jpg");
int dstWidth = image1.cols;
int dstHeight = image1.rows * 2;
cv::Mat dst = cv::Mat(dstHeight, dstWidth, CV_8UC3, cv::Scalar(0,0,0));
cv::Rect roi(cv::Rect(0,0,image1.cols, image1.rows));
cv::Mat targetROI = dst(roi);
image1.copyTo(targetROI);
targetROI = dst(cv::Rect(0,image1.rows,image1.cols, image1.rows));
image2.copyTo(targetROI);
// create image window named "My Image"
cv::namedWindow("OpenCV Window");
// show the image on window
cv::imshow("OpenCV Window", dst);
// wait key for 5000 ms
cv::waitKey(5000);
return 0;
}
This is the result of the program above
Your code works ok for me, if images have the same size. Otherwise, the call to
image2.copyTo(targetROI);
will copy image2 into a newly created image, not in dst as you would expect.
If you want to make it work in general, you should:
1) to set dstWidth and dstHeight like:
int dstWidth = max(image1.cols, image2.cols);
int dstHeight = image1.rows + image2.rows;
2) set the second ROI with the size of the second image:
targetROI = dst(cv::Rect(0, image1.rows, image2.cols, image2.rows));
// ^ ^
From the comments, to show 4 images disposed as 2x2, you need a little more work:
#include <opencv2\opencv.hpp>
#include <iostream>
using namespace cv;
using namespace std;
int main()
{
// read an image
cv::Mat image1 = cv::imread("path_to_image1");
cv::Mat image2 = cv::imread("path_to_image2");
cv::Mat image3 = cv::imread("path_to_image3");
cv::Mat image4 = cv::imread("path_to_image4");
//////////////////////
// image1 image2
// image3 image4
//////////////////////
int max13cols = max(image1.cols, image3.cols);
int max24cols = max(image2.cols, image4.cols);
int dstWidth = max13cols + max24cols;
int max12rows = max(image1.rows, image2.rows);
int max34rows = max(image3.rows, image4.rows);
int dstHeight = max12rows + max34rows;
cv::Mat dst = cv::Mat(dstHeight, dstWidth, CV_8UC3, cv::Scalar(0, 0, 0));
cv::Rect roi(cv::Rect(0, 0, image1.cols, image1.rows));
image1.copyTo(dst(roi));
roi = cv::Rect(max13cols, 0, image2.cols, image2.rows);
image2.copyTo(dst(roi));
roi = cv::Rect(0, max12rows, image3.cols, image3.rows);
image3.copyTo(dst(roi));
roi = cv::Rect(max13cols, max12rows, image4.cols, image4.rows);
image4.copyTo(dst(roi));
cv::imshow("OpenCV Window", dst);
cv::waitKey(0);
return 0;
}
I create a Bird-View-Image with the warpPerspective()-function like this:
warpPerspective(frame, result, H, result.size(), CV_WARP_INVERSE_MAP, BORDER_TRANSPARENT);
The result looks very good and also the border is transparent:
Bird-View-Image
Now I want to put this image on top of another image "out". I try doing this with the function warpAffine like this:
warpAffine(result, out, M, out.size(), CV_INTER_LINEAR, BORDER_TRANSPARENT);
I also converted "out" to a four channel image with alpha channel according to a question which was already asked on stackoverflow:
Convert Image
This is the code: cvtColor(out, out, CV_BGR2BGRA);
I expected to see the chessboard but not the gray background. But in fact, my result looks like this:
Result Image
What am I doing wrong? Do I forget something to do? Is there another way to solve my problem? Any help is appreciated :)
Thanks!
Best regards
DamBedEi
I hope there is a better way, but here it is something you could do:
Do warpaffine normally (without the transparency thing)
Find the contour that encloses the image warped
Use this contour for creating a mask (white values inside the image warped, blacks in the borders)
Use this mask for copy the image warped into the other image
Sample code:
// load images
cv::Mat image2 = cv::imread("lena.png");
cv::Mat image = cv::imread("IKnowOpencv.jpg");
cv::resize(image, image, image2.size());
// perform warp perspective
std::vector<cv::Point2f> prev;
prev.push_back(cv::Point2f(-30,-60));
prev.push_back(cv::Point2f(image.cols+50,-50));
prev.push_back(cv::Point2f(image.cols+100,image.rows+50));
prev.push_back(cv::Point2f(-50,image.rows+50 ));
std::vector<cv::Point2f> post;
post.push_back(cv::Point2f(0,0));
post.push_back(cv::Point2f(image.cols-1,0));
post.push_back(cv::Point2f(image.cols-1,image.rows-1));
post.push_back(cv::Point2f(0,image.rows-1));
cv::Mat homography = cv::findHomography(prev, post);
cv::Mat imageWarped;
cv::warpPerspective(image, imageWarped, homography, image.size());
// find external contour and create mask
std::vector<std::vector<cv::Point> > contours;
cv::Mat imageWarpedCloned = imageWarped.clone(); // clone the image because findContours will modify it
cv::cvtColor(imageWarpedCloned, imageWarpedCloned, CV_BGR2GRAY); //only if the image is BGR
cv::findContours (imageWarpedCloned, contours, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_NONE);
// create mask
cv::Mat mask = cv::Mat::zeros(image.size(), CV_8U);
cv::drawContours(mask, contours, 0, cv::Scalar(255), -1);
// copy warped image into image2 using the mask
cv::erode(mask, mask, cv::Mat()); // for avoid artefacts
imageWarped.copyTo(image2, mask); // copy the image using the mask
//show images
cv::imshow("imageWarpedCloned", imageWarpedCloned);
cv::imshow("warped", imageWarped);
cv::imshow("image2", image2);
cv::waitKey();
One of the easiest ways to approach this (not necessarily the most efficient) is to warp the image twice, but set the OpenCV constant boundary value to different values each time (i.e. zero the first time and 255 the second time). These constant values should be chosen towards the minimum and maximum values in the image.
Then it is easy to find a binary mask where the two warp values are close to equal.
More importantly, you can also create a transparency effect through simple algebra like the following:
new_image = np.float32((warp_const_255 - warp_const_0) *
preferred_bkg_img) / 255.0 + np.float32(warp_const_0)
The main reason I prefer this method is that openCV seems to interpolate smoothly down (or up) to the constant value at the image edges. A fully binary mask will pick up these dark or light fringe areas as artifacts. The above method acts more like true transparency and blends properly with the preferred background.
Here's a small test program that warps with transparent "border", then copies the warped image to a solid background.
int main()
{
cv::Mat input = cv::imread("../inputData/Lenna.png");
cv::Mat transparentInput, transparentWarped;
cv::cvtColor(input, transparentInput, CV_BGR2BGRA);
//transparentInput = input.clone();
// create sample transformation mat
cv::Mat M = cv::Mat::eye(2,3, CV_64FC1);
// as a sample, just scale down and translate a little:
M.at<double>(0,0) = 0.3;
M.at<double>(0,2) = 100;
M.at<double>(1,1) = 0.3;
M.at<double>(1,2) = 100;
// warp to same size with transparent border:
cv::warpAffine(transparentInput, transparentWarped, M, transparentInput.size(), CV_INTER_LINEAR, cv::BORDER_TRANSPARENT);
// NOW: merge image with background, here I use the original image as background:
cv::Mat background = input;
// create output buffer with same size as input
cv::Mat outputImage = input.clone();
for(int j=0; j<transparentWarped.rows; ++j)
for(int i=0; i<transparentWarped.cols; ++i)
{
cv::Scalar pixWarped = transparentWarped.at<cv::Vec4b>(j,i);
cv::Scalar pixBackground = background.at<cv::Vec3b>(j,i);
float transparency = pixWarped[3] / 255.0f; // pixel value: 0 (0.0f) = fully transparent, 255 (1.0f) = fully solid
outputImage.at<cv::Vec3b>(j,i)[0] = transparency * pixWarped[0] + (1.0f-transparency)*pixBackground[0];
outputImage.at<cv::Vec3b>(j,i)[1] = transparency * pixWarped[1] + (1.0f-transparency)*pixBackground[1];
outputImage.at<cv::Vec3b>(j,i)[2] = transparency * pixWarped[2] + (1.0f-transparency)*pixBackground[2];
}
cv::imshow("warped", outputImage);
cv::imshow("input", input);
cv::imwrite("../outputData/TransparentWarped.png", outputImage);
cv::waitKey(0);
return 0;
}
I use this as input:
and get this output:
which looks like ALPHA channel isn't set to ZERO by warpAffine but to something like 205...
But in general this is the way I would do it (unoptimized)