繁体   English   中英

使用unity 3D进行动态墙建模

[英]Dynamic wall modeling using unity 3D

我有一个python代码从图像(简单的房屋平面图)中获取坐标。它将所有坐标保存到文本文件中。我在下面提供了上述代码。

import cv2
import numpy as np
import matplotlib.pyplot as plt

filename = 'Plans.jpg'
img = cv2.imread(filename)
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)

gray = np.float32(gray)
dst = cv2.cornerHarris(gray,2,3,0.04)

#result is dilated for marking the corners, not important
dst = cv2.dilate(dst,None)

# Threshold for an optimal value, it may vary depending on the image.
img[dst>0.01*dst.max()]=[0,0,255]


coord = np.where(np.all(img == (0, 0, 255), axis=-1))
lol = zip(coord[0], coord[1])
x, y = coord[0], coord[1]

file = open("coordinates.txt", "w")

file.write(str(lol))
file.close()

plt.scatter(coord[0], coord[1])
plt.show()

这是我使用的图像(房屋平面图)。

在此处输入图片说明

执行以上代码后,所有坐标保存如下

[(88, 67), (88, 68), (88, 69), (88, 70), (88, 252), (88, 253), (88, 254), (88, 255), (88, 459), (88, 460), (88, 461), (88, 462), (88, 760), (88, 761), (88, 762), (88, 763), (88, 972), (88, 973), (88, 974), (88, 975), (88, 1148), (88, 1149), (88, 1150), (88, 1151), (89, 67), (89, 68), (89, 69), (89, 70), (89, 252), (89, 253), (89, 254), (89, 255), (89, 459), (89, 460), (89, 461), (89, 462), (89, 760), (89, 761), (89, 762), (89, 763), (89, 972), (89, 973), (89, 974), (89, 975), (89, 1148), (89, 1149), (89, 1150), (89, 1151), (90, 67), (90, 68), (90, 69), (90, 70), (90, 252), (90, 253), (90, 254), (90, 255), (90, 459), (90, 460), (90, 461), (90, 462), (90, 760), (90, 761), (90, 762), (90, 763), (90, 972), (90, 973), (90, 974), (90, 975), (90, 1148), (90, 1149), (90, 1150), (90, 1151), (91, 67), (91, 68), (91, 69), (91, 70), (91, 252), (91, 253), (91, 254), (91, 255), (91, 459), (91, 460), (91, 461), (91, 462), (91, 760), (91, 761), (91, 762), (91, 763), (91, 972), (91, 973), (91, 974), (91, 975), (91, 1148), (91, 1149), (91, 1150), (91, 1151), (112, 252), (112, 253), (112, 254), (112, 255), (112, 459), (112, 460), (112, 461), (112, 462), (112, 760), (112, 761), (112, 762), (112, 763), (112, 972), (112, 973), (112, 974), (112, 975), (113, 91), (113, 92), (113, 93), (113, 94), (113, 252), (113, 253), (113, 254), (113, 255), (113, 459), (113, 460), (113, 461), (113, 462), (113, 594), (113, 595), (113, 596), (113, 597), (113, 619), (113, 620), (113, 621), (113, 622), (113, 760), (113, 761), (113, 762), (113, 763), (113, 972), (113, 973), (113, 974), (113, 975), (113, 1124), (113, 1125), (113, 1126), (113, 1127), (114, 91), (114, 92), (114, 93), (114, 94), (114, 252), (114, 253), (114, 254), (114, 255), (114, 459), (114, 460), (114, 461), (114, 462), (114, 594), (114, 595), (114, 596), (114, 597), (114, 619), (114, 620), (114, 621), (114, 622), (114, 760), (114, 761), (114, 762), (114, 763), (114, 972), (114, 973), (114, 974), (114, 975), (114, 1124), (114, 1125), (114, 1126), (114, 1127), (115, 91), (115, 92), (115, 93), (115, 94), (115, 252), (115, 253), (115, 254), (115, 255), (115, 459), (115, 460), (115, 461), (115, 462), (115, 594), (115, 595), (115, 596), (115, 597), (115, 619), (115, 620), (115, 621), (115, 622), (115, 760), (115, 761), (115, 762), (115, 763), (115, 972), (115, 973), (115, 974), (115, 975), (115, 1124), (115, 1125), (115, 1126), (115, 1127), (116, 91), (116, 92), (116, 93), (116, 94), (116, 594), (116, 595), (116, 596), (116, 597), (116, 619), (116, 620), (116, 621), (116, 622), (116, 1124), (116, 1125), (116, 1126), (116, 1127), (269, 1), (269, 2), (269, 3), (270, 0), (270, 1), (270, 2), (270, 3), (270, 4), (271, 0), (271, 1), (271, 2), (271, 3), (271, 4), (272, 0), (272, 1), (272, 2), (272, 3), (272, 4), (273, 0), (273, 1), (273, 2), (273, 3), (490, 1149), (490, 1150), (490, 1151), (490, 1152), (491, 1149), (491, 1150), (491, 1151), (491, 1152), (491, 1305), (491, 1306), (491, 1307), (491, 1308), (492, 1149), (492, 1150), (492, 1151), (492, 1152), (492, 1305), (492, 1306), (492, 1307), (492, 1308), (493, 1149), (493, 1150), (493, 1151), (493, 1152), (493, 1305), (493, 1306), (493, 1307), (493, 1308), (494, 1305), (494, 1306), (494, 1307), (494, 1308), (515, 1305), (515, 1306), (515, 1307), (515, 1308), (516, 1149), (516, 1150), (516, 1151), (516, 1152), (516, 1305), (516, 1306), (516, 1307), (516, 1308), (517, 1149), (517, 1150), (517, 1151), (517, 1152), (517, 1305), (517, 1306), (517, 1307), (517, 1308), (518, 1149), (518, 1150), (518, 1151), (518, 1152), (518, 1305), (518, 1306), (518, 1307), (518, 1308), (519, 1149), (519, 1150), (519, 1151), (519, 1152), (555, 1125), (555, 1126), (555, 1127), (555, 1128), (555, 1148), (555, 1149), (555, 1150), (555, 1151), (556, 1125), (556, 1126), (556, 1127), (556, 1128), (556, 1148), (556, 1149), (556, 1150), (556, 1151), (557, 1125), (557, 1126), (557, 1127), (557, 1128), (557, 1148), (557, 1149), (557, 1150), (557, 1151), (558, 1125), (558, 1126), (558, 1127), (558, 1128), (558, 1148), (558, 1149), (558, 1150), (558, 1151), (570, 595), (570, 596), (570, 597), (570, 598), (570, 618), (570, 619), (570, 620), (570, 621), (571, 595), (571, 596), (571, 597), (571, 598), (571, 618), (571, 619), (571, 620), (571, 621), (572, 595), (572, 596), (572, 597), (572, 598), (572, 618), (572, 619), (572, 620), (572, 621), (573, 595), (573, 596), (573, 597), (573, 598), (573, 618), (573, 619), (573, 620), (573, 621), (680, 91), (680, 92), (680, 93), (680, 94), (681, 91), (681, 92), (681, 93), (681, 94), (681, 193), (681, 194), (681, 195), (681, 196), (681, 296), (681, 297), (681, 298), (681, 299), (681, 386), (681, 387), (681, 388), (681, 389), (681, 478), (681, 479), (681, 480), (681, 481), (681, 658), (681, 659), (681, 660), (681, 661), (682, 91), (682, 92), (682, 93), (682, 94), (682, 193), (682, 194), (682, 195), (682, 196), (682, 296), (682, 297), (682, 298), (682, 299), (682, 386), (682, 387), (682, 388), (682, 389), (682, 478), (682, 479), (682, 480), (682, 481), (682, 658), (682, 659), (682, 660), (682, 661), (683, 91), (683, 92), (683, 93), (683, 94), (683, 193), (683, 194), (683, 195), (683, 196), (683, 296), (683, 297), (683, 298), (683, 299), (683, 386), (683, 387), (683, 388), (683, 389), (683, 478), (683, 479), (683, 480), (683, 481), (683, 658), (683, 659), (683, 660), (683, 661), (684, 193), (684, 194), (684, 195), (684, 196), (684, 296), (684, 297), (684, 298), (684, 299), (684, 386), (684, 387), (684, 388), (684, 389), (684, 478), (684, 479), (684, 480), (684, 481), (684, 658), (684, 659), (684, 660), (684, 661), (698, 1125), (698, 1126), (698, 1127), (698, 1128), (698, 1148), (698, 1149), (698, 1150), (698, 1151), (699, 1125), (699, 1126), (699, 1127), (699, 1128), (699, 1148), (699, 1149), (699, 1150), (699, 1151), (700, 1125), (700, 1126), (700, 1127), (700, 1128), (700, 1148), (700, 1149), (700, 1150), (700, 1151), (701, 1125), (701, 1126), (701, 1127), (701, 1128), (701, 1148), (701, 1149), (701, 1150), (701, 1151), (702, 1005), (702, 1006), (702, 1007), (702, 1008), (702, 1028), (702, 1029), (702, 1030), (702, 1031), (703, 1005), (703, 1006), (703, 1007), (703, 1008), (703, 1028), (703, 1029), (703, 1030), (703, 1031), (704, 1005), (704, 1006), (704, 1007), (704, 1008), (704, 1028), (704, 1029), (704, 1030), (704, 1031), (705, 193), (705, 194), (705, 195), (705, 196), (705, 296), (705, 297), (705, 298), (705, 299), (705, 386), (705, 387), (705, 388), (705, 389), (705, 478), (705, 479), (705, 480), (705, 481), (705, 658), (705, 659), (705, 660), (705, 661), (705, 1005), (705, 1006), (705, 1007), (705, 1008), (705, 1028), (705, 1029), (705, 1030), (705, 1031), (706, 91), (706, 92), (706, 93), (706, 94), (706, 193), (706, 194), (706, 195), (706, 196), (706, 296), (706, 297), (706, 298), (706, 299), (706, 310), (706, 311), (706, 312), (706, 335), (706, 336), (706, 337), (706, 338), (706, 386), (706, 387), (706, 388), (706, 389), (706, 478), (706, 479), (706, 480), (706, 481), (706, 534), (706, 535), (706, 536), (706, 537), (706, 559), (706, 560), (706, 561), (706, 562), (706, 658), (706, 659), (706, 660), (706, 661), (707, 91), (707, 92), (707, 93), (707, 94), (707, 193), (707, 194), (707, 195), (707, 196), (707, 296), (707, 297), (707, 298), (707, 299), (707, 310), (707, 311), (707, 312), (707, 313), (707, 335), (707, 336), (707, 337), (707, 338), (707, 386), (707, 387), (707, 388), (707, 389), (707, 478), (707, 479), (707, 480), (707, 481), (707, 534), (707, 535), (707, 536), (707, 537), (707, 559), (707, 560), (707, 561), (707, 562), (707, 658), (707, 659), (707, 660), (707, 661), (708, 91), (708, 92), (708, 93), (708, 94), (708, 193), (708, 194), (708, 195), (708, 196), (708, 296), (708, 297), (708, 298), (708, 299), (708, 310), (708, 311), (708, 312), (708, 313), (708, 335), (708, 336), (708, 337), (708, 338), (708, 386), (708, 387), (708, 388), (708, 389), (708, 478), (708, 479), (708, 480), (708, 481), (708, 534), (708, 535), (708, 536), (708, 537), (708, 559), (708, 560), (708, 561), (708, 562), (708, 658), (708, 659), (708, 660), (708, 661), (709, 91), (709, 92), (709, 93), (709, 94), (709, 310), (709, 311), (709, 312), (709, 313), (709, 335), (709, 336), (709, 337), (709, 338), (709, 534), (709, 535), (709, 536), (709, 537), (709, 559), (709, 560), (709, 561), (709, 562), (788, 1029), (788, 1030), (788, 1031), (788, 1032), (788, 1124), (788, 1125), (788, 1126), (788, 1127), (789, 1029), (789, 1030), (789, 1031), (789, 1032), (789, 1124), (789, 1125), (789, 1126), (789, 1127), (790, 1029), (790, 1030), (790, 1031), (790, 1032), (790, 1124), (790, 1125), (790, 1126), (790, 1127), (791, 1029), (791, 1030), (791, 1031), (791, 1032), (791, 1124), (791, 1125), (791, 1126), (791, 1127), (811, 1005), (811, 1006), (811, 1007), (811, 1008), (812, 1005), (812, 1006), (812, 1007), (812, 1008), (812, 1123), (812, 1124), (812, 1125), (813, 1005), (813, 1006), (813, 1007), (813, 1008), (813, 1123), (813, 1124), (813, 1125), (813, 1126), (814, 1005), (814, 1006), (814, 1007), (814, 1008), (814, 1123), (814, 1124), (814, 1125), (814, 1126), (815, 1123), (815, 1124), (815, 1125), (815, 1126), (816, 1124), (816, 1125), (816, 1126), (919, 651), (919, 652), (919, 653), (919, 654), (919, 674), (919, 675), (919, 676), (919, 677), (920, 651), (920, 652), (920, 653), (920, 654), (920, 674), (920, 675), (920, 676), (920, 677), (921, 651), (921, 652), (921, 653), (921, 654), (921, 674), (921, 675), (921, 676), (921, 677), (922, 651), (922, 652), (922, 653), (922, 654), (922, 674), (922, 675), (922, 676), (922, 677), (925, 1125), (925, 1126), (925, 1127), (925, 1128), (925, 1148), (925, 1149), (925, 1150), (925, 1151), (926, 1125), (926, 1126), (926, 1127), (926, 1128), (926, 1148), (926, 1149), (926, 1150), (926, 1151), (927, 1125), (927, 1126), (927, 1127), (927, 1128), (927, 1148), (927, 1149), (927, 1150), (927, 1151), (928, 1125), (928, 1126), (928, 1127), (928, 1128), (928, 1148), (928, 1149), (928, 1150), (928, 1151), (979, 1125), (979, 1126), (979, 1127), (979, 1128), (979, 1148), (979, 1149), (979, 1150), (979, 1151), (980, 1125), (980, 1126), (980, 1127), (980, 1128), (980, 1148), (980, 1149), (980, 1150), (980, 1151), (981, 1125), (981, 1126), (981, 1127), (981, 1128), (981, 1148), (981, 1149), (981, 1150), (981, 1151), (982, 1125), (982, 1126), (982, 1127), (982, 1128), (982, 1148), (982, 1149), (982, 1150), (982, 1151), (1009, 91), (1009, 92), (1009, 93), (1009, 94), (1009, 310), (1009, 311), (1009, 312), (1009, 313), (1009, 335), (1009, 336), (1009, 337), (1009, 338), (1009, 534), (1009, 535), (1009, 536), (1009, 537), (1009, 559), (1009, 560), (1009, 561), (1009, 562), (1009, 650), (1009, 651), (1009, 652), (1009, 653), (1009, 675), (1009, 676), (1009, 677), (1009, 678), (1009, 1124), (1009, 1125), (1009, 1126), (1009, 1127), (1010, 91), (1010, 92), (1010, 93), (1010, 94), (1010, 310), (1010, 311), (1010, 312), (1010, 313), (1010, 335), (1010, 336), (1010, 337), (1010, 338), (1010, 534), (1010, 535), (1010, 536), (1010, 537), (1010, 559), (1010, 560), (1010, 561), (1010, 562), (1010, 650), (1010, 651), (1010, 652), (1010, 653), (1010, 675), (1010, 676), (1010, 677), (1010, 678), (1010, 1124), (1010, 1125), (1010, 1126), (1010, 1127), (1011, 91), (1011, 92), (1011, 93), (1011, 94), (1011, 310), (1011, 311), (1011, 312), (1011, 313), (1011, 335), (1011, 336), (1011, 337), (1011, 338), (1011, 534), (1011, 535), (1011, 536), (1011, 537), (1011, 559), (1011, 560), (1011, 561), (1011, 562), (1011, 650), (1011, 651), (1011, 652), (1011, 653), (1011, 675), (1011, 676), (1011, 677), (1011, 678), (1011, 1124), (1011, 1125), (1011, 1126), (1011, 1127), (1012, 91), (1012, 92), (1012, 93), (1012, 94), (1012, 310), (1012, 311), (1012, 312), (1012, 313), (1012, 335), (1012, 336), (1012, 337), (1012, 338), (1012, 534), (1012, 535), (1012, 536), (1012, 537), (1012, 559), (1012, 560), (1012, 561), (1012, 562), (1012, 650), (1012, 651), (1012, 652), (1012, 653), (1012, 675), (1012, 676), (1012, 677), (1012, 678), (1012, 1124), (1012, 1125), (1012, 1126), (1012, 1127), (1034, 67), (1034, 68), (1034, 69), (1034, 70), (1034, 1148), (1034, 1149), (1034, 1150), (1034, 1151), (1035, 67), (1035, 68), (1035, 69), (1035, 70), (1035, 1148), (1035, 1149), (1035, 1150), (1035, 1151), (1036, 67), (1036, 68), (1036, 69), (1036, 70), (1036, 1148), (1036, 1149), (1036, 1150), (1036, 1151), (1037, 67), (1037, 68), (1037, 69), (1037, 70), (1037, 1148), (1037, 1149), (1037, 1150), (1037, 1151)]

