常用的Emgu CV代码(主要有图片格式转换,图片裁剪,图片翻转,图片旋转和图片平移等功能)

本文介绍了一种基于Emgu CV库实现的图像处理方法,包括裁剪、翻转、旋转和平移等操作,并提供了详细的代码实现。这些方法适用于需要进行图像预处理的应用场景。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

转载自博客

using System;
using System.Drawing;
using Emgu.CV;
using Emgu.CV.CvEnum;
using Emgu.CV.Structure;

namespace ZNLGIS
{
    public class ImageClass
    {
        //图片裁剪
        public static Image<Bgr, Byte> Cut(Image<Bgr,Byte> image ,Rectangle rectangle)
        {
            System.Drawing.Size roisize = new Size(260,380);
            IntPtr dst = CvInvoke.cvCreateImage(roisize, Emgu.CV.CvEnum.IPL_DEPTH.IPL_DEPTH_8U, 3);
            CvInvoke.cvSetImageROI(image.Ptr, rectangle);
            CvInvoke.cvCopy(image.Ptr, dst, IntPtr.Zero);

            return OpenCVEmguCVDotNet.IplImagePointerToEmgucvImage<Bgr, Byte>(dst);
        }
        //图片裁剪
        public static Image<Bgr, Byte> Cut2(Image<Bgr,Byte> image,int oldwidth,int oldheight)
        {
            int x = image.Width - oldwidth;
            int y = image.Height - oldheight;
            System.Drawing.Size roisize = new System.Drawing.Size(oldwidth, oldheight); //要裁剪的图片大小
            IntPtr dst = CvInvoke.cvCreateImage(roisize, Emgu.CV.CvEnum.IPL_DEPTH.IPL_DEPTH_8U, 3);
            System.Drawing.Rectangle rect = new System.Drawing.Rectangle(x/2, y/2, oldwidth, oldheight);
            CvInvoke.cvSetImageROI(image.Ptr, rect);
            CvInvoke.cvCopy(image.Ptr, dst, IntPtr.Zero);

            return OpenCVEmguCVDotNet.IplImagePointerToEmgucvImage<Bgr, Byte>(dst);
        }
        //图片翻转
        public static Image<Bgr, Byte> FlipImage(Image<Bgr, Byte> image, bool isHorizontal)
        {
            if (isHorizontal)
            {
                CvInvoke.cvFlip(image.Ptr, IntPtr.Zero, FLIP.HORIZONTAL);
            }
            else
            {
                CvInvoke.cvFlip(image.Ptr, IntPtr.Zero, FLIP.VERTICAL);
            }

            return image;
        }
        //图片旋转
        public static Image<Bgr, Byte> RotateImage(Image<Bgr, Byte> image_old, double angle, bool clockwise)
        {
            IntPtr image_temp;

            double anglerad = Math.PI * (angle / 180);
            int newwidth = (int)Math.Abs(image_old.Bitmap.Height * Math.Sin(anglerad)) +
                        (int)Math.Abs(image_old.Bitmap.Width * Math.Cos(anglerad)) + 1;
            int newheight = (int)Math.Abs(image_old.Bitmap.Height * Math.Cos(anglerad)) +
                        (int)Math.Abs(image_old.Bitmap.Width * Math.Sin(anglerad)) + 1;

            image_temp = CvInvoke.cvCreateImage(new Size(newwidth, newheight), IPL_DEPTH.IPL_DEPTH_8U, 3);
            CvInvoke.cvZero(image_temp);
            int flag = -1;

            if (clockwise)
            {
                flag = 1;
            }

            float[] m = new float[6];
            int w = image_old.Bitmap.Width;
            int h = image_old.Bitmap.Height;
            m[0] = (float)Math.Cos(flag * angle * Math.PI / 180);
            m[1] = (float)Math.Sin(flag * angle * Math.PI / 180);
            m[3] = -m[1];
            m[4] = m[0];

            m[2] = w * 0.5f;
            m[5] = h * 0.5f;

            unsafe
            {
                void* p;
                IntPtr ptr;
                fixed (float* pc = m)
                {
                    p = (void*)pc;
                    ptr = new IntPtr(p);
                }

                IntPtr M = CvInvoke.cvMat(2, 3, MAT_DEPTH.CV_32F, ptr);
                CvInvoke.cvGetQuadrangleSubPix(image_old.Ptr,image_temp,M);
            }

            return OpenCVEmguCVDotNet.IplImagePointerToEmgucvImage<Bgr, Byte>(image_temp);
        }
        //图片平移
        public static Image<Bgr, Byte> Py(Image<Bgr, Byte> src,int x,int y)
        {
            System.Drawing.Size roisize = new Size(src.Width, src.Height);

            Image<Bgr, Byte> dst = new Image<Bgr, byte>(src.Width, src.Height, new Bgr(Color.Transparent));

            int i, j;
            int w = src.Width;
            int h = src.Height;

            if (x >= 0 && y >= 0)
            {
                for (i = 0; i < w - x; i++)
                {
                    for (j = 0; j < h - y; j++)
                    {
                        CvInvoke.cvSet2D(dst, j + y, i + x, CvInvoke.cvGet2D(src, j, i));
                    }
                }
            }
            else if (x >= 0 && y < 0)
            {
                for (i = 0; i < w - x; i++)
                {
                    for (j = -y; j < h; j++)
                    {
                        CvInvoke.cvSet2D(dst, j + y, i + x, CvInvoke.cvGet2D(src, j, i));
                    }
                }
            }
            else if (x < 0 && y >= 0)
            {
                for (i = -x; i < w; i++)
                {
                    for (j = 0; j < h - y; j++)
                    {
                        CvInvoke.cvSet2D(dst, j + y, i + x, CvInvoke.cvGet2D(src, j, i));
                    }
                }
            }
            else
            {
                for (i = -x; i < w; i++)
                {
                    for (j = -y; j < h; j++)
                    {
                        CvInvoke.cvSet2D(dst, j + y, i + x, CvInvoke.cvGet2D(src, j, i));
                    }
                }
            }

            return OpenCVEmguCVDotNet.IplImagePointerToEmgucvImage<Bgr, Byte>(dst);

        }
    }
}


