1、分割函數(shù):
GetImagePart方法實現(xiàn)主要邏輯:
- 轉(zhuǎn)換為灰度圖像
- 使用FindEdge方法檢測左右邊界
- 計算10等分參數(shù)(處理余數(shù)分配)
- 生成分割矩形集合
- 返回指定分區(qū)的ROI
2、邊界檢測方法:
FindEdge方法通過逐列掃描檢測有效區(qū)域邊界
支持從左向右或從右向左掃描
3、程序
private Mat originalMat;
private void BtnOpen_Click(object sender, EventArgs e)
{
using (var openFileDialog = new OpenFileDialog())
{
openFileDialog.Filter = "Image Files|*.jpg;*.png;*.bmp";
if (openFileDialog.ShowDialog() == DialogResult.OK)
{
try
{
// 釋放之前的圖像資源
originalMat?.Dispose();
// 讀取圖像(BGR格式)
originalMat = Cv2.ImRead(openFileDialog.FileName, ImreadModes.Color);
if (originalMat.Empty())
{
MessageBox.Show("無法加載圖像!");
return;
}
// 轉(zhuǎn)換為RGB格式并顯示
// Cv2.CvtColor(originalMat, originalMat, ColorConversionCodes.BGR2RGB);
pictureBoxOriginal.Image = OpenCvSharp.Extensions.BitmapConverter.ToBitmap(originalMat);
}
catch (Exception ex)
{
MessageBox.Show($"加載圖像出錯: {ex.Message}");
}
}
}
}
public Mat GetImagePart(Mat sourceImage, int partNumber)
{
if (sourceImage.Empty())
return null;
using (Mat gray = new Mat())
{
Cv2.CvtColor(sourceImage, gray, ColorConversionCodes.BGR2GRAY);
// 計算左右極點
int x_left = FindEdge(gray, true);
int x_right = FindEdge(gray, false);
int totalWidth = x_right - x_left + 1;
if (totalWidth < 10)
throw new ArgumentException("有效區(qū)域?qū)挾炔蛔?0像素");
// 計算分割參數(shù)
int baseWidth = totalWidth / 10;
int remainder = totalWidth % 10;
List<Rect> segments = new List<Rect>();
int currentRight = x_right;
for (int i = 0; i < 10; i++)
{
int width = baseWidth + (i < remainder ? 1 : 0);
int x = currentRight - width + 1;
segments.Add(new Rect(x, 0, width, sourceImage.Rows));
currentRight = x - 1;
}
// 驗證參數(shù)有效性
if (partNumber < 0 || partNumber >= segments.Count)
throw new ArgumentOutOfRangeException("分段編號應為0-9");
Rect roi = segments[partNumber];
return new Mat(sourceImage, roi);
}
}
private int FindEdge(Mat gray, bool findLeft)
{
int edge = findLeft ? 0 : gray.Cols - 1;
int step = findLeft ? 1 : -1;
int end = findLeft ? gray.Cols : -1;
for (int x = edge; x != end; x += step)
{
using (Mat col = gray.Col(x))
{
if (Cv2.CountNonZero(col) > 0)
{
edge = x;
break;
}
}
}
return edge;
}
private void button2_Click(object sender, EventArgs e)
{
using (Mat part = GetImagePart(originalMat, 3))
{
if (part != null)
{
Bitmap bmp = OpenCvSharp.Extensions.BitmapConverter.ToBitmap(part);
pictureBoxResult.Image?.Dispose();
pictureBoxResult.Image = bmp;
}
}
}