上面保存的文本文件可以作为TextAssets对象统一访问。 Unity代码如下。

using UnityEngine;
using System.Collections;
using UnityEditor;

public class CreateWalls : MonoBehaviour {

    public GameObject start;
    public GameObject end;
    int k=0;
    int count = 0;
    public TextAsset TextFile;
    public GameObject wallPrehab;
    GameObject wall;


    Vector3[] coordinates = null;

    /*
     *  Use this for initialization
     */
    void Start () {
        readTextFileLines();
        Vector3 start = new Vector3();
        Vector3 end = new Vector3();
        for (int i = 0; i < coordinates.Length; i++)
        {
            start = coordinates[i];
            k = i + 1;
            if (k == coordinates.Length)
            {
                end = coordinates[0];
            }
            else
            {
                end = coordinates[k];
            }
            setStart(start);
            setEnd(end);
            adjust();
        }


    }

    // Update is called once per frame
    void Update () {

    }

    /*
     * getting the mouse clicked position coordinate 
     */
    void setStart(Vector3 x){
        start.transform.position = x;
        Debug.Log("Start = " + start.transform.position);
        wall = (GameObject) Instantiate (wallPrehab, start.transform.position, Quaternion.identity);

    }

    /*
     * getting the mouse click over position coordinate
     */
    void setEnd(Vector3 y){
        end.transform.position = y;
        Debug.Log("End = " + end.transform.position);
    }