using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.Runtime.InteropServices;
using Emgu.CV;
using Emgu.CV.CvEnum;
using Emgu.CV.Structure;

namespace ZNLGIS
{
    public class OpenCVEmguCVDotNet
    {
        /// <summary>
        /// 将MIplImage结构转换到IplImage指针;
        /// 注意:指针在使用完之后必须用Marshal.FreeHGlobal方法释放。
        /// </summary>
        /// <param name="mi">MIplImage对象</param>
        /// <returns>返回IplImage指针</returns>
        public static IntPtr MIplImageToIplImagePointer(MIplImage mi)
        {
            IntPtr ptr = Marshal.AllocHGlobal(mi.nSize);
            Marshal.StructureToPtr(mi, ptr, false);
            return ptr;
        }

        /// <summary>
        /// 将IplImage指针转换成MIplImage结构
        /// </summary>
        /// <param name="ptr">IplImage指针</param>
        /// <returns>返回MIplImage结构</returns>
        public static MIplImage IplImagePointerToMIplImage(IntPtr ptr)
        {
            return (MIplImage)Marshal.PtrToStructure(ptr, typeof(MIplImage));
        }

        /// <summary>
        /// 将IplImage指针转换成Emgucv中的Image对象;
        /// 注意:这里需要您自己根据IplImage中的depth和nChannels来决定
        /// </summary>
        /// <typeparam name="TColor">Color type of this image (either Gray, Bgr, Bgra, Hsv, Hls, Lab, Luv, Xyz or Ycc)</typeparam>
        /// <typeparam name="TDepth">Depth of this image (either Byte, SByte, Single, double, UInt16, Int16 or Int32)</typeparam>
        /// <param name="ptr">IplImage指针</param>
        /// <returns>返回Image对象</returns>
        public static Image<TColor, TDepth> IplImagePointerToEmgucvImage<TColor, TDepth>(IntPtr ptr)
            where TColor : struct, IColor
            where TDepth : new()
        {
            MIplImage mi = IplImagePointerToMIplImage(ptr);
            return new Image<TColor, TDepth>(mi.width, mi.height, mi.widthStep, mi.imageData);
        }

