[問題] opencv 不知道矩陣大小,如何使用??

看板C_and_CPP (C/C++)作者 (馬力)時間12年前 (2013/10/06 14:06), 編輯推噓0(005)
留言5則, 2人參與, 最新討論串1/1
開發平台(Platform): VC++2010 額外使用到的函數庫(Library Used): OpenCV2.4.5 問題(Question): 我想要用矩陣來裝我的資料,可是我不知道要用多大的矩陣來裝 有沒有辦法不先宣告要多大個矩陣?? 我是想說用一個count去計數,先運算一次,讓我知道要宣告多大的mat 然再運算一次把資料裝進去,可是都會發生記憶體錯誤。 請問有沒有甚麼辦法可以不須先宣告矩陣大小,直接就可以使用?? 餵入的資料(Input): 預期的正確結果(Expected Output): 錯誤結果(Wrong Output):記憶體會錯誤,就當掉了 程式碼(Code):(請善用置底文網頁, 記得排版) http://codepad.org/5DBzzjuE vector<Point2f> temp_angle(1); Mat vector_angle00 = Mat(good_matches.size(), 1, CV_32FC1); for (int i = 0; i < good_matches.size(); i++) { temp_angle[0] = cvPoint2D32f(keypoints1[good_matches[i].queryIdx].pt.x - keypoints2[good_matches[i].trainIdx].pt.x, keypoints1[good_matches[i].queryIdx].pt.y -(keypoints2[good_matches[i].trainIdx].pt.y)); if (temp_angle[0].x >= 0) { if (temp_angle[0].y >= 0) { vector_angle00.at<float>(i, 0) = atan(temp_angle[0].y/temp_angle[0].x)*180/3.1415926; } else { vector_angle00.at<float>(i, 0) = (atan(temp_angle[0].y/temp_angle[0].x)*180/3.1415926)+360; } } else if(temp_angle[0].x < 0) { vector_angle00.at<float>(i, 0) = (atan(temp_angle[0].y/temp_angle[0].x)*180/3.1415926)+180; } } CvMat vector_angle = vector_angle00; CvScalar ori_MeanScalar; CvScalar ori_StandardDeviationScalar; cvAvgSdv(&vector_angle, &ori_MeanScalar, &ori_StandardDeviationScalar); printf("ori_Std:%f\n", ori_StandardDeviationScalar.val[0]); Mat vector_angle1(&vector_angle, 1); int match_angle_range = 10; int histSize = 361; float range[] = {0, 360}; const float* histRange = {range}; bool uniform = true; bool accumulate = false; Mat angle_hist; calcHist(&vector_angle1, 1, 0, Mat(), angle_hist, 1, &histSize, &histRange, uniform, accumulate); double min, max; int minInd, maxInd; cv::minMaxIdx(angle_hist, &min, &max, &minInd, &maxInd, Mat()); int count = 0; for (int i = 0; i < vector_angle1.rows; i++) { if (vector_angle1.at<float>(i, 0) <= maxInd+1+(match_angle_range/2) && vector_angle1.at<float>(i, 0) >= maxInd-(match_angle_range/2)) { count++; } } /////////////////////////////////////////////////////////////////// std::vector< DMatch > angle_matches_Idx; CvScalar angle_MeanScalar; CvScalar angle_StandardDeviationScalar; Mat vector_angle_match00 = Mat(count, 1, CV_32FC1); Mat vector_angle_match_len00 = Mat(count, 1, CV_32FC1); Mat vector_dis(count, 1, CV_32FC1); count = 0; for (int i = 0; i < vector_angle1.rows; i++) { if (vector_angle1.at<float>(i, 0) <= maxInd+1+(match_angle_range/2) && vector_angle1.at<float>(i, 0) >= maxInd-(match_angle_range/2)) { vector_dis.at<float>(count, 0) = sqrt(pow((keypoints1[good_matches[i].queryIdx].pt.x - keypoints2[good_matches[i].trainIdx].pt.x), 2) + pow((keypoints1[good_matches[i].queryIdx].pt.y - (keypoints2[good_matches[i].trainIdx].pt.y )), 2)); angle_matches_Idx.push_back(good_matches[i]); vector_angle_match00.at<float>(count, 0) = vector_angle1.at<float>(i, 0); vector_angle_match_len00.at<float>(count, 0) = vector_dis.at<float>(i, 0); count++; } } 補充說明(Supplement): -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 140.117.95.130 ※ 編輯: Marlee 來自: 140.117.95.130 (10/06 14:13)

10/06 14:20, , 1F
C++ 與 C API 不要混用. 然後有 Mat(vector<T>&) 這個建構式
10/06 14:20, 1F

10/06 15:51, , 2F
沒註解不知你要做啥 不過第89行用i不合理吧
10/06 15:51, 2F

10/06 15:52, , 3F
然後一樓的意思 應該是用vector的push_back
10/06 15:52, 3F

10/06 15:53, , 4F
全放進vector後 再用這vector來初始化Mat
10/06 15:53, 4F

10/06 15:54, , 5F
另外cv::Mat那麼好用 何苦再用CvMat呢
10/06 15:54, 5F
文章代碼(AID): #1IKFu1q7 (C_and_CPP)
文章代碼(AID): #1IKFu1q7 (C_and_CPP)