溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

C#中怎么調(diào)用open cv函數(shù)

發(fā)布時(shí)間:2021-07-27 18:35:23 來源:億速云 閱讀:702 作者:Leah 欄目:編程語言

今天就跟大家聊聊有關(guān)C#中怎么調(diào)用open cv函數(shù),可能很多人都不太了解,為了讓大家更加了解,小編給大家總結(jié)了以下內(nèi)容,希望大家根據(jù)這篇文章可以有所收獲。

1.先是在程序中圖像的導(dǎo)入,我是根據(jù)圖像路徑實(shí)現(xiàn),其中path是string類型,是圖像路徑。

IntPtr img=CvInvoke.cvLoadImage(path, Emgu.CV.CvEnum.LOAD_IMAGE_TYPE.CV_LOAD_IMAGE_ANYCOLOR);

2.圖像灰度化處理,先創(chuàng)建一幅尺寸大小為為原圖的8位圖像GrayImg1:

Rectangle cr = CvInvoke.cvGetImageROI(img1);

                int width = cr.Width;

                int height = cr.Height;

IntPtr GrayImg1 = CvInvoke.cvCreateImage(cr.Size, Emgu.CV.CvEnum.IPL_DEPTH.IPL_DEPTH_8U, 1);

現(xiàn)在就能使用cvCvtColor函數(shù)實(shí)現(xiàn)灰度化:

CvInvoke.cvCvtColor(img1, GrayImg1, Emgu.CV.CvEnum.COLOR_CONVERSION.CV_BGR2GRAY);

3.直方圖的創(chuàng)建,并獲取數(shù)據(jù)

int[] hist_size = new int[1] { 256 };//建一個(gè)數(shù)組來存放直方圖數(shù)據(jù)

IntPtr HistImg=CvInvoke.cvCreateHist(1, hist_size, Emgu.CV.CvEnum.HIST_TYPE.CV_HIST_ARRAY, null, 1);//創(chuàng)建了一個(gè)空的直方圖

CvInvoke.cvCalcHist(inPtr1, HistImg,false,System.IntPtr.Zero);//計(jì)算inPtr1指向圖像的數(shù)據(jù),并傳入Histimg中,其中IntPtr[] inPtr1 = new IntPtr[1] { SubImg}。

現(xiàn)在要獲取Histimg中的具體數(shù)據(jù):

for (int i = 0; i < 256; i++)

            {

                temphist[i] = CvInvoke.cvQueryHistValue_1D(histImg, i);

            }

這樣在數(shù)組temphist中保存了直方圖數(shù)據(jù)。

4.對(duì)第一步中由cvLoadImage導(dǎo)入的圖像進(jìn)行像素點(diǎn)的操作。由于img 是IntPtr類型無法直接進(jìn)行操作,所以首先要進(jìn)行格式的轉(zhuǎn)化,把IntPtr型轉(zhuǎn)換成MIplImage:

Emgu.CV.Structure.MIplImage MIpImg =

(Emgu.CV.Structure.MIplImage)System.Runtime.InteropServices.Marshal.PtrToStructure(img, typeof(Emgu.CV.Structure.MIplImage));

然后再C#中使用unsafe中指針操作:npixel = (int)((byte*)img.imageData + img.widthStep * i)[j];

5.在二值話的圖像,對(duì)不為零的區(qū)域經(jīng)行檢測(cè)。

IntPtr Dyncontour = new IntPtr();//存放檢測(cè)到的圖像塊的首地址

IntPtr Dynstorage = CvInvoke.cvCreateMemStorage(0);開辟內(nèi)存區(qū)域

int n= CvInvoke.cvFindContours(tempimg, Dynstorage, ref Dyncontour, StructSize.MCvContour, Emgu.CV.CvEnum.RETR_TYPE.CV_RETR_CCOMP,Emgu.CV.CvEnum.CHAIN_APPROX_METHOD.CV_CHAIN_APPROX_NONE, new Point(0, 0));

n表示檢測(cè)到不為零區(qū)域的個(gè)數(shù)。

6.對(duì)第五步檢測(cè)到的區(qū)域繪制輪廓

for(;DyncontourTemp!=null&&DyncontourTemp.Ptr.ToInt32()!=0;DyncontourTemp=DyncontourTemp.HNext)

{

CvInvoke.cvDrawContours(tempContImg, DyncontourTemp,new MCvScalar(255, 255, 255),new MCvScalar(255, 255, 255), 0, 1, Emgu.CV.CvEnum.LINE_TYPE.EIGHT_CONNECTED, new Point(0, 0));

 }

其中的DyncontourTemp為

Seq<Point> DyncontourTemp1= new Seq<Point>(Dyncontour, null);//方便對(duì)IntPtr類型進(jìn)行操作

Seq<Point> DyncontourTemp=DyncontourTemp1;

7.對(duì)第五步檢測(cè)出的區(qū)域的坐標(biāo)提取,通過cvFindContours函數(shù)的調(diào)用在 Dyncontour中存放的是不為零區(qū)域坐標(biāo)的值存儲(chǔ)在內(nèi)存中的首地址指針。

seq<Point> DyncontourTemp1= new Seq<Point>(Dyncontour, null); //方便對(duì)IntPtr類型進(jìn)行操作

int total=contourImg.Total;//contourImg包含的元素的總數(shù)

 int TempX = 0;  int TempY = 0;int[,] contourArray = new int[2,total];

 //獲得輪廓的坐標(biāo)值

 for (int i = 0; i < total;i++ )

{

  contourArray[0,i]=contourImg[i].X;

  contourArray[1,i]=contourImg[i].Y;

 }

看完上述內(nèi)容,你們對(duì)C#中怎么調(diào)用open cv函數(shù)有進(jìn)一步的了解嗎?如果還想了解更多知識(shí)或者相關(guān)內(nèi)容,請(qǐng)關(guān)注億速云行業(yè)資訊頻道,感謝大家的支持。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI