每行读取灰色图像

时间:2013-11-06 20:01:28

标签: c++ opencv

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中创建了一个算法,它可以读取所有圆圈并获取颜色。但这并不高效。 我想改变我的算法,我不想使用圆形或边缘检测方法。 我喜欢从第一行的greyimage读取,如果从一侧找到连续的颜色模式(white, black, white, black),则在此行中搜索。

有人能帮帮我吗? 提前致谢

0 个答案:

没有答案