OpenCV的 - 如何转换在C CPP code [英] OpenCV - How to convert a cpp code in C

查看:240
本文介绍了OpenCV的 - 如何转换在C CPP code的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不是CPP好,我需要有这样code的C接口:

 的#include< cv.h>
#包括LT&;&highgui.h GT;#包括LT&;矢量>
INT主(INT ARGC,CHAR *的argv [])
{
    CV ::垫IMG = CV :: imread(的argv [1]);    //转换RGB垫灰色
    CV ::垫灰色;
    CV :: cvtColor(IMG,灰色,CV_BGR2GRAY);    //组装边框之前存储在图像中设定点
    的std ::矢量< CV ::点>点;
    CV :: Mat_<&UCHAR GT;:迭代它= gray.begin<&UCHAR GT;();
    CV :: Mat_<&UCHAR GT;:迭代结束= gray.end<&UCHAR GT;();
    为(;!它到底= ++吧)
    {
        如果(*它)points.push_back(it.pos());
    }    //计算最小的边框
    CV :: RotatedRect盒= CV :: minAreaRect(CV ::垫(点));//绘制在原始图像中边界框(调试)
// CV :: Point2f顶点[4];
//box.points(vertices);
//对于(INT I = 0; I&4; ++ⅰ)
// {
        // CV ::线(IMG,顶点[I]中,顶点[第(i + 1)%4],CV ::标量(0,255,0),1,CV_AA);
//}
// CV :: imshow(盒子,IMG);
//cv::imwrite(\"box.png,IMG);    利息由框定义的区域的//设定区域
    CV ::矩形的投资回报率;
    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;    //裁剪原始图像所定义的投资回报率
    CV ::垫作物= IMG(ROI);
    CV :: imshow(剪裁,作物);    CV :: imwrite(cropped.png作物);
    cvWaitKey(0);    返回0;
}

有人可以帮我把它包起来,或将其转换?
谢谢!

编辑:

这是我做的尝试:

 的IplImage *数字,*灰色*脱粒;
    位= cvLoadImage(digit.png,1);
    灰色= cvCreateImage(cvGetSize(数字),digit->深度上,1);
    脱粒= cvCreateImage(cvGetSize(数字),digit->深度上,1);
    cvCvtColor(数字,灰色,CV_RGB2GRAY);
    cvThreshold(灰色,脱粒,250,255,CV_THRESH_BINARY);
    CvMemStorage *存储= cvCreateMemStorage(0);
    CvSeq * ptseq = cvCreateSeq(CV_SEQ_KIND_GENERIC | CV_32SC2,sizeof的(CvContour)的sizeof(CvPoint),存储);
    INT I,J;
    CvPoint点;
    对于(i = 0; I<&阈值GT;宽度;我++){
        为(J = 0; J<&阈值GT;高度; J ++){            如果(cvGet2D(脱粒,J,I).VAL [0] == 0){
               point.x = I;
               point.y = j的;
               cvSeqPush(ptseq,&安培;点);
            }
        }
    }
    CvRect箱= cvBoundingRect(ptseq,1);
    CvRect投资回报率;
    roi.x = box.x - (box.width / 2);
    roi.y = box.y - (box.height / 2);
    roi.width = box.width;
    roi.height = box.height;
    cvSetImageROI(THRESH,投资回报率);    * IplImage的结果= cvCreateImage(cvGetSize(THRESH),阈值与GT;深度上,1);
    cvCopy(THRESH,因此,NULL);
    cvResetImageROI(THRESH);
    cvShowImage(产出,结果);    cvWaitKey(0);
    cvDestroyAllWindows();
    返回0;


解决方案

您code有几个问题,不知道你关心他们,但无论如何,我会一一列举:


  • 无需额外的灰度图像。您可以使用脱粒为灰度转换为好。这更像是一种优化技巧;

  • cvGet2D()对比正在做的如果像素是黑色的,添加到列表的和正确的逻辑是周围的其他方式;

  • I告诉你在 CV :: RotatedRect CvBox2D 相当于评论,但你没'别听;

  • 不知道你为什么决定使用 cvBoundingRect()。我的code清楚地显示了 CV :: minAreaRect(),它的当量为 cvMinAreaRect2();

  • 另一个优化技巧:一旦你设置了投资回报率,你可以简单地显示图像,并将其保存在磁盘上。无需创建一个临时结果作业;

我想这就是它。让选票来了:

 的IplImage *数字,*反复做;
位= cvLoadImage(digit.png,1);
脱粒= cvCreateImage(cvGetSize(数字),digit->深度上,1);
cvCvtColor(数字,THRESH,CV_RGB2GRAY);cvThreshold(脱粒,脱粒,250,255,CV_THRESH_BINARY);CvMemStorage *存储= cvCreateMemStorage(0);
CvSeq * ptseq = cvCreateSeq(CV_SEQ_KIND_GENERIC | CV_32SC2,sizeof的(CvContour)的sizeof(CvPoint),存储);INT I,J;
对于(i = 0; I<&阈值GT;宽度;我++)
{
    为(J = 0; J<&阈值GT;高度; J ++)
{
        如果(cvGet2D(脱粒,J,I).VAL [0]!= 0)
    {
           CvPoint点;
           point.x = I;
           point.y = j的;
           cvSeqPush(ptseq,&安培;点);
        }
    }
}CvBox2D盒= cvMinAreaRect2(ptseq,0);CvRect投资回报率;
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,投资回报率);cvShowImage(产出,THRESH);
cvSaveImage(output.png,THRESH);cvWaitKey(0);cvReleaseImage(安培;脱粒);
cvReleaseImage(安培;数字);
cvClearMemStorage(储存);
cvDestroyAllWindows();

i'm not good in cpp and i need to have a c interface of this code:

#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;
}

can someone help me to wrap it or convert it? thanks!!

EDIT:

THIS IS WHAT I TRY:

    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;

解决方案

Your code had several problems, not sure if you care about them but I'll list them anyway:

  • No need for the extra GRAY image. You can use thresh for the grayscale conversion as well. This is more like an optimization tip;
  • cvGet2D() comparison was doing if pixel is black, add to the list, and the right logic is the other way around;
  • I told you in a comment that cv::RotatedRect is the equivalent of CvBox2D, but you didn't listen;
  • No idea why you decided to use cvBoundingRect(). My code clearly shows cv::minAreaRect() and it's equivalent is cvMinAreaRect2();
  • Another optimization tip: once you set a ROI you can simply display the image and save it on the disk. No need to create a temporary result for the job;

I guess that's it. Let the up votes come:

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

这篇关于OpenCV的 - 如何转换在C CPP code的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