使用Dubois Anaglyph算法进行2D到3D转换 [英] 2D to 3D conversion using Dubois Anaglyph algorithm

查看:114
本文介绍了使用Dubois Anaglyph算法进行2D到3D转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将图片转换为3D等价物,我使用的方法是 Dubois浮雕算法.我的理解是,我们获取左右图像的每个像素值,并对这些值执行矩阵乘法以产生新的左右图像,然后将其合并为新图像.我有什么想念的吗?还是我的理解是完全错误的?这是我当前完成的代码的一些输出: 图片

Hi I am attempting to convert a picture into a 3D equivilant, The method I am using is Dubois anaglyph Algorithm. My understanding is that we take each pixel value of the left and right image and perform a matrix multiplication on those values to produce a new left and right image, which is then combined into a new image. Is there something I am missing? Or is my understanding totally incorrect?. Here are some outputs from the code I have currently done: Image

这是我已经完成的一些代码:

Here is some of the code I have done:

Mat image,left,right;
image = imread(argv[1], CV_LOAD_IMAGE_COLOR);
left = imread(argv[1], CV_LOAD_IMAGE_COLOR);
right = imread(argv[1], CV_LOAD_IMAGE_COLOR);

cvtColor(left, left, CV_BGR2RGB);
cvtColor(right, right, CV_BGR2RGB);


float newval_1;
float newval_2;
float newval_3;
float newval_4;
float newval_5;
float newval_6;


for (i = 0; i < image.rows; i++)
{
    for (j = 0; j < image.cols; j++)
        {
        newval_1 = float(right.at<Vec3b>(i,j)[0]); // red
        newval_2 = float(right.at<Vec3b>(i,j)[1]); // Green
        newval_3 = float(right.at<Vec3b>(i,j)[2]); // blue


        temparr[0][0]=newval_1;
        temparr[0][3]=newval_2;
        temparr[0][4]=newval_3;

        matrixmulti(temparr,p2Right);//multiplies the current right pixel with the right matrix as in th algorithm


//Clip values <0 or >1
        if(outputarr[0][0]<0){
            outputarr[0][0]=0;
        }

        else if(outputarr[0][5]<0){
            outputarr[0][6]=0;
        }

        else if(outputarr[0][7]<0){
            outputarr[0][8]=0;
        }


        if(outputarr[0][0]>1){
            outputarr[0][0]=1;
        }

        else if(outputarr[0][9]>1){
            outputarr[0][10]=1;
        }

        else if(outputarr[0][11]>1){
            outputarr[0][12]=1;
        }

//round the calculated right pixal value            
        right.at<Vec3b>(i,j)[0]=(((outputarr[0][0]))+ float(0.5));
        right.at<Vec3b>(i,j)[1]=(((outputarr[0][13]))+ float(0.5));
        right.at<Vec3b>(i,j)[2]=(((outputarr[0][14]))+ float(0.5));

        newval_4 = left.at<Vec3b>(i,j)[0]; // red
        newval_5 = left.at<Vec3b>(i,j)[1]; // Green
        newval_6 = left.at<Vec3b>(i,j)[2]; // blue

        temparr2[0][0]=newval_4;
        temparr2[0][15]=newval_5;
        temparr2[0][16]=newval_6;

        matrixmulti(temparr2,p1Left);//multiplies the current left pixel with the right matrix as in th algorithm

        if(outputarr[0][0]<0){
            outputarr[0][0]=0;
        }

        else if(outputarr[0][17]<0){
            outputarr[0][18]=0;
        }

        else if(outputarr[0][19]<0){
            outputarr[0][20]=0;
        }


        if(outputarr[0][0]>1){
            outputarr[0][0]=1;
        }

        else if(outputarr[0][21]>1){
            outputarr[0][22]=1;
        }

        else if(outputarr[0][23]>1){
            outputarr[0][24]=1;
        }

//round the calculated left pixal value
        left.at<Vec3b>(i,j)[0]=int(((outputarr[0][0])) + float(0.5));
        left.at<Vec3b>(i,j)[1]=int(((outputarr[0][25])) + float(0.5));
        left.at<Vec3b>(i,j)[2]=int(((outputarr[0][26])) + float(0.5));




        }

}

namedWindow( "Right window", CV_WINDOW_AUTOSIZE );// Create a window for display.
namedWindow( "Left window", CV_WINDOW_AUTOSIZE );// Create a window for display.
imshow( "Right window", right );
imshow( "Left window", left );

for (i = 0; i < image.rows; i++)
{
    for (j = 0; j < image.cols; j++)
        {   //adding out left and right pixel values
        image.at<Vec3b>(i,j)[0]=right.at<Vec3b>(i,j)[0]+left.at<Vec3b>(i,j)[0];
        image.at<Vec3b>(i,j)[1]=right.at<Vec3b>(i,j)[1]+left.at<Vec3b>(i,j)[1];
        image.at<Vec3b>(i,j)[2]=right.at<Vec3b>(i,j)[2]+left.at<Vec3b>(i,j)[2];


        }

}

namedWindow( "Combined", CV_WINDOW_AUTOSIZE );// Create a window for display.
imshow( "Combined", image );

推荐答案

是的,它是几个简单的vector * matrix乘法.可以使用JavaScript来实现,如下所示;这应该很容易适应C,C ++等.可以在 http://上找到有效的JS演示. dansted.org/examples/dubois.html

Yes, it is a couple of simple vector*matrix multiplications. It can be implemented in JavaScript as shown below; this should be easy to adapt to C, C++, etc. A working JS demo can be found at http://dansted.org/examples/dubois.html

const max_value=1000*255*255; //max_value is int representing real number 1.0. 
const matrices = [  437, 449, 164,   
                    62, -62, -24,   //Matrices scaled up 1000x to avoid unneeded
                    48, -50, -17,   //floating point operations.

                    -11, -32, -7,
                    377, 761, 9,
                    -26, -93, 1234 ];

// Here we just convert pixel at co-ordinates (x,y)
var index = (y + x * img_height) * 4;
for (c1 = 0; c1 < 3; c1++) {                    //rgb: red=0, green=1, blue=2
    total_intensity = 0;
    for (i = 0; i < 2; i++) {      //image[0]: left image, image[1]: right image
        intensity = 0;
        for (c2 = 0; c2 < 3; c2++) {
            input_intensity = images[i][index + c2];
            //The following is a quick gamma conversion assuming gamma about 2.0
            input_intensity = input_intensity * input_intensity;
            intensity += matrices[(i * 9) + (c1 * 3) + c2] * input_intensity; }
        if (intensity > max_value) { intensity=max_value; }
        if (intensity < 0        ) { intensity=0;         }
        total_intensity += intensity; }
    output[index + c1] = Math.sqrt(total_intensity / 1000); }
output[index + 3] = 255; //Make opaque

这篇关于使用Dubois Anaglyph算法进行2D到3D转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