我想在同一窗口中显示2,3或更多图像。
我的问题是如何将第二张,第三张图像放在主图像的右侧(上方,左侧或上方)。
我想使用OpenCV创建这样的东西。
---------------
| | |
| | |
---------------
| | |
| | |
---------------
提前致谢 乔治
答案 0 :(得分:19)
我最近实施了这个。所以想分享它。 它使用C ++ API。代码是不言自明的(希望如此)。
/**
* @brief makeCanvas Makes composite image from the given images
* @param vecMat Vector of Images.
* @param windowHeight The height of the new composite image to be formed.
* @param nRows Number of rows of images. (Number of columns will be calculated
* depending on the value of total number of images).
* @return new composite image.
*/
cv::Mat makeCanvas(std::vector<cv::Mat>& vecMat, int windowHeight, int nRows) {
int N = vecMat.size();
nRows = nRows > N ? N : nRows;
int edgeThickness = 10;
int imagesPerRow = ceil(double(N) / nRows);
int resizeHeight = floor(2.0 * ((floor(double(windowHeight - edgeThickness) / nRows)) / 2.0)) - edgeThickness;
int maxRowLength = 0;
std::vector<int> resizeWidth;
for (int i = 0; i < N;) {
int thisRowLen = 0;
for (int k = 0; k < imagesPerRow; k++) {
double aspectRatio = double(vecMat[i].cols) / vecMat[i].rows;
int temp = int( ceil(resizeHeight * aspectRatio));
resizeWidth.push_back(temp);
thisRowLen += temp;
if (++i == N) break;
}
if ((thisRowLen + edgeThickness * (imagesPerRow + 1)) > maxRowLength) {
maxRowLength = thisRowLen + edgeThickness * (imagesPerRow + 1);
}
}
int windowWidth = maxRowLength;
cv::Mat canvasImage(windowHeight, windowWidth, CV_8UC3, Scalar(0, 0, 0));
for (int k = 0, i = 0; i < nRows; i++) {
int y = i * resizeHeight + (i + 1) * edgeThickness;
int x_end = edgeThickness;
for (int j = 0; j < imagesPerRow && k < N; k++, j++) {
int x = x_end;
cv::Rect roi(x, y, resizeWidth[k], resizeHeight);
cv::Size s = canvasImage(roi).size();
// change the number of channels to three
cv::Mat target_ROI(s, CV_8UC3);
if (vecMat[k].channels() != canvasImage.channels()) {
if (vecMat[k].channels() == 1) {
cv::cvtColor(vecMat[k], target_ROI, CV_GRAY2BGR);
}
} else {
vecMat[k].copyTo(target_ROI);
}
cv::resize(target_ROI, target_ROI, s);
if (target_ROI.type() != canvasImage.type()) {
target_ROI.convertTo(target_ROI, canvasImage.type());
}
target_ROI.copyTo(canvasImage(roi));
x_end += resizeWidth[k] + edgeThickness;
}
}
return canvasImage;
}
这是示例输出。
答案 1 :(得分:15)
答案 2 :(得分:7)
答案取决于您使用的接口(C或C ++)。一般工作流程
cv::Mat
,C版为IplImage*
),足以容纳您的合成图像Mat::Mat(const Mat& m, const Range& rowRange, const Range& colRange)
构造函数获取指向原始窗口子图像的cv::Mat
,然后使用copyTo
方法将小图像复制到大图像中答案 3 :(得分:7)
或者只是使用:
Mat a, Mat b, Mat dst // a,b loaded
cv::hconcat(a, b, dst) // horizontal
cv::vconcat(a, b, dst) // vertical
Mat dst - &gt; | a | b |
或用矢量:
std::vector<cv::Mat> matrices = {
a, b
};
hconcat(matrices, dst);
答案 4 :(得分:0)
尝试此代码(请参阅我的评论):
Mat img = imread("lena.JPG");
CV::Mat chann[3], all; // creating
split(img, chann); // split an image into their color channel and n keep them inside
a 3 element array called chann
imshow("ppl", img);
hconcat(chann, 3, all); // joining the images together in a horizontal manner, the
array, number of array, and the destination
imshow("B :: G :: R",all); // this just the little help i could provide
答案 5 :(得分:-1)
OpenCV附带的GUI非常有限,如果你需要做任何复杂的事情,你真的应该在Windows上使用GUI框架,如QT或VC ++