一連のbmp画像を取り込み、画像上のボックスを検出し、トリミングされたボックスをbmp画像として別のフォルダーに返すC#でプログラムを作成するガイダンスを探しています。
Visual Basic 2010 を使用しています。
私は、.NET サーバーに組み込まれているさまざまな組み込みアルゴリズムをすべて調べました。また、Emgucv の例をダウンロードして試してみました。プリセット画像を使用して、ユーザーが http://www.codeproject.com/script/Articles/ViewDownloads.aspx?aid=30725 をクリックした場所で指定された特定の四角形にトリミングするプログラムを見つけました。これは、Emgucv リファレンスに組み込まれている Example.ShapeDetection.exe を使用します。
using System;
using System.Drawing.Drawing2D;
using System.Collections;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Forms;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Text;
using Emgu.CV;
using Emgu.Util;
using Emgu.CV.Structure;
using Emgu.CV.CvEnum;
namespace Project1{
public static class Project1 {
public static void main()
{
//load in desired images to be processed
//Load the image from file
//for loop that runs all the code below for all the files in a certain folder
//for example
//for(int i = 0; i<"number of images in image folder"; i++){
//code that loads the bmp images
Image<Bgr, Byte> img = new Image<Bgr, byte>(fileNameTextBox.Text).Resize(400, 400, true);
Image<Gray, Byte> gray = img.Convert<Gray, Byte>().PyrDown().PyrUp();
Gray cannyThreshold = new Gray(180);
Gray cannyThresholdLinking = new Gray(120);
Gray circleAccumulatorThreshold = new Gray(120);
Image<Gray, Byte> cannyEdges = gray.Canny(cannyThreshold, cannyThresholdLinking);
LineSegment2D[] lines = cannyEdges.HoughLinesBinary(
1, //Distance resolution in pixel-related units
Math.PI / 45.0, //Angle resolution measured in radians.
20, //threshold
10, //min Line width
10 //gap between lines
)[0]; //Get the lines from the first channel
#region Find rectangles
List<MCvBox2D> boxList = new List<MCvBox2D>();
using (MemStorage storage = new MemStorage()) //allocate storage for contour approximation
for (Contour<Point> contours = cannyEdges.FindContours(); contours != null; contours = contours.HNext)
{
Contour<Point> currentContour = contours.ApproxPoly(contours.Perimeter * 0.05, storage);
if (contours.Area > 100) //only consider contours with area greater than 250
{
if (currentContour.Total == 4) //The contour has 4 vertices.
{
#region determine if all the angles in the contour are 90 degree's
bool isRectangle = true;
Point[] pts = currentContour.ToArray();
LineSegment2D[] edges = PointCollection.PolyLine(pts, true);
for (int i = 0; i < edges.Length; i++)
{
double angle = Math.Abs(
edges[(i + 1) % edges.Length].GetExteriorAngleDegree(edges[i]));
if (angle < 89 || angle > 91)
{
isRectangle = false;
break;
}
}
#endregion
if (isRectangle) boxList.Add(currentContour.GetMinAreaRect());
}
}
}
#endregion
originalImageBox.Image = img;
#region draw rectangles
Image<Bgr, Byte> RectangleImage = img.Copy();
foreach (MCvBox2D box in boxList)
RectangleImage.Draw(box, new Bgr(Color.Green), 2);
RectangleImageBox.Image = RectangleImage;
#endregion
}
}
}
これは私が現在持っているコードであり、私が知っているいくつかの問題がありますが、それらを解決する方法を見つけることができないようです.
filenameImageBox、originalImageBox、および RectangleImageBox は、現在のコンテキストには存在しません。私の推測では、彼らはもはや存在しない画像を参照したためです。
私のコメントにあるように、リソース内のすべての *.bmp イメージを調べてプログラムにロードするループを作成しようとしています。私はこれをどこに行くべきかわかりません。
ただし、最も重要なことは、このコードが探している四角形をうまく検出し、空白の画像に同じサイズで同じ場所に四角形を再描画することです。私がやりたいことは、四角形を全画面のサイズにトリミングすることです。これを行うことができる上にリンクされたCropping Imagesプログラムには部分がありますが、ShapeDetectionアルゴリズムの長方形検出を使用して、そのプログラムからトリミングを分離するのに苦労していました。
提案をいただければ幸いです。私は C# で数週間しか働いていませんが、以前は C++ と Java の経験がありました。