2012-04-25 37 views
0

我不是在CPP好,我需要有這樣的代碼的C接口:OpenCV的 - 如何轉換一段cpp的代碼用C

#include <cv.h> 
#include <highgui.h> 

#include <vector> 


int main(int argc, char* argv[]) 
{ 
    cv::Mat img = cv::imread(argv[1]); 

    // Convert RGB Mat to GRAY 
    cv::Mat gray; 
    cv::cvtColor(img, gray, CV_BGR2GRAY); 

    // Store the set of points in the image before assembling the bounding box 
    std::vector<cv::Point> points; 
    cv::Mat_<uchar>::iterator it = gray.begin<uchar>(); 
    cv::Mat_<uchar>::iterator end = gray.end<uchar>(); 
    for (; it != end; ++it) 
    { 
     if (*it) points.push_back(it.pos()); 
    } 

    // Compute minimal bounding box 
    cv::RotatedRect box = cv::minAreaRect(cv::Mat(points)); 

// Draw bounding box in the original image (debug purposes) 
//cv::Point2f vertices[4]; 
//box.points(vertices); 
//for (int i = 0; i < 4; ++i) 
//{ 
     //cv::line(img, vertices[i], vertices[(i + 1) % 4], cv::Scalar(0, 255, 0), 1, CV_AA); 
//} 
//cv::imshow("box", img); 
//cv::imwrite("box.png", img); 

    // Set Region of Interest to the area defined by the box 
    cv::Rect roi; 
    roi.x = box.center.x - (box.size.width/2); 
    roi.y = box.center.y - (box.size.height/2); 
    roi.width = box.size.width; 
    roi.height = box.size.height; 

    // Crop the original image to the defined ROI 
    cv::Mat crop = img(roi); 
    cv::imshow("crop", crop); 

    cv::imwrite("cropped.png", crop); 
    cvWaitKey(0); 

    return 0; 
} 

有人可以幫我把它包起來或轉換嗎? 謝謝!!

編輯:

這是我嘗試:

IplImage *digit,*gray,*thresh; 
    digit = cvLoadImage("digit.png", 1); 
    gray = cvCreateImage(cvGetSize(digit), digit->depth, 1); 
    thresh = cvCreateImage(cvGetSize(digit), digit->depth, 1); 
    cvCvtColor(digit, gray, CV_RGB2GRAY); 
    cvThreshold(gray, thresh, 250, 255, CV_THRESH_BINARY); 
    CvMemStorage* storage = cvCreateMemStorage(0); 
    CvSeq* ptseq = cvCreateSeq(CV_SEQ_KIND_GENERIC|CV_32SC2, sizeof(CvContour),sizeof(CvPoint),storage); 
    int i,j; 
    CvPoint point; 
    for(i=0;i<thresh->width;i++){ 
     for(j=0;j<thresh->height;j++){ 

      if(cvGet2D(thresh, j, i).val[0]==0){ 
       point.x=i; 
       point.y=j; 
       cvSeqPush(ptseq, &point); 
      } 
     } 
    } 
    CvRect box = cvBoundingRect(ptseq, 1); 
    CvRect roi; 
    roi.x = box.x - (box.width/2); 
    roi.y = box.y - (box.height/2); 
    roi.width = box.width; 
    roi.height = box.height; 
    cvSetImageROI(thresh, roi); 

    IplImage *result = cvCreateImage(cvGetSize(thresh), thresh->depth, 1); 
    cvCopy(thresh, result,NULL); 
    cvResetImageROI(thresh); 
    cvShowImage("output", result); 

    cvWaitKey(0); 
    cvDestroyAllWindows(); 
    return 0; 
+0

對於那些誰不知道,這個代碼是在[另一個答案]我寫的(http://stackoverflow.com/a/10317919/176769)。我認爲這個問題實際上是家庭作業(而不是一個好的)是公平的。你可以再詳細一點嗎?代碼中的哪些部分是你正在努力的? – karlphillip 2012-04-25 22:34:23

+0

std ::向量分; cv :: Mat_ :: iterator it = gray.begin (); cv :: Mat_ :: iterator end = gray.end (); (; it!= end; ++ it) if(* it)points.push_back(it.pos()); } – JackTurky 2012-04-25 22:36:03

+0

該部分仍凍結我..而是使用向量,我嘗試cvSeq,好的,但迭代器? – JackTurky 2012-04-25 22:36:39

回答

4

你的代碼有幾個問題,不知道你是否關心他們,但無論如何,我會一一列舉:

  • 不需要額外的灰色圖像。您也可以使用thresh進行灰度轉換。這更像是一個優化技巧;
  • cvGet2D()比較在做如果像素是黑色的,加入列表,右邊的邏輯是相反的;
  • I told you in a commentcv::RotatedRect相當於CvBox2D,但你沒有聽;
  • 不知道爲什麼你決定使用cvBoundingRect()。我的代碼清楚地顯示cv::minAreaRect(),它相當於cvMinAreaRect2();
  • 另一個優化技巧:一旦您設置ROI,您可以簡單地顯示圖像並將其保存在磁盤上。無需爲該工作創建臨時result;

我想就是這樣。讓選票來:

IplImage *digit, *thresh; 
digit = cvLoadImage("digit.png", 1); 
thresh = cvCreateImage(cvGetSize(digit), digit->depth, 1); 
cvCvtColor(digit, thresh, CV_RGB2GRAY); 

cvThreshold(thresh, thresh, 250, 255, CV_THRESH_BINARY); 

CvMemStorage* storage = cvCreateMemStorage(0); 
CvSeq* ptseq = cvCreateSeq(CV_SEQ_KIND_GENERIC|CV_32SC2, sizeof(CvContour), sizeof(CvPoint), storage); 

int i,j; 
for (i=0;i<thresh->width;i++) 
{ 
    for (j=0;j<thresh->height;j++) 
{ 
     if (cvGet2D(thresh, j, i).val[0] != 0) 
    { 
      CvPoint point; 
      point.x=i; 
      point.y=j; 
      cvSeqPush(ptseq, &point); 
     } 
    } 
} 

CvBox2D box = cvMinAreaRect2(ptseq, 0); 

CvRect roi; 
roi.x = box.center.x - (box.size.width/2); 
roi.y = box.center.y - (box.size.height/2); 
roi.width = box.size.width; 
roi.height = box.size.height; 
cvSetImageROI(thresh, roi); 

cvShowImage("output", thresh); 
cvSaveImage("output.png", thresh); 

cvWaitKey(0); 

cvReleaseImage(&thresh); 
cvReleaseImage(&digit); 
cvClearMemStorage(storage); 
cvDestroyAllWindows(); 
+0

好的!它的作品:)謝謝! – JackTurky 2012-04-26 09:58:21

+0

您好卡爾我再次哈哈我在這裏得到了一個EXC_BAD_ACESS thresh = cvCreateImage(cvGetSize(digit),digit-> depth,1); 任何想法爲什麼? – 2012-05-26 21:33:36

+0

打印參數值以找出可能的原因。 – karlphillip 2012-05-26 22:59:50