C#,图片生成裸眼3D立体图像的源代码

C#,图片生成裸眼3D立体图像的源代码_第1张图片

国外找来的,从一张图片生成 裸眼3D 立体图像 的代码。

C#,图片生成裸眼3D立体图像的源代码_第2张图片

using System;
using System.Drawing;

namespace Legalsoft.Truffer.Algorithm
{
    public class StereoBit
    {
        public int prev { get; set; } = 0;
        public int next { get; set; } = 0;
    }

    public class StereoImageBuilder
    {
        private static readonly int RESULT_WIDTH = 800;
        private static readonly int NO_BIT = -1;
        private static readonly int RESULT_HEIGHT = 600;
        private static readonly int BACK_WIDTH = 100;
        private static readonly int EYE_SPACE = 128;

        public static Image Build(Image front, Image back, byte depthCount)
        {
            if (front == null || back == null) return null;

            Bitmap bmpBack = ResizeBitmap(back, BACK_WIDTH, RESULT_HEIGHT);
            Bitmap bmpFront = ResizeBitmap(front, RESULT_WIDTH, RESULT_HEIGHT);
            Bitmap bmpResult = new Bitmap(RESULT_WIDTH, RESULT_HEIGHT);
            byte[][] depthVector = new byte[RESULT_HEIGHT][];
            for (int i = 0; i < RESULT_HEIGHT; i++)
            {
                depthVector[i] = new byte[RESULT_WIDTH];
            }
            CalcDepthVector(depthVector, bmpFront, depthCount);
            StereoBit[] bits = new StereoBit[RESULT_WIDTH];
            for (int i = 0; i < RESULT_HEIGHT; i++)
            {
                CalcEqualColorLine(depthVector[i], bits, depthCount);
                ColorUpResult(bits, bmpResult, bmpBack, i);
            }
            return Image.FromHbitmap(bmpResult.GetHbitmap());
        }

        private static Bitmap ResizeBitmap(Image image, int width, int height)
        {
            return new Bitmap(image, width, height);
        }

        private static void CalcDepthVector(byte[][] depthVector, Bitmap bmpFront, byte depthCount)
        {
            for (int i = 0; i < RESULT_HEIGHT; i++)
            {
                for (int j = 0; j < RESULT_WIDTH; j++)
                {
                    depthVector[i][j] = (byte)(CalcGray(bmpFront.GetPixel(j, i)) * depthCount / 255);
                }
            }
        }

        private static void CalcEqualColorLine(byte[] depthVector, StereoBit[] bits, byte depthCount)
        {
            for (int i = 0; i < bits.Length; i++)
            {
                bits[i].prev = NO_BIT;
                bits[i].next = NO_BIT;
                if (i + BACK_WIDTH < bits.Length)
                    bits[i].next = i + BACK_WIDTH;
                if (i - BACK_WIDTH >= 0)
                    bits[i].prev = i - BACK_WIDTH;
            }

            for (int layer = 0; layer < depthCount; layer++)
            {
                for (int x = 0; x < RESULT_WIDTH; x++)
                {
                    int left_x = x - EYE_SPACE / 2 + layer / 2;
                    int right_x = x + EYE_SPACE / 2 - (layer + 1) / 2;
                    if (depthVector[x] == layer && left_x >= 0 && right_x < RESULT_WIDTH)
                    {
                        if (bits[left_x].next != NO_BIT) bits[bits[left_x].next].prev = NO_BIT;
                        bits[left_x].next = right_x;
                        if (bits[right_x].prev != NO_BIT) bits[bits[right_x].prev].next = NO_BIT;
                        bits[right_x].prev = left_x;
                    }
                }
            }
        }

        private static void ColorUpResult(StereoBit[] bits, Bitmap bmpResult, Bitmap bmpBack, int y)
        {
            for (int x = 0; x < RESULT_WIDTH; x++)
            {
                if (bits[x].prev == NO_BIT)
                {
                    bmpResult.SetPixel(x, y, bmpBack.GetPixel(x % BACK_WIDTH, y));
                    int temp = x;
                    while (bits[temp].next != NO_BIT)
                    {
                        temp = bits[temp].next;
                        bmpResult.SetPixel(temp, y, bmpBack.GetPixel(x % BACK_WIDTH, y));
                    }
                }
            }
        }

        private static byte CalcGray(Color c)
        {
            return (byte)(0.299 * c.R + 0.587 * c.G + 0.114 * c.B);
        }
    }
}

C#,图片生成裸眼3D立体图像的源代码_第3张图片

——————————————————————

POWER BY 315SOFT.COM &
TRUFFER.CN

你可能感兴趣的:(C#实用代码,Coding,Recipes,c#,算法,裸眼3D,3D,立体图)