c/c++语言开发共享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; 

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

    我想就是这样。 让投票结果来:

     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(); 

      以上就是c/c++开发分享OpenCV – 如何在C中转换cpp代码相关内容,想了解更多C/C++开发(异常处理)及C/C++游戏开发关注计算机技术网(www.ctvol.com)!)。

      本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

      ctvol管理联系方式QQ:251552304

      本文章地址:https://www.ctvol.com/c-cdevelopment/522276.html

      (0)
      上一篇 2020年12月9日
      下一篇 2020年12月9日

      精彩推荐