我是靠谱客的博主 拼搏棉花糖,最近开发中收集的这篇文章主要介绍OpenCvSharp 绘图,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

NumSharp NDArray 切片赋值

nd[new NumSharp.Slice(i-margin_x, i+margin_x), new NumSharp.Slice(j-margin_y, j+margin_y)] = new int[] { 0, 255, 0 };

NumSharp NDArray 转 OpenCvSharp Mat

public Mat ToMat(NDArray nDArray)
{
    return new Mat(nDArray.shape[0], nDArray.shape[1], GetMatType(nDArray), (Array)nDArray);
}

OpenCvSharp Mat 转 NumSharp NDArray

public NumSharp.NDArray ToNDArray(Mat mat)
{
    var matType = mat.Type();
    var channels = mat.Channels();
    var size = mat.Rows * mat.Cols * channels;
    var shape = channels == 1 ? new NumSharp.Shape(mat.Rows, mat.Cols) : new NumSharp.Shape(mat.Rows, mat.Cols, channels);
    if (matType == MatType.CV_8UC1 || matType == MatType.CV_8UC3 || matType == MatType.CV_8UC4)
    {
        var managedArray = new byte[size];
        Marshal.Copy(mat.Data, managedArray, 0, size);
        var aslice = ArraySlice.FromArray(managedArray);
        return new NDArray(aslice, shape);
    }
    throw new Exception($"mat data type = {matType} is not supported");
}

绘制图形

using System.Runtime.InteropServices;
using OpenCvSharp;
using NumSharp;
using NumSharp.Backends.Unmanaged;

namespace Sense
{
    public class Drawer
    {
        public Drawer() { }

        public MatType GetMatType(NumSharp.NDArray nDArray)
        {
            int channels = nDArray.ndim == 3 ? nDArray.shape[2] : 1;
            return nDArray.typecode switch
            {
                NPTypeCode.Int32 => channels == 1 ? MatType.CV_32SC1 :
                channels == 2 ? MatType.CV_32SC2 :
                throw new ArgumentException($"nDArray data type = {nDArray.typecode} & channels = {channels} is not supported"),

                NPTypeCode.Float => channels == 1 ? MatType.CV_32FC1 :
                throw new ArgumentException($"nDArray data type = {nDArray.typecode} & channels = {channels} is not supported"),

                NPTypeCode.Double => channels == 1 ? MatType.CV_64FC1 :
                throw new ArgumentException($"nDArray data type = {nDArray.typecode} & channels = {channels} is not supported"),

                NPTypeCode.Byte => channels == 1 ? MatType.CV_8UC1 :
                channels == 3 ? MatType.CV_8UC3 :
                throw new ArgumentException($"nDArray data type = {nDArray.typecode} & channels = {channels} is not supported"),

                _ => throw new ArgumentException($"nDArray data type = {nDArray.typecode} is not supported")
            };
        }


        public NumSharp.NDArray ToNDArray(Mat mat)
        {
            var matType = mat.Type();
            var channels = mat.Channels();
            var size = mat.Rows * mat.Cols * channels;
            var shape = channels == 1 ? new NumSharp.Shape(mat.Rows, mat.Cols)
                : new NumSharp.Shape(mat.Rows, mat.Cols, channels);
            if (matType == MatType.CV_8UC1 || matType == MatType.CV_8UC3 || matType == MatType.CV_8UC4)
            {
                var managedArray = new byte[size];
                Marshal.Copy(mat.Data, managedArray, 0, size);
                var aslice = ArraySlice.FromArray(managedArray);
                return new NDArray(aslice, shape);
            }

            throw new Exception($"mat data type = {matType} is not supported");
        }

        public Mat ToMat(NDArray nDArray)
        {
            return new Mat(nDArray.shape[0], nDArray.shape[1], GetMatType(nDArray), (Array)nDArray);
        }

        public void Run()
        {
            Drawer drawer = new Drawer();
            Scalar sc = new Scalar(100, 100, 100);
            int chip_w = 550;
            int chip_h = 550;
            int row = 11;
            int col = 11;
            int mat_w = (row + 2) * chip_w;
            int mat_h = (col + 4) * chip_h;
            Mat mat = new Mat(mat_w, mat_h, MatType.CV_8UC3, sc);
            int center_x = mat_w / 2;
            int center_y = mat_h / 2;
            int r = 5 * chip_w - 200;
            int margin_x = 230;
            int margin_y = 180;

            NDArray nd = drawer.ToNDArray(mat);

            for (int i = chip_w / 2; i < mat_w; i += chip_w)
            {
                for (int j = chip_h / 2; j < mat_h; j += chip_h)
                {                    
                    int dist = (i - center_x) * (i - center_x) + (j - center_y) * (j - center_y);
                    if (dist < r * r)
                    {
                        nd[new NumSharp.Slice(i-margin_x, i+margin_x), 
                           new NumSharp.Slice(j-margin_y, j+margin_y)] = new int[] { 0, 255, 0 };                        
                    }

                }
            }
            nd[new NumSharp.Slice(center_x - margin_x, center_x + margin_x), 
               new NumSharp.Slice(center_y - margin_y, center_y + margin_y)] = new int[] { 255, 0, 0 };
            Mat res = drawer.ToMat(nd);
            int label_x = -2;
            
            for (int i = chip_w / 2; i < mat_w; i += chip_w)
            {
                label_x += 1;
                int label_y = 0;
                for (int j = chip_h / 2; j < mat_h; j += chip_h)
                {                    
                    int dist = (i - center_x) * (i - center_x) + (j - center_y) * (j - center_y);
                    if (dist < r * r)
                    {
                        label_y += 1;
                        Cv2.PutText(res,label_x+","+label_y, new Point(j,i), 
                                    HersheyFonts.HersheyComplex,2,new Scalar(0,0,0),5, LineTypes.Link8);
                    }else
                    {
                        label_y = 0;
                    }
                }
            }
            Cv2.Circle(res, new Point(center_y, center_x), r+80, new Scalar(0, 0, 255), 4, LineTypes.Link8);
            Cv2.ImWrite("E://test0.png", res);
        }
    }
}

最后

以上就是拼搏棉花糖为你收集整理的OpenCvSharp 绘图的全部内容,希望文章能够帮你解决OpenCvSharp 绘图所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(50)

评论列表共有 0 条评论

立即
投稿
返回
顶部