opencv標(biāo)定程序的代碼(完全測(cè)試好的)_第1頁(yè)
opencv標(biāo)定程序的代碼(完全測(cè)試好的)_第2頁(yè)
opencv標(biāo)定程序的代碼(完全測(cè)試好的)_第3頁(yè)
opencv標(biāo)定程序的代碼(完全測(cè)試好的)_第4頁(yè)
opencv標(biāo)定程序的代碼(完全測(cè)試好的)_第5頁(yè)
已閱讀5頁(yè),還剩19頁(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、opencv 標(biāo)定程序的代碼(完全測(cè)試好的)/* 轉(zhuǎn)自: 非常 感謝博主的分享! !*/這個(gè)是我在 win32 平臺(tái)下寫(xiě)的程序,測(cè)試完全通過(guò): 不過(guò)要注意下面的問(wèn)題:(1)opencv 庫(kù)是 1.0 的,我在 vc6.0 的平臺(tái)使用(2)軟件的設(shè)置要正確(3)圖片載入的時(shí)候,使用的是批處理文件(其實(shí)就是建 一個(gè) txt 文件,里面寫(xiě)的和 dos 下面的操作代碼一樣的輸入)(4)里面現(xiàn)在還有個(gè)問(wèn)題沒(méi)有解決:在得到角點(diǎn)坐標(biāo)的時(shí) 候, cvFindCornerSubPix 里面的搜索區(qū)域還不明白 ( 05)下面是我在網(wǎng)上搜的別人的代 :測(cè)試通過(guò)的: A. 基于計(jì)算機(jī)視覺(jué)技術(shù):e2-a456-7f0d

2、87c24b7a.rarB.C.下面是我的代碼:/*標(biāo)定的過(guò)程:圖片的加載角點(diǎn)的檢測(cè)提取角點(diǎn)精確坐標(biāo)參數(shù)求解利用參數(shù)對(duì)圖像進(jìn)行矯正*/#include<stdio.h>#include"cv.h"#include"highgui.h"#include <stdlib.h>/函數(shù)聲明void PrintMat(CvMat *matrix,BOOL save_or_show,FILE *fp); int main(int argc,char *argv)int i=1;char k=0;int Curr

3、entImage = 0;int CurrentRow = 0; / 行int CurrentColumn = 0; / 列int findcorner_result=0;FILE *fp;/文件指針int ChessBoardSize_w =6;/ 角點(diǎn)個(gè)數(shù)int ChessBoardSize_h =7;int width_pixel=1280;/ 像素int high_pixel=1024;floatSquareSize=10; intNImages=16;CvSize ChessBoardSize; CvSizeimage_pixel;int NPoints=0;int*corner_c

4、ounter;float*temppoints;態(tài)存儲(chǔ)管理。 。CvPoint2D32f *corners;/單通道灰度圖像IplImage *grayimage=0;IplImage *srcimage=0;IplImage *result_image=0; 像/棋盤(pán)大小/這里可以使用內(nèi)存動(dòng)/存儲(chǔ)角點(diǎn)坐標(biāo)的數(shù)組/三通道圖像/矯正以后的圖CvMat *intrinsic_matrix=0; / 內(nèi)部參數(shù)矩 陣CvMat*distortion_coeffs=0;/畸變系數(shù)CvMat*rotation_vectors=0;/旋轉(zhuǎn)向量CvMat*translation_vectors=0;/ 平移向

5、量CvMat*points_counts=0;/圖片角點(diǎn)數(shù)CvMatobject_points=0;/世界坐標(biāo)系中角點(diǎn)的坐標(biāo)CvMat *image_points=0;/ 檢測(cè)到的角點(diǎn)坐標(biāo) CvMat *temp_matrix,*rotation_matrix,*translation_matrix;*/數(shù)據(jù)載入、開(kāi)辟空間image_pixel= cvSize(width_pixel,high_pixel);ChessBoardSize=cvSize(ChessBoardSize_w,ChessBoardSize_h);NPoints=ChessBoardSize_w*ChessBoardSi

6、ze_h; corner_counter=calloc(NImages,sizeof(int); /動(dòng)態(tài)管理,記著釋放空間temppoints =calloc(NImages*NPoints*3,sizeof(float); corners=calloc(NImages*NPoints,sizeof(CvPoint2D32f); if(corner_counter=0 )|(temppoints=0 )|( corners=0 ) )return -1;= cvCreateMat(3,3,CV_32FC1);= cvCreateMat(1,4,CV_32FC1);intrinsic_matri

7、x/內(nèi)參數(shù)矩陣distortion_coeffs/形變參數(shù)rotation_vectors = cvCreateMat(NImages,3,CV_32FC1);/ 旋轉(zhuǎn)向量translation_vectors = cvCreateMat(NImages,3,CV_32FC1);/ 平移向量points_counts = cvCreateMat(NImages,1,CV_32SC1); /視圖數(shù)目object_points =cvCreateMat(NImages*NPoints,3,CV_32FC1);/ 世界坐標(biāo)系中 角點(diǎn)的坐標(biāo)image_points = cvCreateMat(NIma

8、ges*NPoints,2,CV_32FC1);/ 檢測(cè)到的坐標(biāo) 點(diǎn)坐標(biāo) temp_matrix= cvCreateMat(1,3,CV_32FC1);rotation_matrix = cvCreateMat(3,3,CV_32FC1); /旋轉(zhuǎn)矩陣translation_matrix = cvCreateMat(3,3,CV_32FC1);/旋轉(zhuǎn)矩陣grayimage=cvCreateImage(image_pixel,IPL_DEPTH_8U,1);/單通道灰度圖像result_image=cvCreateImage(image_pixel,IPL_DEPTH_8U,1); /校正以后的

9、圖像fp = fopen( "data1.txt", "w+" );/ 打開(kāi)文件 ,建立一個(gè)文件,然后寫(xiě)入數(shù)據(jù)fprintf(fp, " 坐標(biāo)數(shù)據(jù): n" ); /寫(xiě)入數(shù)據(jù),寫(xiě)入文件/*/圖片的加載以及角點(diǎn)的提取if(argc!=NImages+1)printf(" 圖片加載有誤 !");return -1;for(CurrentImage=0;CurrentImage<NImages;CurrentImage+ +)/加載圖片if(srcimage=cvLoadImage(argvCurrentIm

10、age+1,1)!=0)/色彩轉(zhuǎn)換cvCvtColor(srcimage,grayimage,CV_BGR2GRAY);/角點(diǎn)檢測(cè)findcorner_result=cvFindChessboardCorners(grayimage,ChessB oardSize,&cornersCurrentImage*NPoints,&corner_counterCurrentImage,CV_CALIB_CB_ADAPT IVE_THRESH);/畫(huà)出檢測(cè)到的點(diǎn)cvDrawChessboardCorners(srcimage,ChessBoardSize,&amp

11、;cornersCurrentImage*NPoints,corner_counterCurrentI mage,findcorner_result);/精確坐標(biāo)位置cvFindCornerSubPix(grayimage,&cornersCurrentImageNPoints,corner_counterCurrentImage,cvSize(10,10), cvSize(-1,-1),/ 這個(gè)搜索的范圍。 。?cvTermCriteria(CV_TERMCRIT_ITER|CV_TERMCRIT_EPS,500,0.003)/迭代終止條件);cvNamedWindow(&q

12、uot;image",1); cvShowImage("image",srcimage);printf(" 檢測(cè)到的角點(diǎn):%dn",corner_counterCurrentlmage);/fprintf(fp," 檢測(cè)到的角點(diǎn):%dn",corner_counterCurrentlmage);/for(i=0;i<corner_counterCurrentlmage;i+)/坐標(biāo)輸出/ /printf(” 第 %d 個(gè)角點(diǎn) %f %fn",i,cornersCurrentlmage*NPoints+

13、i.x,cornersCurrentlmage*NPoints+i.y);/cvWaitKey(0);printf(" 按任意鍵提取下一幅圖片角點(diǎn)。 。 n");/fprintf(fp," 按任意鍵提取下一幅圖片角點(diǎn)。 。n"); printf(" 角點(diǎn)提取結(jié)束 n"); printf(" 開(kāi)始定標(biāo)n");fprintf(fp," 角點(diǎn)提取結(jié)束 n");fprintf(fp," 開(kāi)始定標(biāo) n");/棋盤(pán)世界坐標(biāo)系坐標(biāo)for(CurrentImage = 0 ; Current

14、Image < NImages ; CurrentImage+)/ 圖片for (CurrentRow = 0; CurrentRow <ChessBoardSize_h; CurrentRow+)/ 行for (CurrentColumn = 0; CurrentColumn < ChessBoardSize_w; CurrentColumn+)/ 列temppoints(CurrentImage*NPoints*3)+(CurrentRow*ChessBoardSize_w+CurrentColumn)*3=(float)(CurrentRow*S

15、quareSize);temppoints(CurrentImage*NPoints*3)+(CurrentRow*ChessBoardSize_w+CurrentColumn)*3+1=(float)(CurrentColumn*Squ areSize);temppoints(CurrentImage*NPoints*3)+(CurrentRow*ChessBo ardSize_w+CurrentColumn)*3+2=0;/參數(shù)求解*object_points=cvMat(NImages*NPoints,3,CV_32FC1,temppoi nts);/ 似乎這里開(kāi)始有點(diǎn)問(wèn)題。 。還有就是

16、在標(biāo)定過(guò)程中這個(gè) 棋盤(pán)的邊長(zhǎng)和標(biāo)定的精度無(wú)關(guān)cvSetData(image_points,corners,sizeof(CvPoint2D32f);/ 設(shè) 置 image_points 矩陣的數(shù)據(jù)cvSetData(points_counts,corner_counter,sizeof(int);/*printf(" 棋盤(pán)坐標(biāo) ");printf("n");PrintMat(object_points);printf("n 計(jì)算機(jī) n");printf("n");PrintMat(image_points);pri

17、ntf("n");printf("n 角點(diǎn)個(gè)數(shù) n");PrintMat(points_counts);printf("n");*/ cvCalibrateCamera2(object_points,image_points,points_counts, cvGetSize(grayimage),intrinsic_matrix,distortion_coeffs,rotation_vectors, translation_vectors,0); printf("n 內(nèi)部岑數(shù)矩陣 n"); fprintf(fp,&

18、quot;n 內(nèi)部岑數(shù)矩陣 n");PrintMat(intrinsic_matrix,FALSE,NULL);PrintMat(intrinsic_matrix,TRUE,fp); printf("n 形變岑數(shù) n"); fprintf(fp,"n 形變岑數(shù) n");PrintMat(distortion_coeffs,FALSE,NULL);PrintMat(distortion_coeffs,TRUE,fp); for(i=0;i<NImages;i+)cvGetRow(rotation_vectors,temp_matri

19、x,i);cvRodrigues2(temp_matrix,rotation_matrix,0); printf(" 第 %d 個(gè)圖片的旋轉(zhuǎn)向量 n",i); fprintf(fp," 第 %d 個(gè)圖片的旋轉(zhuǎn)向量 n",i);PrintMat(temp_matrix,FALSE,NULL);PrintMat(temp_matrix,TRUE,fp); printf(" 第%d個(gè)圖片的旋轉(zhuǎn)矩陣n",i);fprintf(fp,"第%d個(gè)圖片的旋轉(zhuǎn)矩陣n",i);PrintMat(rotation_matrix,FAL

20、SE,NULL);PrintMat(rotation_matrix,TRUE,fp);printf("n");fprintf(fp,"n"); /cvReleaseMat(&temp_matrix);printf(" 平移矩陣 n");fprintf(fp," 平移矩陣 n");for(i=0;i<NImages;i+)cvGetRow(translation_vectors,temp_matrix,i); cvRodrigues2(temp_matrix,translation_ma

21、trix,0);printf("第%d個(gè)圖片的平移向量n",i); fprintf(fp," 第%d個(gè)圖片的平移向量n",i);PrintMat(temp_matrix,FALSE,NULL); PrintMat(temp_matrix,TRUE,fp);printf(" 第 %d 個(gè)圖片的平移矩陣 n",i); fprintf(fp," 第 %d 個(gè)圖片的平移矩陣 n",i); PrintMat(translation_matrix,FALSE,NULL); PrintMat(translation_matrix

22、,TRUE,fp); printf("n");fprintf(fp,"n"); / PrintMat(translation_vectors,FALSE,NULL);/ PrintMat(translation_vectors,TRUE,fp); printf(" 標(biāo) 定結(jié)束 n");fprintf(fp," 標(biāo)定結(jié)束 n"); printf(" 按任意鍵開(kāi)始誤差分析 n");fprintf(fp," 按任意鍵開(kāi)始誤差分析 n");cvWaitKey(0);/誤差分析/利用已

23、知的內(nèi)部參數(shù),使用 cvProjectPoints2() 計(jì)算出 世界坐標(biāo)系中的坐標(biāo)在圖片中的坐標(biāo)也就是校準(zhǔn)后的坐標(biāo)/將校準(zhǔn)后的坐標(biāo)和原來(lái)求得的坐標(biāo)進(jìn)行比較 for(CurrentImage=0;CurrentImage<NImages;CurrentImage+)CvMat *object_matrix= cvCreateMat(NPoints,3,CV_32FC1);CvMat *image_matrix=cvCreateMat(NPoints,2,CV_32FC1);CvMat *project_image_matrix= cvCreateMat(NPoints,2,CV_

24、32FC1);CvMat *rotation_matrix_1=cvCreateMat(1,3,CV_32FC1);CvMat *translation_matrix_1=cvCreateMat(1,3,CV_32FC1);CvMat *rotation_matrix=cvCreateMat(3,1,CV_32FC1);CvMat *translation_matrix=cvCreateMat(3,1,CV_32FC1);double err=0; cvGetRows(object_points,object_matrix,CurrentImage*NPoints ,(CurrentImage

25、+1)*NPoints,1);cvGetRow(rotation_vectors,rotation_matrix_1,CurrentImage);cvReshape(rotation_matrix_1,rotation_matrix,0,3);cvGetRow(translation_vectors,translation_matrix_1,CurrentIma ge);cvReshape(translation_matrix_1,translation_matrix,0,3);cvGetRows(image_points,project_image_matrix,CurrentImage*

26、NPoints,(CurrentImage+1)*NPoints,1);cvProjectPoints2(object_matrix,rotation_matrix,translation_matr ix,intrinsic_matrix,distortion_coeffs,image_matrix,0,0,0,0,0); err=cvNorm(image_matrix,project_image_matrix,CV_L2,0);printf(" 第 %d 幅圖像的誤差為 fn",Currentlmage+1,err);fprintf(fp," 第 %d 幅圖像的

27、誤差為 %fn",CurrentImage+1,err); cvNamedWindow("Undistort_image",1); /顯示校正后的圖片printf("n");for(CurrentImage=0;CurrentImage<NImages;CurrentImage+)/加載圖片if(srcimage=cvLoadImage(argvCurrentImage+1,1)!=0)/色彩轉(zhuǎn)換cvCvtColor(srcimage,grayimage,CV_BGR2GRAY);cvUndistort2(grayimage,re

28、sult_image,intrinsic_matrix,distortion _coeffs);cvShowImage("Undistort_image",result_image); printf(" 按任意鍵顯示下一幅圖片。 。 n"); cvWaitKey(0);/關(guān)閉文件fclose(fp);/釋放內(nèi)存 cvWaitKey(0);free(corner_counter); free(temppoints); free(corners); cvDestroyWindow("Undistort_image"); cvDestroy

29、Window("Image");cvReleaseMat(&intrinsic_matrix); cvReleaseMat(&distortion_coeffs); cvReleaseMat(&rotation_vectors); cvReleaseMat(&translation_vectors); cvReleaseMat(&rotation_matrix); cvReleaseMat(&translation_matrix); cvReleaseMat(&points_counts); cvReleaseMat(&object_points); cv

溫馨提示

  • 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)論