        /// <summary>
        /// 将IplImage指针转换成Emgucv中的IImage接口;
        /// 1通道对应灰度图像,3通道对应BGR图像,4通道对应BGRA图像。
        /// 注意:3通道可能并非BGR图像,而是HLS,HSV等图像
        /// </summary>
        /// <param name="ptr">IplImage指针</param>
        /// <returns>返回IImage接口</returns>
        public static IImage IplImagePointToEmgucvIImage(IntPtr ptr)
        {
            MIplImage mi = IplImagePointerToMIplImage(ptr);
            Type tColor;
            Type tDepth;
            string unsupportedDepth = "不支持的像素位深度IPL_DEPTH";
            string unsupportedChannels = "不支持的通道数(仅支持1,2,4通道)";
            switch (mi.nChannels)
            {
                case 1:
                    tColor = typeof(Gray);
                    switch (mi.depth)
                    {
                        case IPL_DEPTH.IPL_DEPTH_8U:
                            tDepth = typeof(Byte);
                            return new Image<Gray, Byte>(mi.width, mi.height, mi.widthStep, mi.imageData);
                        case IPL_DEPTH.IPL_DEPTH_16U:
                            tDepth = typeof(UInt16);
                            return new Image<Gray, UInt16>(mi.width, mi.height, mi.widthStep, mi.imageData);
                        case IPL_DEPTH.IPL_DEPTH_16S:
                            tDepth = typeof(Int16);
                            return new Image<Gray, Int16>(mi.width, mi.height, mi.widthStep, mi.imageData);
                        case IPL_DEPTH.IPL_DEPTH_32S:
                            tDepth = typeof(Int32);
                            return new Image<Gray, Int32>(mi.width, mi.height, mi.widthStep, mi.imageData);
                        case IPL_DEPTH.IPL_DEPTH_32F:
                            tDepth = typeof(Single);
                            return new Image<Gray, Single>(mi.width, mi.height, mi.widthStep, mi.imageData);
                        case IPL_DEPTH.IPL_DEPTH_64F:
                            tDepth = typeof(Double);
                            return new Image<Gray, Double>(mi.width, mi.height, mi.widthStep, mi.imageData);
                        default:
                            throw new NotImplementedException(unsupportedDepth);
                    }
                case 3:
                    tColor = typeof(Bgr);
                    switch (mi.depth)
                    {
                        case IPL_DEPTH.IPL_DEPTH_8U:
                            tDepth = typeof(Byte);
                            return new Image<Bgr, Byte>(mi.width, mi.height, mi.widthStep, mi.imageData);
                        case IPL_DEPTH.IPL_DEPTH_16U:
                            tDepth = typeof(UInt16);
                            return new Image<Bgr, UInt16>(mi.width, mi.height, mi.widthStep, mi.imageData);
                        case IPL_DEPTH.IPL_DEPTH_16S:
                            tDepth = typeof(Int16);
                            return new Image<Bgr, Int16>(mi.width, mi.height, mi.widthStep, mi.imageData);
                        case IPL_DEPTH.IPL_DEPTH_32S:
                            tDepth = typeof(Int32);
                            return new Image<Bgr, Int32>(mi.width, mi.height, mi.widthStep, mi.imageData);
                        case IPL_DEPTH.IPL_DEPTH_32F:
                            tDepth = typeof(Single);
                            return new Image<Bgr, Single>(mi.width, mi.height, mi.widthStep, mi.imageData);
                        case IPL_DEPTH.IPL_DEPTH_64F:
                            tDepth = typeof(Double);
                            return new Image<Bgr, Double>(mi.width, mi.height, mi.widthStep, mi.imageData);
                        default:
                            throw new NotImplementedException(unsupportedDepth);
                    }
                case 4:
                    tColor = typeof(Bgra);
                    switch (mi.depth)
                    {
                        case IPL_DEPTH.IPL_DEPTH_8U:
                            tDepth = typeof(Byte);
                            return new Image<Bgra, Byte>(mi.width, mi.height, mi.widthStep, mi.imageData);
                        case IPL_DEPTH.IPL_DEPTH_16U:
                            tDepth = typeof(UInt16);
                            return new Image<Bgra, UInt16>(mi.width, mi.height, mi.widthStep, mi.imageData);
                        case IPL_DEPTH.IPL_DEPTH_16S:
                            tDepth = typeof(Int16);
                            return new Image<Bgra, Int16>(mi.width, mi.height, mi.widthStep, mi.imageData);
                        case IPL_DEPTH.IPL_DEPTH_32S:
                            tDepth = typeof(Int32);
                            return new Image<Bgra, Int32>(mi.width, mi.height, mi.widthStep, mi.imageData);
                        case IPL_DEPTH.IPL_DEPTH_32F:
                            tDepth = typeof(Single);
                            return new Image<Bgra, Single>(mi.width, mi.height, mi.widthStep, mi.imageData);
                        case IPL_DEPTH.IPL_DEPTH_64F:
                            tDepth = typeof(Double);
                            return new Image<Bgra, Double>(mi.width, mi.height, mi.widthStep, mi.imageData);
                        default:
                            throw new NotImplementedException(unsupportedDepth);
                    }
                default:
                    throw new NotImplementedException(unsupportedChannels);
            }
        }

