how to detect door in floor plan image file using emgu.cv in C#

40 views Asked by At

What i have tried ?

I have image file and geojson file, i need to overlay the geojson file on the image and do some color changes in the room and door.

i have used the emgu.cv library to detect the room and able to switch the color in the image.

Got stuck here

but i need find the room door as well and change the color.. but i have only coordinates for the room. how can i able to find the room door in this ?

Do i need the room door coordinates as well ? I have tried to detect the room door by using the room color(yellow), but it detects both the door in the room.

need some guidance on how to approach this please.

 static void DetectAndDrawDoors(Image<Bgr, byte> image, List<PolygonData> polygonDataList)
 {
     Mat hsvImage = new Mat();
     CvInvoke.CvtColor(image, hsvImage, ColorConversion.Bgr2Hsv);


     ScalarArray lowerBound = new ScalarArray(new MCvScalar(20, 50, 0));
     ScalarArray upperBound = new ScalarArray(new MCvScalar(29, 255, 100));

     Mat yellowMask = new Mat();
     CvInvoke.InRange(hsvImage, lowerBound, upperBound, yellowMask);

     CvInvoke.GaussianBlur(yellowMask, yellowMask, new Size(5, 5), 0);

     CvInvoke.MorphologyEx(yellowMask, yellowMask, MorphOp.Open, CvInvoke.GetStructuringElement(ElementShape.Rectangle, new Size(5, 5), new Point(-1, -1)), new Point(-1, -1), 1, BorderType.Default, new MCvScalar());

     VectorOfVectorOfPoint contours = new VectorOfVectorOfPoint();
     CvInvoke.FindContours(yellowMask, contours, null, RetrType.List, ChainApproxMethod.ChainApproxSimple);


     // Iterate over contours using a for loop
     for (int i = 0; i < contours.Size; i++)
     {
         VectorOfPoint approxContour = new VectorOfPoint();
         CvInvoke.ApproxPolyDP(contours[i], approxContour, 10, false);

         if (CvInvoke.ContourArea(approxContour) > 20)
         {
             Rectangle boundingBox = CvInvoke.BoundingRectangle(approxContour);

             // Check if the current contour is part of any active polygon
             foreach (var polygonData in polygonDataList.Where(pd => pd.Active == "True"))
             {
                 if (IsContourInActivePolygon(approxContour, polygonData.Coordinates))
                 {
                     CvInvoke.Rectangle(image, boundingBox, new MCvScalar(0, 255, 0), 20);

                 }

             }
         }
     }

 }

 static bool IsContourInActivePolygon(VectorOfPoint contour, List<Coordinate> polygonCoordinates)
 {
     // Convert contour points to coordinates
     List<Coordinate> contourCoordinates = contour.ToArray().Select(point => new Coordinate { Latitude = point.Y, Longitude = point.X }).ToList();

     // Check if any point of the contour is inside the polygon
     return contourCoordinates.Any(point => IsPointInPolygon(point, polygonCoordinates));
 }

 static bool IsPointInPolygon(Coordinate point, List<Coordinate> polygonCoordinates)
 {
     // Implementation of point in polygon algorithm
     int count = polygonCoordinates.Count;
     bool inside = false;

     for (int i = 0, j = count - 1; i < count; j = i++)
     {
         if (((polygonCoordinates[i].Latitude > point.Latitude) != (polygonCoordinates[j].Latitude > point.Latitude)) &&
             (point.Longitude < (polygonCoordinates[j].Longitude - polygonCoordinates[i].Longitude) * (point.Latitude - polygonCoordinates[i].Latitude) / (polygonCoordinates[j].Latitude - polygonCoordinates[i].Latitude) + polygonCoordinates[i].Longitude))
         {
             inside = !inside;
         }
     }

     return inside;
 }

Image reference for what i achieved : enter image description here

Sample geojson value for room 34

   {
    "type": "Feature",
    "geometry": {
        "type": "Polygon",
        "coordinates": [
            [
                768,
                74
            ],
            [
                768,
                322
            ],
            [
                851,
                322
            ],
            [
                875,
                346
            ],
            [
                872,
                349
            ],
            [
                872,
                350
            ],
            [
                931,
                409
            ],
            [
                934,
                406
            ],
            [
                935,
                406
            ],
            [
                959,
                430
            ],
            [
                959,
                522
            ],
            [
                1102,
                522
            ],
            [
                1102,
                74
            ]
        ]
    },
    "properties": {
        "active": true,
        "roomname": "34",
        "disp_label": "",
        "arch_ref": ""
    }
},
0

There are 0 answers