美文网首页古今中外文史赏析
C#:图像分割与显示函数实现

C#:图像分割与显示函数实现

作者: 大龙10 | 来源:发表于2025-05-07 06:56 被阅读0次

1、分割函数:

GetImagePart方法实现主要逻辑:

  • 转换为灰度图像
  • 使用FindEdge方法检测左右边界
  • 计算10等分参数(处理余数分配)
  • 生成分割矩形集合
  • 返回指定分区的ROI

2、边界检测方法:

  • FindEdge方法通过逐列扫描检测有效区域边界

  • 支持从左向右或从右向左扫描

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;
                        }
                        // 转换为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("有效区域宽度不足10像素");

                // 计算分割参数
                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;
                }

                // 验证参数有效性
                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;
                }
            }
        }

4、运行结果

相关文章

  • 图像处理

    图像显示 要将图像显示需要 用imread函数载入图像,存储到数据结构Mat类中 用imshow函数显示 图像腐蚀...

  • MFC下实现 灰度图像显示函数代码 C++

    layout: posttitle: "MFC下实现 灰度图像显示函数代码 C++"category: codi...

  • 图像语义分割实践(四)损失函数与实现

    在确定检测任务和模型构建完成后,随之需要对训练的准则Criterion进行实现,可称之为损失函数或代价函数,简明而...

  • OpenCV-Python学习(一):图像入门

    目标: 1.读取图像 2.显示图像 3.保存图像 4.使用Matplotlib显示图像 一、读取图像 读取图像函数...

  • C#显示与隐式转换操作符

    C#类中显示与隐匿转换操作符同时只能实现一个

  • 2022-06-14 Python OpenCV 常用工具类

    显示图像 cv.imshow()是显示图像的函数,需要2个参数,一个是需要显示的图像,还一个是显示图像窗口的标题,...

  • 机器视觉常见库

    图像处理中的常见任务包括显示图像、基本操作(如裁剪、翻转、旋转等)、图像分割、分类和特征提取、图像恢复和图像识别。...

  • Opencv之图像分割

    1、阈值分割 1.1 简介 图像阈值化分割是一种传统的最常用的图像分割方法,因其实现简单、计算量小、性能较稳定而成...

  • 图像分割

    1、阈值分割 1.1 简介 图像阈值化分割是一种传统的最常用的图像分割方法,因其实现简单、计算量小、性能较稳定而成...

  • 形态学滤波

    腐蚀与膨胀能实现多种多样的功能,主要如下: · 消除噪声 · 分割出独立的图像元素,在图像中连接相邻的元素 · 寻...

网友评论

    本文标题:C#:图像分割与显示函数实现

    本文链接:https://www.haomeiwen.com/subject/ebtfijtx.html