        /// <summary>
        /// 将Emgucv中的Image对象转换成IplImage指针;
        /// </summary>
        /// <typeparam name="TColor">Color type of this image (either Gray, Bgr, Bgra, Hsv, Hls, Lab, Luv, Xyz or Ycc)</typeparam>
        /// <typeparam name="TDepth">Depth of this image (either Byte, SByte, Single, double, UInt16, Int16 or Int32)</typeparam>
        /// <param name="image">Image对象</param>
        /// <returns>返回IplImage指针</returns>
        public static IntPtr EmgucvImageToIplImagePointer<TColor, TDepth>(Image<TColor, TDepth> image)
            where TColor : struct, IColor
            where TDepth : new()
        {
            return image.Ptr;
        }

        /// <summary>
        /// 将IplImage指针转换成位图对象;
        /// 对于不支持的像素格式,可以先使用cvCvtColor函数转换成支持的图像指针
        /// </summary>
        /// <param name="ptr">IplImage指针</param>
        /// <returns>返回位图对象</returns>
        public static Bitmap IplImagePointerToBitmap(IntPtr ptr)
        {
            MIplImage mi = IplImagePointerToMIplImage(ptr);
            PixelFormat pixelFormat;    //像素格式
            string unsupportedDepth = "不支持的像素位深度IPL_DEPTH";
            string unsupportedChannels = "不支持的通道数(仅支持1,2,4通道)";
            switch (mi.nChannels)
            {
                case 1:
                    switch (mi.depth)
                    {
                        case IPL_DEPTH.IPL_DEPTH_8U:
                            pixelFormat = PixelFormat.Format8bppIndexed;
                            break;
                        case IPL_DEPTH.IPL_DEPTH_16U:
                            pixelFormat = PixelFormat.Format16bppGrayScale;
                            break;
                        default:
                            throw new NotImplementedException(unsupportedDepth);
                    }
                    break;
                case 3:
                    switch (mi.depth)
                    {
                        case IPL_DEPTH.IPL_DEPTH_8U:
                            pixelFormat = PixelFormat.Format24bppRgb;
                            break;
                        case IPL_DEPTH.IPL_DEPTH_16U:
                            pixelFormat = PixelFormat.Format48bppRgb;
                            break;
                        default:
                            throw new NotImplementedException(unsupportedDepth);
                    }
                    break;
                case 4:
                    switch (mi.depth)
                    {
                        case IPL_DEPTH.IPL_DEPTH_8U:
                            pixelFormat = PixelFormat.Format32bppArgb;
                            break;
                        case IPL_DEPTH.IPL_DEPTH_16U:
                            pixelFormat = PixelFormat.Format64bppArgb;
                            break;
                        default:
                            throw new NotImplementedException(unsupportedDepth);
                    }
                    break;
                default:
                    throw new NotImplementedException(unsupportedChannels);

            }
            Bitmap bitmap = new Bitmap(mi.width, mi.height, mi.widthStep, pixelFormat, mi.imageData);
            //对于灰度图像,还要修改调色板
            if (pixelFormat == PixelFormat.Format8bppIndexed)
                SetColorPaletteOfGrayscaleBitmap(bitmap);
            return bitmap;
        }

