我正在使用C API开发OpenCV应用程序。我正在尝试让cvLine函数具有随机的厚度,因此第一次绘制时它将具有3的厚度,第二次可能是10。现在,每次绘制它都是均匀的厚度。我曾尝试实现cvRandInt,但即使通过研究,我也难以理解如何使用此功能。

CvRNG rng;

int thickness=cvRandInt(&rng);


IplImage* imgScribble = NULL;

while(true )
    {

         IplImage *frame=0;
         frame=cvQueryFrame(capture);
          if( !frame ) break;


         if(imgScribble == NULL)
             {


     imgScribble = cvCreateImage(cvGetSize(frame), 8, 3);

             }



// Holds the yellow thresholded image

  IplImage* imgYellowThresh1 = GetThresholdedImage1(frame,1);
  double moment10 = cvGetSpatialMoment(moments_yellow, 1, 0);
  double moment01 = cvGetSpatialMoment(moments_yellow, 0, 1);
  double area = cvGetCentralMoment(moments_yellow, 0, 0);
  int lastX = posX;
  int lastY = posY;
  posX = moment10/area;
  posY = moment01/area;

                if(lastX>0 && lastY>0 && posX>0 && posY>0)

                  {

             cvLine(imgScribble, cvPoint(posX, posY), cvPoint(lastX, lastY), cvScalar(10,255,255), thickness,CV_AA, 0);

                   }

最佳答案

我认为这应该工作:

//----------------------------------------------------------------------------------------------
// MAIN
//----------------------------------------------------------------------------------------------
int main( int argc, char** argv )
{
    int MaxThickness=10;
    CvRNG rng = cvRNG(0xffffffff);
    //  CvRNG rng(time(NULL));
    cvNamedWindow("img");
    IplImage* img=cvCreateImage( cvSize(800,600), 8, 3 );

    int MaxThikness=10;

    int k=0;
    while(k!=27)
    {
        int thickness=cvRandInt(&rng)%MaxThikness;
        //cout << thickness << endl;
        cvSet(img,cvScalar(255,0,0));
        cvLine(img,cvPoint(50,50),cvPoint(0,0),cvScalar(100,255,255),thickness);
        cvShowImage("img",img);
        k=cvWaitKey(500);
    }
    return 0;
}

对于您的片段(由于它不完整,我无法对其进行测试)。
CvRNG rng = cvRNG(0xffffffff);
int MaxThikness=10;
int thickness=0;
IplImage* imgScribble = NULL;
IplImage *frame = NULL;
IplImage* imgYellowThresh1=NULL;
while(true )
    {
         frame=cvQueryFrame(capture);
          if( !frame ) break;
         if(imgScribble == NULL)
             {
     imgScribble = cvCreateImage(cvGetSize(frame), 8, 3);
             }

// Holds the yellow thresholded image

  imgYellowThresh1 = GetThresholdedImage1(frame,1);
  double moment10 = cvGetSpatialMoment(moments_yellow, 1, 0);
  double moment01 = cvGetSpatialMoment(moments_yellow, 0, 1);
  double area = cvGetCentralMoment(moments_yellow, 0, 0);
  int lastX = posX;
  int lastY = posY;
  posX = moment10/area;
  posY = moment01/area;

  thickness=cvRandInt(&rng)%MaxThikness;
  if(lastX>0 && lastY>0 && posX>0 && posY>0)
                  {
             cvLine(imgScribble, cvPoint(posX, posY), cvPoint(lastX, lastY), cvScalar(10,255,255), thickness,CV_AA, 0);
                   }

关于c - 随机线粗度-OpenCv,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/18030690/

10-17 01:27