109 lines
3.7 KiB
C#
109 lines
3.7 KiB
C#
using Aurora.Api.DataType;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using System.Windows.Media.Imaging;
|
|
|
|
using OpenCvSharp;
|
|
using Matrox.MatroxImagingLibrary;
|
|
using Aurora.Api.Devices;
|
|
using System.IO;
|
|
using System.Configuration;
|
|
using Aurora.Util.HislogPlus;
|
|
|
|
namespace WeldingCommon.Utils
|
|
{
|
|
public class ImageUtil
|
|
{
|
|
public static void SaveImage(MIL_ID image, string fileName, int value, bool useCompress = false)
|
|
{
|
|
if (useCompress) {
|
|
CompressSaveImage(image, fileName, MIL.M_JPEG_LOSSY, value);
|
|
} else {
|
|
CompressSaveImage(image, fileName, MIL.M_PNG);
|
|
}
|
|
}
|
|
|
|
private static bool CompressSaveImage(MIL_ID image, string filePath, MIL_INT format, int icompress = 5)
|
|
{
|
|
string directoryName = Path.GetDirectoryName(filePath);
|
|
if (Directory.Exists(directoryName) == false) {
|
|
Directory.CreateDirectory(directoryName);
|
|
}
|
|
|
|
if (format == MIL.M_JPEG_LOSSY) {
|
|
MIL_INT sizeX = 0, sizeY = 0, attribute = 0;
|
|
MIL_ID milSystem = MIL.M_NULL;
|
|
|
|
attribute = MIL.M_IMAGE + MIL.M_COMPRESS + MIL.M_JPEG_LOSSY;
|
|
|
|
MIL.MbufInquire(image, MIL.M_SIZE_X, ref sizeX);
|
|
MIL.MbufInquire(image, MIL.M_SIZE_Y, ref sizeY);
|
|
MIL.MbufInquire(image, MIL.M_OWNER_SYSTEM, ref milSystem);
|
|
|
|
MIL_ID compressImage = MIL.M_NULL;
|
|
MIL.MbufAllocColor(milSystem, 3, sizeX, sizeY, 8 + MIL.M_UNSIGNED, attribute, ref compressImage);
|
|
MIL.MbufControl(compressImage, MIL.M_Q_FACTOR, icompress); // 1 ~ 99(압축율 최소 ~ 최대)
|
|
MIL.MbufCopy(image, compressImage);
|
|
|
|
MIL.MbufExport(filePath, format, compressImage);
|
|
MIL.MbufFree(compressImage);
|
|
} else {
|
|
MIL.MbufExport(filePath, format, image);
|
|
}
|
|
return true;
|
|
}
|
|
|
|
public static Point[][] FindMinContourForCenter(Mat oriImg, double threshold)
|
|
{
|
|
// Convert to grayscale
|
|
Mat grayImg = new Mat();
|
|
Cv2.CvtColor(oriImg, grayImg, ColorConversionCodes.BGR2GRAY);
|
|
|
|
// Apply threshold
|
|
Mat thresImg = new Mat();
|
|
Cv2.Threshold(grayImg, thresImg, threshold, 200, ThresholdTypes.BinaryInv);
|
|
|
|
// Find contours
|
|
Point[][] contours;
|
|
HierarchyIndex[] hierarchy;
|
|
Cv2.FindContours(thresImg, out contours, out hierarchy, RetrievalModes.CComp, ContourApproximationModes.ApproxSimple);
|
|
|
|
int minWidth = 150;
|
|
int minWSize = int.MaxValue;
|
|
int minCntNo = 0;
|
|
for (int i = 0; i < contours.Length; i++) {
|
|
Rect rect = Cv2.BoundingRect(contours[i]);
|
|
if (rect.Width < minWidth) {
|
|
continue;
|
|
}
|
|
if (minWSize > rect.Width) {
|
|
minWSize = rect.Width;
|
|
minCntNo = i;
|
|
}
|
|
}
|
|
|
|
// Create an empty image to draw the minimum contour
|
|
Mat minCntImg = Mat.Zeros(oriImg.Size(), MatType.CV_8UC1);
|
|
// Draw the minimum contour
|
|
Cv2.DrawContours(minCntImg, contours, minCntNo, Scalar.White, -1);
|
|
|
|
// Find the contour again in the drawn image
|
|
Point[][] minContour;
|
|
Cv2.FindContours(minCntImg, out minContour, out hierarchy, RetrievalModes.External, ContourApproximationModes.ApproxSimple);
|
|
|
|
return minContour;
|
|
}
|
|
|
|
public static void SaveCenterImage(Mat imagesource)
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
}
|