        /// <summary>
        /// 将位图转换成IplImage指针
        /// </summary>
        /// <param name="bitmap">位图对象</param>
        /// <returns>返回IplImage指针</returns>
        public static IntPtr BitmapToIplImagePointer(Bitmap bitmap)
        {
            IImage iimage = null;
            switch (bitmap.PixelFormat)
            {
                case PixelFormat.Format8bppIndexed:
                    iimage = new Image<Gray, Byte>(bitmap);
                    break;
                case PixelFormat.Format16bppGrayScale:
                    iimage = new Image<Gray, UInt16>(bitmap);
                    break;
                case PixelFormat.Format24bppRgb:
                    iimage = new Image<Bgr, Byte>(bitmap);
                    break;
                case PixelFormat.Format32bppArgb:
                    iimage = new Image<Bgra, Byte>(bitmap);
                    break;
                case PixelFormat.Format48bppRgb:
                    iimage = new Image<Bgr, UInt16>(bitmap);
                    break;
                case PixelFormat.Format64bppArgb:
                    iimage = new Image<Bgra, UInt16>(bitmap);
                    break;
                default:
                    Image<Bgra, Byte> tmp1 = new Image<Bgra, Byte>(bitmap.Size);
                    Byte[, ,] data = tmp1.Data;
                    for (int i = 0; i < bitmap.Width; i++)
                    {
                        for (int j = 0; j < bitmap.Height; j++)
                        {
                            Color color = bitmap.GetPixel(i, j);
                            data[j, i, 0] = color.B;
                            data[j, i, 1] = color.G;
                            data[j, i, 2] = color.R;
                            data[j, i, 3] = color.A;
                        }
                    }
                    iimage = tmp1;
                    break;
            }
            return iimage.Ptr;
        }

