0

我正在尝试使用 cvMerge() 合并 8 个图像,但它不起作用。我应该怎么做才能合并这 8 个图像?

下面是查看图像不同位平面的代码。并将所有 8 张图像组合起来,就像输入的图像一样。

#include <iostream>
#include <cv.h>
#include <highgui.h>
#include "rgb.h"
#include <cmath>
using namespace std;

int T(int r)
{
if (r == 0)
return 0;
else
return 255;
}

int main()
{
char infname[256];
cout << "Enter input image : ";
cin >> infname;
IplImage *mono = cvLoadImage(infname, 0);
BwImage pic(mono);
int H = mono->height;
int W = mono->width;
IplImage *out0 = cvCreateImage(cvSize(W,H), IPL_DEPTH_8U, 1);
IplImage *out1 = cvCreateImage(cvSize(W,H), IPL_DEPTH_8U, 1);
IplImage *out2 = cvCreateImage(cvSize(W,H), IPL_DEPTH_8U, 1);
IplImage *out3 = cvCreateImage(cvSize(W,H), IPL_DEPTH_8U, 1);
IplImage *out4 = cvCreateImage(cvSize(W,H), IPL_DEPTH_8U, 1);
IplImage *out5 = cvCreateImage(cvSize(W,H), IPL_DEPTH_8U, 1);
IplImage *out6 = cvCreateImage(cvSize(W,H), IPL_DEPTH_8U, 1);
IplImage *out7 = cvCreateImage(cvSize(W,H), IPL_DEPTH_8U, 1);
IplImage *output1 = cvCreateImage(cvSize(W,H), IPL_DEPTH_8U, 4);
IplImage *output2 = cvCreateImage(cvSize(W,H), IPL_DEPTH_8U, 4);
IplImage *output = cvCreateImage(cvSize(W,H), IPL_DEPTH_8U, 2);

BwImage bp0(out0);
BwImage bp1(out1);
BwImage bp2(out2);
BwImage bp3(out3);
BwImage bp4(out4);
BwImage bp5(out5);
BwImage bp6(out6);
BwImage bp7(out7);
BwImage b1(output1);
BwImage b2(output2);
BwImage b(output);

for (int y = 0; y < H; y++)
for (int x = 0; x < W; x++){
bp0[y][x] = T(pic[y][x] % 2);
bp1[y][x] = T((pic[y][x] / 2) % 2);
bp2[y][x] = T((pic[y][x] / 4) % 2);
bp3[y][x] = T((pic[y][x] / 8) % 2);
bp4[y][x] = T((pic[y][x] / 16) % 2);
bp5[y][x] = T((pic[y][x] / 32) % 2);
bp6[y][x] = T((pic[y][x] / 64) % 2);
bp7[y][x] = T((pic[y][x] / 128) % 2);
}

// here's the pronlem. i guess

cvMerge(out0,out1,out2,out3,output1);
cvMerge(output1,out4,out5,out6,output2);
cvMerge(output2,out7,NULL,NULL,output);

cvWaitKey(0);
cvSaveImage("b0.png", out0);
cvSaveImage("b1.png", out1);
cvSaveImage("b2.png", out2);
cvSaveImage("b3.png", out3);
cvSaveImage("b4.png", out4);
cvSaveImage("b5.png", out5);
cvSaveImage("b6.png", out6);
cvSaveImage("b7.png", out7);
cvSaveImage("b8.png", output);
cvReleaseImage(&mono);
cvReleaseImage(&out0);
cvReleaseImage(&out1);
cvReleaseImage(&out2);
cvReleaseImage(&out3);
cvReleaseImage(&out4);
cvReleaseImage(&out5);
cvReleaseImage(&out6);
cvReleaseImage(&out7);
cvReleaseImage(&output);
return 0;
}
4

1 回答 1

0

我的猜测是,除非您有 RGBA 格式的输出图像,否则通道数必须为 3,而不是您输入的 2 或 4:

IplImage *output2 = cvCreateImage(cvSize(W,H), IPL_DEPTH_8U, 4);
IplImage *output = cvCreateImage(cvSize(W,H), IPL_DEPTH_8U, 2);

尝试将其设置为 3 并一次仅合并 3 个图像。但我不认为合并可以将 3 通道图像作为输入......然后cvtColor(src, src, CV_BGR2GRAY);在合并之前使用(它将对 3 通道的值进行平均每个像素)。

于 2013-03-06T14:32:38.763 回答