输入图像,产量裁剪得当,输入图像,输出没有适当裁剪我们正在使用C#项目中的OpenCV库执行自动裁剪操作,对于很少的示例自动裁剪图像,但是对于其他样本则没有正确裁剪。
让我们知道源代码中缺少的东西。
请找到下面的代码。
Mat image = new Mat();
Microsoft.Win32.OpenFileDialog openFileDlg = new Microsoft.Win32.OpenFileDialog();
Nullable<bool> result = openFileDlg.ShowDialog();
if (result == true)
{
image = Cv2.ImRead(openFileDlg.FileName);
}
Mat GrayImage = new Mat();
Cv2.CvtColor(image, GrayImage, ColorConversionCodes.BGR2GRAY);
Mat BlurImage = new Mat();
Cv2.GaussianBlur(GrayImage,BlurImage, new OpenCvSharp.Size(5,5),0);
Mat CannyImage = new Mat();
OpenCvSharp.Point[][] contours;
HierarchyIndex[] hierarchy;
Cv2.Canny(BlurImage, CannyImage, 80, 150,7);
Cv2.ImShow("Original Image", image);
Cv2.ImShow("Canny Edged Image", CannyImage);
Cv2.DestroyAllWindows();
Cv2.FindContours(CannyImage, out contours, out hierarchy,RetrievalModes.External, ContourApproximationModes.ApproxSimple);
image.SaveImage(@"C:\Users\TestAccount\Documents\AutoCrop_OpenCV\Output03012022\20220103.jpeg");
var contourIndex = 0;
var previousArea = 0;
var biggestContourRect = Cv2.BoundingRect(contours[0]);
while ((contourIndex >= 0))
{
var contour = contours[contourIndex];
var boundingRect = Cv2.BoundingRect(contour); //Find bounding rect for each contour
var boundingRectArea = boundingRect.Width * boundingRect.Height;
if (boundingRectArea > previousArea)
{
biggestContourRect = boundingRect;
previousArea = boundingRectArea;
}
contourIndex = hierarchy[contourIndex].Next;
}
var finalImage = new Mat(image, biggestContourRect); //Crop the image
string outputBaseDirectory = @"C:\Users\TestAccount\Documents\AutoCrop_OpenCV\Output03012022";
finalImage.SaveImage(string.Format("{0}{1}{2}", outputBaseDirectory, "\\Output", System.IO.Path.GetFileName(openFileDlg.FileName)));
Cv2.ImShow("Final Image", finalImage);
发布于 2022-01-05 00:03:31
与后一种不正确的作物相比,OpenCV需要更好的对比度。在试图找到轮廓之前,尝试使用阈值函数。我看到的主要问题是不正确的作物是背景,可以部分通过文件,这将阻止正确的边缘检测。
简而言之,中的--对比度需要改进,这可以用阈值来实现。
编辑
/// <summary>
/// Performs a gradient detection function and returns the altered image
/// </summary>
/// <param name="mat">The Mat image to perform the functions on</param>
/// <param name="id">ID for tests</param>
/// <returns>The Mat image altered by the function</returns>
public static Mat GradientDetection(Mat mat, string id = "")
{
Log.Information("Performing gradient detection on image");
Mat clone = mat.Sobel(MatType.CV_32F, 1, 0);
if (_settingsService.PIDSettings.Detector.SetTestRemoveBorder != 0)
{
SaveLoadHelper.SaveImage(clone, $"{id}_Sobel");
}
clone = clone.Abs();
if (_settingsService.PIDSettings.Detector.SetTestRemoveBorder != 0)
{
SaveLoadHelper.SaveImage(clone, $"{id}_GradientDetection");
}
clone.MinMaxIdx(out double min, out double max);
clone = 255 * ((clone - min) / (max - min));
if (_settingsService.PIDSettings.Detector.SetTestRemoveBorder != 0)
{
SaveLoadHelper.SaveImage(clone, $"{id}_ScaledBack");
}
return clone;
}
和
/// <summary>
/// Detects lines in the image then return a mask of detected areas
/// </summary>
/// <param name="mat">The Mat image to run the detection on</param>
/// <param name="rectKernel">Rectangular Kernel</param>
/// <param name="squareKernel">Square Kernel</param>
/// <param name="threshold">Theshold for image binarisation</param>
/// <param name="id">ID for tests</param>
/// <returns>The Mat image with the detections performed on it</returns>
public static Mat LineDetection(Mat mat, Mat rectKernel, Mat squareKernel, int threshold = 65, string id = "")
{
Log.Information("Performing Line detection on image");
mat = mat.MorphologyEx(MorphTypes.Close, rectKernel);
if (_settingsService.PIDSettings.Detector.SetTestRemoveBorder != 0)
{
SaveLoadHelper.SaveImage(mat, $"{id}_LineDetectionStage1");
}
mat.ConvertTo(mat, MatType.CV_8UC1);
mat = mat.Threshold(threshold, 255, ThresholdTypes.Binary);
if (_settingsService.PIDSettings.Detector.SetTestRemoveBorder != 0)
{
SaveLoadHelper.SaveImage(mat, $"{id}_LineDetectionStage2");
}
mat = mat.MorphologyEx(MorphTypes.Close, squareKernel);
//mat = mat.Erode(InputArray.KIND_SHIFT, null, 4);
mat = mat.Erode(InputArray.KIND_SHIFT, null, 2);
if (_settingsService.PIDSettings.Detector.SetTestRemoveBorder != 0)
{
SaveLoadHelper.SaveImage(mat, $"{id}_LineDetectionStage3");
}
return mat;
}
在这里帮了不少忙
https://stackoverflow.com/questions/70588448
复制相似问题