        /// <summary>
        /// 设置256级灰度位图的调色板
        /// </summary>
        /// <param name="bitmap"></param>
        public static void SetColorPaletteOfGrayscaleBitmap(Bitmap bitmap)
        {
            PixelFormat pixelFormat = bitmap.PixelFormat;
            if (pixelFormat == PixelFormat.Format8bppIndexed)
            {
                ColorPalette palette = bitmap.Palette;
                for (int i = 0; i < palette.Entries.Length; i++)
                    palette.Entries[i] = Color.FromArgb(255, i, i, i);
                bitmap.Palette = palette;
            }
        }
    }
}
<think>好的,我现在需要处理用户关于YOLO模型数据增强的请求,特别是要求完整的代码,同时处理图片标签。首先,我得回顾用户提供的引用内容,看看有哪些信息可以利用。用户引用了四个资料,其中提到使用imgaug库进行数据增强,同时处理bounding boxes。引用12都提到了如何安装依赖库实现坐标变换,引用4还给出了代码示例的结构,比如保存增强后的文件并修改文件名。用户的需求很明确,想要一个完整的代码,能够同时增强图片对应的YOLO标签。接下来,我需要确保代码符合YOLO的数据格式。YOLO的标签是txt文件,每行包含类别归一化的坐标(中心点x,y,宽度,高度)。所以在数据增强时,必须保持这些坐标的正确转换,并且归一化后的值要在0到1之间。然后,考虑用户可能需要的增强方法。用户提到70+种方法,但实际代码可能需要选择常用且有效的变换。比如仿射变换(平移旋转、缩放)、颜色调整(亮度、对比度、饱度)、模糊等。需要确保这些变换应用到图片时,对应的边界框也正确变换。接下来,结构部分。代码应该包括读取图片标签,生成增强序列,应用变换,计算新的bbox坐标,保存增强后的图片标签。同时,文件名需要处理,比如添加后缀_1、_2等,避免重复。还要处理XML文件,但用户可能指的是YOLO的txt标签,所以需要确认是否需要处理XML,但根据问题,用户明确YOLO格式,所以应专注于txt文件。然后,检查依赖库是否正确安装,如imgaugOpenCV。需要确保用户知道如何安装这些库,并在代码中正确导入。另外,需要注意边界框在变换后可能超出图像范围的情况,需要进行裁剪或调整,确保坐标在0到1之间。同时,有些增强可能不适合目标检测,比如过度旋转导致目标不可识别,但用户可能需要自行调整参数。最后,测试代码是否完整,是否有错误处理,比如文件读取失败,或者路径不存在的情况。用户可能需要一个简单明了的示例,直接运行即可,不需要复杂配置。总结:代码结构应包括文件读取、增强管道定义、应用变换、保存结果,并处理标签的同步变换。同时,提供使用说明,如安装依赖、设置路径参数等。</think>以下是通过imgaug库实现YOLO格式数据增强的完整代码,包含图片与标签同步处理的核心功能: ```python import cv2 import os import xml.etree.ElementTree as ET import imgaug as ia from imgaug import augmenters as iaa import numpy as np def yolo_augmentation(image_path, label_path, output_dir, augment_num=3): # 创建增强序列 seq = iaa.Sequential([ iaa.Fliplr(0.5), # 50%概率水平翻转 iaa.Affine( rotate=(-15, 15), # 旋转-15到15度 translate_percent={"x": (-0.1, 0.1), "y": (-0.1, 0.1)}, # 平移 scale=(0.8, 1.2) # 缩放 ), iaa.GaussianBlur(sigma=(0, 1.0)), # 高斯模糊 iaa.AdditiveGaussianNoise(scale=(0, 0.05*255)) # 高斯噪声 ], random_order=True) # 读取图像标签 image = cv2.imread(image_path) h, w = image.shape[:2] # 读取YOLO标签 with open(label_path, 'r') as f: lines = f.readlines() # 转换YOLO格式为imgaug格式 bbs = [] for line in lines: class_id, x_center, y_center, bbox_w, bbox_h = map(float, line.strip().split()) x_min = (x_center - bbox_w/2) * w y_min = (y_center - bbox_h/2) * h x_max = (x_center + bbox_w/2) * w y_max = (y_center + bbox_h/2) * h bbs.append(ia.BoundingBox(x1=x_min, y1=y_min, x2=x_max, y2=y_max)) # 执行增强 for i in range(augment_num): seq_det = seq.to_deterministic() image_aug = seq_det.augment_image(image) bbs_aug = seq_det.augment_bounding_boxes([ia.BoundingBoxesOnImage(bbs, shape=image.shape)])[0] # 转换回YOLO格式 new_lines = [] for bb in bbs_aug.bounding_boxes: # 处理越界坐标 x_min = max(0, min(bb.x1, w)) y_min = max(0, min(bb.y1, h)) x_max = max(0, min(bb.x2, w)) y_max = max(0, min(bb.y2, h)) # 计算归一化坐标 x_center = ((x_min + x_max)/2) / w y_center = ((y_min + y_max)/2) / h bbox_w = (x_max - x_min) / w bbox_h = (y_max - y_min) / h new_lines.append(f"{int(class_id)} {x_center:.6f} {y_center:.6f} {bbox_w:.6f} {bbox_h:.6f}\n") # 保存增强结果 base_name = os.path.splitext(os.path.basename(image_path))[0] cv2.imwrite(os.path.join(output_dir, f"{base_name}_aug{i}.jpg"), image_aug) with open(os.path.join(output_dir, f"{base_name}_aug{i}.txt"), 'w') as f: f.writelines(new_lines) if __name__ == "__main__": # 配置参数 input_dir = "path/to/your/images" label_dir = "path/to/your/labels" output_dir = "path/to/output" os.makedirs(output_dir, exist_ok=True) # 遍历数据集 for filename in os.listdir(input_dir): if filename.endswith(".jpg"): image_path = os.path.join(input_dir, filename) label_path = os.path.join(label_dir, filename.replace(".jpg", ".txt")) yolo_augmentation(image_path, label_path, output_dir, augment_num=3) ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值