0

我正在开发一个使用 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);                 

                   }
4

1 回答 1

2

我认为这应该有效:

//----------------------------------------------------------------------------------------------
// 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);                 
                   }
于 2013-08-03T08:37:17.700 回答