OpenCV – 如何在C中转换cpp代码

我不擅长cpp,我需要有这个代码的ac接口:

#include  #include  #include  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 points; cv::Mat_::iterator it = gray.begin(); cv::Mat_::iterator end = gray.end(); 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;iwidth;i++){ for(j=0;jheight;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; 

你的代码有几个问题,不确定你是否关心它们但我会列出它们:

  • 无需额外的灰色图像。 您也可以使用thresh进行灰度转换。 这更像是一个优化提示;
  • 如果像素为黑色,则进行cvGet2D()比较,添加到列表中 ,而正确的逻辑则相反;
  • 我在评论中告诉你 , cv::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;iwidth;i++) { for (j=0;jheight;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();