機(jī)器視覺(jué)課程設(shè)計(jì)_第1頁(yè)
機(jī)器視覺(jué)課程設(shè)計(jì)_第2頁(yè)
機(jī)器視覺(jué)課程設(shè)計(jì)_第3頁(yè)
機(jī)器視覺(jué)課程設(shè)計(jì)_第4頁(yè)
機(jī)器視覺(jué)課程設(shè)計(jì)_第5頁(yè)
已閱讀5頁(yè),還剩9頁(yè)未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說(shuō)明:本文檔由用戶(hù)提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡(jiǎn)介

1、機(jī)器視覺(jué)課程設(shè)計(jì)對(duì)圓盤(pán)形零件圓心與直徑和矩形零件長(zhǎng)與寬尺寸測(cè)量學(xué)生學(xué)院機(jī)電學(xué)院專(zhuān)業(yè)班級(jí)學(xué) 號(hào)學(xué)生姓名指導(dǎo)教師2015年1月20日1問(wèn)題描述31.1 基本目標(biāo)31.2 基本要求32程序及其算法32.1 檢測(cè)與計(jì)算圓半徑的程序 32.2檢測(cè)與計(jì)算矩形長(zhǎng)和寬的程序62.2.1 打開(kāi)攝像頭程序62.2.2 保存拍攝的照片程序72.2.3讀取拍攝到的圖片(讀取文名字Curre ntlmage.jpg 的圖片)72.2.4檢測(cè)邊上的點(diǎn)和計(jì)算長(zhǎng)和高的函數(shù) 82.2.5老師寫(xiě)的顯示圖片的函數(shù) 1112133運(yùn)行結(jié)果 4小結(jié)1問(wèn)題描述1.1基本目標(biāo)顯示一張圖片(包含一個(gè)矩形或一個(gè)圓),測(cè)量矩形的長(zhǎng)寬或圓的直徑。

2、完成得及1.2基本要求“機(jī)器視覺(jué)”考試結(jié)果要求獨(dú)立在計(jì)算機(jī)上完成,建議使用Visual C+和OpenCV實(shí)現(xiàn)一個(gè)具有視覺(jué)捕捉、圖像顯示、尺寸測(cè)量等功能的對(duì)話框程序,其中必須完成對(duì)圓 盤(pán)形零件圓心與直徑和矩形零件長(zhǎng)與寬尺寸測(cè)量?jī)?nèi)容。在教師提供的基本框架程序基礎(chǔ) 上,修改、補(bǔ)充界面和功能。2程序及其算法2.1檢測(cè)與計(jì)算圓半徑的程序思路:從圖片中間橫掃取點(diǎn)得 M_PointO , M_Point1 (x坐標(biāo)相加除2的圓心的x 坐標(biāo))中間縱向取點(diǎn)得 M_Point2,M_Point3 (y坐標(biāo)相加除2的圓心的y坐標(biāo)) 圓上四個(gè)點(diǎn)到圓心的距離(半徑)取平均值,輸出為半徑。顯示圖片程序如下:double

3、 c_DialogTeclar n:f_MakeCircle(double e_dThreshold)if(NULL=m_plpllmageSource)return 0;/定義變量存圖像的寬,高,行像素int qWidth=m_plpllmageSource-width;int qHeight=m_plpllmageSource-height;int qWidthStep=m_plpllmageSource-widthStep;uchar *q_pchDataImage=(uchar *)m_plpllmageSource-imageData;CvPoi nt M_Poi nt4=;/存放檢