    /*
     * invoking the wall building method
     */
    void adjust(){
        adjustWall ();
    }

    /*
     * build the wall in between start point and the end point
     */
    void adjustWall(){
        start.transform.LookAt (end.transform.position);
        end.transform.LookAt (start.transform.position);
        float distance = Vector3.Distance (start.transform.position, end.transform.position);
        wall.transform.position = start.transform.position + distance / 2 * start.transform.forward;
        wall.transform.rotation = start.transform.rotation;
        wall.transform.localScale = new Vector3 (wall.transform.localScale.x, wall.transform.localScale.y, distance);
    }

    /*
     * Reading from the text file
     */ 
    void readTextFileLines()
    {
        string splits = TextFile.text.TrimStart('[');
        string[] split = TextFile.text.Split(')');


        string split_1 = null;
        string split_2 = null;

        int pos = 0;
        int lengthOfString = 0;
        int valX, valZ = 0;

        /*
         * Getting the count of the coordinates in the array
         */
        foreach (string x in split)
        {
            count++;
        }

        string[] stringArr = new string[count];
        int[] valuesX = new int[count];
        int[] valuesZ = new int[count];

        /*
         * Splitting the coordinates as x,y
         */
        foreach (string coord in split)
        {
            split_1 = coord;
            split_1 = split_1.Trim('[');
            split_1 = split_1.Trim('(');
            split_1 = split_1.Trim(',');
            split_1 = split_1.Trim(' ');
            split_1 = split_1.Trim('(');
            split_1 = split_1.TrimEnd(']');
            stringArr[pos] = split_1;
            pos++;
        }

        /*
         * Re arranging the code
         */
        for (int a = 0; a < stringArr.Length - 1; a++)
        {
            split_2 = stringArr[a];
            lengthOfString = split_2.Length;

            valX = int.Parse(split_2.Substring(0, split_2.IndexOf(',')));
            valZ = int.Parse(split_2.Substring(split_2.IndexOf(' '), (lengthOfString - split_2.IndexOf(' '))));

            valuesX[a] = valX;
            valuesZ[a] = valZ;

            Debug.Log(valuesX[a]);
            Debug.Log(valuesZ[a]);
        }

        coordinates = new Vector3[count];

        /*
         * Adding the x and z coorinates values to Vector3 arra to build the object
         */
        for (int x =0; x<count;x++) {
            Vector3 createVArray = new Vector3(valuesX[x],0,valuesZ[x]);
            coordinates[x] = createVArray;
        }
    }
}

根据此代码,它应根据“ plans.jpg”生成房屋的墙壁。 我们得到的是所附的屏幕截图。 但这不是我们所需要的。

生成的Unity输出

通用Unity输出

如您所见,与原始图像比较时,附件输出中存在问题。 请,有人可以帮我找出错误! 这是一个很大的帮助。

答案是您的数据不完整(仅角落)且方向错误。

我不得不将它们顺时针旋转90°并镜像翻转坐标

按照Pieter Witvoet的建议,ajdustWall()void无法确定正确的构建顺序

joreldraw建议使用meshfilter,但仅提供了链接

我正在为您的代码设计一种解决方案,该解决方案将使用meshfiler并尽快编辑/更新此帖子

这是数据的原始方向 这是数据的原始方向

这是在旋转和翻转之后 这是在旋转和翻转之后

在这里,我将坐标叠加在原始坐标上 在这里,我将坐标叠加在原始坐标上

这是叠加层的缩放 这是叠加层的缩放

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM