1
uchar** edge_detect(uchar ** gray_arr,uchar ** binary_arr,int height,int width)
{
int x;
//Store the quickmask as a 3X3 two-d array
short int quickmask[3][3]={{-1, 0, -1},{ 0, 4, 0},{-1, 0, -1} };
for(int i=1;i<(height-1);i++)
{
    for(int j=1;j<(width-1);j++)
    {  
        x=0;
        //Convolute the mask
        for(int a=-1;a<2;a++)
            for(int b=-1;b<2;b++)
                x = x + gray_arr[i+a][j+b]*quickmask[a+1][b+1];
        //Take care of the case wheen x<0 or x>255
        if(x<0) x = 0;
        if(x>255) x = 255;
        if(x<THRESHOLD) x = 0;
        if(x>THRESHOLD) x = 255;
        binary_arr[i][j]=x;
    }
}
return(binary_arr);
}

using namespace cv;


void main()
{
Mat src, src_gray;     

src = imread("lena.jpg");


  uchar * data;
  uchar * * binary_arr;
  uchar * * gray_arr;





   /// Convert it to gray   
  cvtColor( src, src_gray, CV_BGR2GRAY );
 std::cout << src.rows << " --- " << src.cols << std::endl;

Rect myROI(0, src_gray.rows-0.2*src_gray.rows, src.cols, 124);

Mat croppedImage = src_gray(myROI);


 /// Reduce the noise so we avoid false circle detection
  GaussianBlur( croppedImage, croppedImage, Size(9, 9), 2, 2 );

  vector<Vec3f> circles;

  /// Apply the Hough Transform to find the circles
  HoughCircles( croppedImage, circles, CV_HOUGH_GRADIENT, 3, 0.01, 200, 50, 0, 60 );

  /// Draw the circles detected
   for( size_t i = 0; i < circles.size(); i++ )
 {
  Point center(cvRound(circles[i][0]), cvRound(circles[i][1]));
  int radius = cvRound(circles[i][2]);
  std::cout << radius << std::endl;
  // circle center
  circle( croppedImage, center, 3, Scalar(0,255,0), -1, 8, 0 );
  // circle outline
  circle( croppedImage, center, radius, Scalar(0,0,255), 3, 8, 0 );
 }

 /// Show your results
 namedWindow( "Hough Circle Transform Demo", CV_NORMAL );
 imshow( "Hough Circle Transform Demo", croppedImage );

 namedWindow( "2", CV_NORMAL );
 imshow( "2", src );

 }`

我有一张灰色的图片,上面有很多圆圈。这些圆圈在右侧有地狱色(白色),然后从左侧开始是黑色、白色、黑色等。我在 C++ 和 openCV 中制作了一个算法,可以读取所有圆圈并获取颜色。但这不是有效的。我想改变我的算法,我不想使用圆形或边缘检测的方法。white, black, white, black我喜欢从第一行的灰度图像中读取,如果从一侧找到连续的颜色模式 ( ),则在这一行中搜索。

谁能帮助我?提前致谢

4

0 回答 0