c++ - Create a single image from images array -
hi i'm trying create single image multiple images in opencv. images use same size.
what reshaping them single line , try merge them new image.
i create new image size of 2 images , pass array recieve error exc_bad_access(code=1, address = ..)
note: sizes of images correct size of single image : [170569 x 1] size of new_image : [170569 x 2]
my code below. thank you
int main(){ mat image[2]; image[0]= imread("image1.jpg",0); image[1]= imread("image2.jpg",0); image[0] = image[0].reshape(0, 1); //single line image[1] = image[1].reshape(0, 1); //single line int size = sizeof(image)/sizeof(mat); mat new_image(image[0].cols,size,cv_32fc1,image); }
if understand need concatenate 2 image of same size 1 mat. wrote quick code perform task.
u can change argument function pointer , add other handlers care variant size image.
#include <iostream> #include <opencv2/opencv.hpp> #include <opencv2/highgui/highgui.hpp> cv::mat cvconcatenatemat(const cv::mat &image1, const cv::mat &image2, bool iscol cv_default(true)){ if (iscol) { cv::mat mergemat = cv::mat(image1.rows, image1.cols + image2.cols, image1.type()); (int j = 0; j < image1.rows; j++) { (int = 0; < image1.cols; i++) { mergemat.at<cv::vec3b>(j,i) = image1.at<cv::vec3b>(j,i); } (int = image1.cols; < mergemat.cols; i++) { mergemat.at<cv::vec3b>(j,i) = image2.at<cv::vec3b>(j,i); } } return mergemat; } else { cv::mat mergemat = cv::mat(image1.rows + image2.rows, image1.cols, image1.type()); (int j = 0; j < image1.cols; j++) { (int = 0; < image1.rows; i++) { mergemat.at<cv::vec3b>(i,j) = image1.at<cv::vec3b>(i,j); } (int = image1.rows; < mergemat.rows; i++) { mergemat.at<cv::vec3b>(i,j) = image2.at<cv::vec3b>(i-image1.rows,j); } } return mergemat; } } int main(int argc, const char * argv[]) { cv::mat image1 = cv::imread("img1.jpg"); cv::mat image2 = cv::imread("img2.jpg"); cv::resize(image2, image2, image1.size()); cv::mat outimage = cvconcatenatemat(image1, image2, false); cv::imshow("out image", outimage); cv::waitkey(0); return 0;
}
Comments
Post a Comment