using System;
using System.Drawing;
using Emgu.CV;
using Emgu.CV.CvEnum;
using Emgu.CV.Structure;
using Emgu.CV.Util;
namespace MBR
{
class Program
{
static void Main(string[] args)
{
string filePath = args[0];
Mat sourceData = CvInvoke.Imread(filePath, ImreadModes.Unchanged);
CvInvoke.Imshow("Source Image", sourceData);
Mat imageData = new Mat(sourceData.Height, sourceData.Width, DepthType.Cv8U, 1);
for (int i = 0; i < sourceData.Rows; i++)
{
for (int j = 0; j < sourceData.Cols; j++)
{
var v = sourceData.Col(j).Row(i);
var pixel = v.GetData();
var tarV = imageData.Col(j).Row(i);
var tarPixel = tarV.GetData();
tarPixel[0] = pixel[3];
tarV.SetTo(tarPixel);
}
}
CvInvoke.Imshow("Threshold Image", imageData);
using (VectorOfVectorOfPoint contours = new VectorOfVectorOfPoint())
{
Mat hierarchy = new Mat();
CvInvoke.FindContours(imageData, contours, hierarchy, RetrType.External, ChainApproxMethod.ChainApproxNone, Point.Empty);
Mat mbr = new Mat(filePath, ImreadModes.Unchanged);
for (int i = 0; i < contours.Size; i++)
{
var rect = CvInvoke.MinAreaRect(contours[i]);
var points = rect.GetVertices();
for (int j = 0; j <= 3; j++)
{
var start = new Point((int)Math.Round(points[j].X), (int)Math.Round(points[j].Y));
var end = new Point((int)Math.Round(points[(j + 1) % 4].X), (int)Math.Round(points[(j + 1) % 4].Y));
CvInvoke.Line(mbr, start, end, new MCvScalar(103, 0, 102), 2);
}
}
CvInvoke.Imshow("MinAreaRect", mbr);
}
CvInvoke.WaitKey(0);
}
}
}