4、索出的四個(gè)點(diǎn)循環(huán)檢索01縱掃for(i nt Cycle_Y=1;Cycle_Yq_pchDatalmageqWidth/2+qWidthStep*(Cycle_Y-1) M_Poi nt0.x=qWidth/2;M_Poi nt0.y=Cycle_Y;for(i nt Cycle_Y=qHeight;Cycle_Y1;Cycle_Y-)if(e_dThresholdq_pchDatalmageqWidth/2+qWidthStep*(Cycle_Y-1)M_Poi nt1.x=qWidth/2;M_Poi nt1.y=Cycle_Y;for(i nt Cycle_X=1;Cycle_Xq_p

5、chDatalmageCycle_X+qWidthStep*(qHeight/2-1)M_Poi nt2.x=Cycle_X;M_Poi nt2.y=q_iHeight/2;for(i nt Cycle_X=qWidth;Cycle_X1;Cycle_X-)if(e_dThresholdq_pchDatalmageCycle_X+qWidthStep*(qHeight/2-1)M_Poi nt3.x=Cycle_X;M_Poi nt3.y=q_iHeight/2;for (i nt i=0;i4;i+)畫(huà)邊界圓cvCircle(m_plpllmageSource,M_Poi nti,8,cvS

6、calarAII(127),2);int Ridius=O,clear=O;int circle_y=abs(M_Poi nt1.y+M_Poi nt0.y)/2;int circle_x=abs(M_Poi nt3.x+M_Poi nt2.x)/2;float c=0,j=0;for (i nt i=0;iGetDlgltem(ID_PICTURE_DISPLAY);f_Con trolShowlmage(m_plpllmageDisplay,e_pCWndPicture,m_plpllmageSource); cvSavelmage(_T(./Result_lmage.jpg),m_plp

7、llmageSource);return 0;2.2檢測(cè)與計(jì)算矩形長(zhǎng)和寬的程序思路:這個(gè)程序主要包括:打開(kāi)攝像頭-保存圖片(拍照)-加載剛剛保存的圖 片-找出點(diǎn)并計(jì)算長(zhǎng)和高 四個(gè)按鈕,每一個(gè)按鈕包含下面的一段代碼,拍攝的圖片保 存為Currentlmage.jpg ,這里事先保存了一張同名的圖片,所以可以直接點(diǎn)擊LoadImage讀取。2.2.1 打開(kāi)攝像頭程序void c_DialogTeclarn:O nBn ClickedButt onOpen Camera()CvCapture *q_pCvCapture=cvCreateCameraCapture(O);if(NULL=q_pCvCa

8、pture)return;IplImage *q_plpllmageCapture=cvQueryFrame(q_pCvCapture);if(NULL=q_plpllmageCapture)return;if(NULL!=m_plpllmageSource)cvReleaselmage(&m _plpllmageSource);m_pIplImageSource=NULL;m_plpllmageSource=cvCI on eImage(q_pIplImageCapture);cvReleaseCapture(&q _pCvCapture);CWnd *e_pCWndPicture=this

9、-GetDlgltem(ID_PICTURE_DISPLAY);f_Con trolShowlmage(m_plpllmageDisplay,e_pCWndPicture,m_plpllmageSource);注:綠色的為檢測(cè)是否成功打開(kāi)攝像頭,藍(lán)色的是讀取攝像頭拍攝的圖片到內(nèi)存,后面 的語(yǔ)句是讓圖片在 mfc窗口顯示出來(lái)。2.2.2 保存拍攝的照片程序void c_DialogTeclar n:O nBn ClickedButt onSavelmage()/ TODO:在此添加控件通知處理程序代碼cvSavelmage(_T(./Curre ntlmage.jpg),m_plpllmageS

10、ource);2.2.3 讀取拍攝到的圖片(讀取文名字Curre ntlmage.jpg的圖片)void c_DialogTeclarn:O nBn ClickedButt onLoadlmage()/ TODO:在此添加控件通知處理程序代碼if(NULL!=m_pIplImageSource)cvReleaseImage(&m _plpllmageSource); m_pIplImageSource=NULL;m_pIplImageSource=cvLoadImage(_T(./Curre ntlmage.jpg),。);CWnd *e_pCWndPicture=this-GetDlglte

11、m(ID_PICTURE_DISPLAY);f_Con trolShowlmage(m_plpllmageDisplay,e_pCWndPicture,m_plpllmageSource);2.2.4檢測(cè)邊上的點(diǎn)和計(jì)算長(zhǎng)和高的函數(shù)思路是:第一步找到邊上的點(diǎn),設(shè)定一個(gè)變量e_dThreshold值為127,通過(guò)循環(huán)讓它與每一個(gè)像素的像素值進(jìn)行比較。如果檢測(cè)到一個(gè)點(diǎn)的像素值比127小,則這個(gè)點(diǎn)可能就是需要的點(diǎn)(黑色=0,白色=255)如果每個(gè)像素都要比較運(yùn)算太多,所以上邊兩個(gè) 0 X3, Y3,1 /點(diǎn)的檢測(cè)是沿著圖片長(zhǎng)的的2/5分處和3/5分處向下檢測(cè),下邊的點(diǎn)是沿著1/2處向上檢測(cè)。第二步是用

12、找到6個(gè)點(diǎn)的坐標(biāo)算出三角形的面積,然后除以底邊邊長(zhǎng)得到高。求三角形的面積用的是行列式的值等于三角形面積的2倍程序如下:double c_DialogTeclarn:f_MakeRecta ngleWidth(double e_dThreshold)/ TODO:在此添加控件通知處理程序代碼/定義一些點(diǎn)和變量int qWidth=m_plpllmageSource-width;int qH eight=m_pIplImageSource-height;int q_iWidthStep=m_plpllmageSource-widthStep;uchar *q_pchDataImage=(uchar

13、 *)m_plpllmageSource-imageData;int q_iXLeftTop=2*q|_i Width/5;int q_iXRightTop=3*q口 Width/5;int qXBottom=qWidth/2;int q_iYLeftTop=2*qH eight/5;int q_iYLeftBottom=3*q_iHeight/5;int q_iYRightMid=qH eight/2;CvPoi nt q_CvPoi ntLeftTop,q_CvPoi ntRightTop,q_CvPoi ntBottom;CvPoi nt q_CvPoi ntLeft_Top,q_CvP

14、oi ntRight_Mid,q_CvLeft_Bottom;/求上下兩邊上的點(diǎn),3個(gè)循環(huán)for(i nt q_iCycleHeight=O;q_iCycleHeightq_pchDataImageq_iCycleHeight*q口 WidthStep+q_iXLeftTop)q_CvPoi ntLeftTop.x=q_iXLeftTop;q_CvPoi ntLeftTop.y=q _i CycleHeight;break;for(i nt q_iCycleHeight=O;q_iCycleHeightq_pchDatalmageq_iCycleHeight*qWidthStep+q_iXRi

15、ghtTop)q_CvPoi ntRightTop.x=q_iXRightTop;q_CvPoi ntRightTop.y=qCycleHeight;break;for(i nt q_iCycleHeight=q_iHeight-1;qCycleHeight=0;q_iCycleHeight-)if(e_dThresholdq_pchDatalmageq_iCycleHeight*qWidthStep+q_iXBottom)q_CvPoi ntBottom.x=q_iXBottom;q_CvPo in tBottom.y=q_iCycleHeight;break;/求左右兩邊上的點(diǎn),3個(gè)循環(huán)f

16、or(i nt q_iCycleWidth=O;q_iCycleWidthq_pchDatalmageq_iCycleWidth+qWidthStep*(qYLeftTop-1)q_CvPoi ntLeft_Top.x=q_iCycleWidth;q_CvPoi ntLeft_Top.y=q_iYLeftTop;break;for(i nt q_iCycleWidth=O;q_iCycleWidthq_pchDatalmageq_iCycleWidth+qWidthStep*(qYLeftBottom-1) q_CvLeft_Bottom.x=q_iCycleWidth;q_CvLeft_Bo

17、ttom.y=q_iYLeftBottom;break;for(i nt q_iCycleWidth=q_iHeight-1;qCycleWidth=O;qCycleWidth-)if(e_dThresholdq_pchDatalmageq_iCycleWidth+qWidthStep*(qYRightMid-1)q_CvPoi ntRight_Mid.x=q_iCycleWidth;q_CvPoi ntRight_Mid.y=q_iYRightMid;break;/在檢測(cè)到的點(diǎn)上畫(huà)圓,只是為了更好看到找點(diǎn)的情況cv Circle (m_plpllmageSource,q_CvPointLef

18、tTop,5,cvScalarAII(127),2);cvCircle(m_plpllmageSource,q_CvPoi ntRightTop,5,cvScalarAII(127),2);cvCircle(m_plpllmageSource,q_CvPoi ntBottom,5,cvScalarAII(127),2);cvCircle(m_plpllmageSource,q_CvPoi ntLeft_Top,5,cvScalarAII(127),2);cvCircle(m_plpllmageSource,q_CvLeft_Bottom,5,cvScalarAII(127),2);cvCirc

19、le(m_plpllmageSource,q_CvPoi ntRight_Mid,5,cvScalarAII(127),2);/顯示畫(huà)圓后的圖片CWnd *e_pCWndPicture=this-GetDlgltem(ID_PICTURE_DISPLAY);f_Con trol Showlmage(m_plpllmageDisplay,e_pCWndPicture,m_plpllmageSource);/計(jì)算長(zhǎng)和高的像素值,三角形3點(diǎn)的坐標(biāo)構(gòu)成行列式,行列式的值=2*面積,行列式的值/底邊=高CvMat Ma;int HIGH,WIDE;float Area;int a=q_CvPoi ntL

20、eftTop.x-q_CvPoi ntRightTop.x;int b=q_CvPoi ntLeftTop.y-q_CvPoi ntRightTop.y;double c=a*a+b*b;/定義行列式doublearr9=q_CvPoi ntLeftTop.x,q_CvPoi ntLeftTop.y,1,q_CvPoi ntRightTop.x,q_CvPoi ntRightTop.y,1,q_CvPoi ntBottom.x,q_CvPoi ntBottom.y,1;cvlni tMatHeader(&Ma,3,3,CV_64FC1,arr);Area=abs(cvDet(&Ma);HIGH

21、=Area/sqrt(c);int A=q_CvPoi ntLeft_Top.x-q_CvLeft_Bottom.x;int B=q_CvPoi ntLeft_Top.y-q_CvLeft_Bottom.y;double C=A*A+B*B;/定義行列式DoubleARR9=q_CvPoi ntLeft_Top.x,q_CvPoi ntLeft_Top.y,1,q_CvLeft_Bottom.x,q_CvLeft_Bottom.y,1,q_CvPoi ntRight_Mid.x,q_CvPoi ntRight_Mid.y,1;cvlni tMatHeader(&Ma,3,3,CV_64FC1,

22、ARR);Area=abs(cvDet(&Ma);WIDE=Area/sqrt(C);/在窗口中顯示長(zhǎng)和高char ch110,ch210;itoa(HIGH,ch1,10);itoa(WIDE,ch2,10);SetDlgltemText(IDC_L0NG,ch1);SetDlgltemText(IDC_SH0RT,ch2);cvSavelmage(_T(./Result_lmage.jpg),m_plpllmageSource);return 0;/調(diào)用上面的函數(shù)進(jìn)行檢測(cè)點(diǎn)和計(jì)算長(zhǎng)和高的值,點(diǎn)擊第四個(gè)按鈕會(huì)調(diào)用這段程序,當(dāng)里面的函數(shù)調(diào)用時(shí),它會(huì)調(diào)用上面那段函數(shù)void c_DialogTe

23、clarn:OnBnClickedButtonRectangle ()/ TODO:在此添加控件通知處理程序代碼f_MakeRecta ngleWidth();2.2.5老師寫(xiě)的顯示圖片的函數(shù)voidc_DialogTeclar n:f_C on trolShowlmage(lpllmage*& e_plpllmageShow,CWnd*e_pCWndC on trol, Ipllmage *e_pIplImageSource)if(NULL=e_pCWndCo ntrol)|(NULL=e_plpllmageSource)return;CRect q_CRectCo ntrol;e_pCWn

24、dCo ntrol-GetClie ntRect(&q_CRectCo ntrol);if(NULL!=e_plpllmageShow)cvReleaseImage(&e_plpllmageShow);e_pIplImageShow=NULL;e_plpllmageShow=cvCreatelmage(cvSize(q_CRectCo ntrol.Width(),q_CRectCo ntrol.Height(), IPL_DEPTH_8U, e_pIplImageSource-n Cha nn els);cvResize(e_plpllmageSource,e_plpllmageShow);H

25、DC q_HDCCo ntrol=e_pCWndCo ntrol-GetDC()-GetSafeHdc();un sig ned int q_piBuffersizeof(BITMAPINFOHEADER)+sizeof(RGBQUAD)*256;BITMAPINFO *e_pBITMAPINFODisplay=(BITMAPINFO *)q_piBuffer;BITMAPINFOHEADER *e_pBITMAPINFOHEADERDisplay=&(e_pBITMAPINFODisplay-bmiHeader);memset(e_pBITMAPINFOHEADERDisplay,0,sizeof(*e_pBITMAPINFOHEADERDisplay); e_pBITMAPINFOHEADERDisplay-biSize=sizeof(BITMAPINFOHEADER);e_pBITMAPINFOHEADERDisplay-biWidth=e_plpllmageShow-width; e_pBITMAPINFOHEADERDisp

溫馨提示

  • 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶(hù)所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶(hù)上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶(hù)上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶(hù)因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

最新文檔

評(píng)論

0/150

提交評(píng)論