日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

EmguCV 一些基本操作

發(fā)布時(shí)間:2023/11/30 编程问答 35 豆豆
生活随笔 收集整理的這篇文章主要介紹了 EmguCV 一些基本操作 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

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

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

二、圖像灰度化處理,先創(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);

三、直方圖的創(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ù)。

四、對(duì)第一步中由cvLoadImage導(dǎo)入的圖像進(jìn)行像素點(diǎn)的操作。由于img 是IntPtr類型無(wú)法直接進(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];

五、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ù)。

六、對(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;

七、對(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;

?}

總結(jié)

以上是生活随笔為你收集整理的EmguCV 一些基本操作的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。