我正在尝试使用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;
}
答案 0 :(得分: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个通道的每个像素的平均值)。