Calculate the average gray scale value in the circular area

Source: Internet
Author: User

Calculate the average gray scale value in the circular area

# Include
 
  
# Include
  
   
Using namespace cv; using namespace std; const int kvalue = 15; // bilateral filter Neighborhood Size const double PI = 3.14; // circular rate int graylevel (Mat image, Mat dst, point cen, int r) // obtain the average gray scale value in the circular area {int graysum = 0, n = 0; for (int I = (cen. y-r); I <= (cen. y + r); ++ I) // access the pixel value in the rectangle frame {uchar * data = image. ptr
   
    
(I); for (int j = (cen. x-r); j <= (cen. x + r); + + j) {double d = (i-cen.y) * (i-cen.y) + (j-cen.x) * (j-cen.x); if (d <r * r) {++ n; graysum + = (int) data [j] ;}}for (int I = (cen. y-r); I <= (cen. y + r); ++ I) // draw a circle. The pixel value in the circle is the average gray value {uchar * temp = dst. ptr
    
     
(I); for (int j = (cen. x-r); j <= (cen. x + r); + + j) {double d = (i-cen.y) * (i-cen.y) + (j-cen.x) * (j-cen.x); if (d <r * r) {temp [j] = (int) (graysum/n) ;}}return (graysum/n);} int main () {Mat src_color = imread ("1.png "); // read the original color map imshow ("source image-color", src_color); Mat src_gray; // convert the color image into a grayscale image cvtColor (src_color, src_gray, COLOR_BGR2GRAY ); imshow ("source image-grayscale", src_gray); // imwrite ("src_gray.png", src_gray); // declare a single-channel image with a pixel value of 0, used to draw the circle detected by Hof transformation above Mat dst (src_gray.size (), src_gray.type (); dst = Scalar: all (0); Mat bf; // bilateral filter bilateralFilter (src_gray, bf, kvalue, kvalue * 2, kvalue/2) for Grayscale Images; // imshow ("grayscale bilateral filter processing", bf ); // imwrite ("src_bf.png", bf); vector
     
      
Circles; // declare a vector to save the detected circle center coordinates and radius HoughCircles (bf, circles, CV_HOUGH_GRADIENT, 1.5, 20,130, 38, 10, 50 ); // Hof transform detection circle std: vector
      
        V; // Save the absolute values of the x-axis minus the y-axis of the center, which is used to distinguish the two-discharge lamp cout <"x = \ ty = \ tr = \ ts = \ tg =" <endl; for (size_t I = 0; I <circles. size (); I ++) // draws the circle detected by the HOF transform {Point center (cvRound (circles [I] [0]), cvRound (circles [I] [1]); int radius = cvRound (circles [I] [2]); double s = 0; // calculate the area of the circle s = PI * radius; int average = 0; average = graylevel (bf, dst, center, radius); // calculate the average gray scale, and draw the circle (dst, center, 2, Scalar (255),-1, 8, 0); // draw the circle (dst, center, radius, scalar (255), 1, 8, 0); // draw a circle v. push_back (abs (center. x-center.y); // store the abscissa of the center minus the absolute value of the ordinate, used to distinguish the two discharge lights cout <center. x <"\ t" <center. y <"\ t" <radius <"\ t" <s <"\ t" <average <endl; // output the Center Coordinate and radius} sort (v. begin (), v. end (); // sort std: vector in ascending order
       
         Points1, points2; // declares the vertex vector, storing the center coordinates of the two lamps for (size_t I = 0; I <circles. size (); I ++) // used to differentiate two lamps {Point center (cvRound (circles [I] [0]), cvRound (circles [I] [1]); if (abs (center. (x-center.y) <v [12]) points1.push _ back (Point (center. x, center. y); // store the center Coordinate elsepoints2.push _ back (Point (center. x, center. y); // store the center coordinates of the lamps from the bottom left to the top right} cv: Vec4f line1, line2; // fit a straight line fitLine (Mat (points1), line1, CV_DIST_L2, 0, 0.01, 0.01); fitLine (Mat (points2), line2, CV_DIST_L2, 0, 0.01, 0.01); int x01 = (int) line1 [2]; int y01 = (int) line1 [3]; int x11 = (int) (x01 + 300 * line1 [0]); int y11 = (int) (y01 + 300 * line1 [1]); int x21 = (int) (x01-300 * line1 [0]); int y21 = (int) (y01-300 * line1 [1]); int x02 = (int) line2 [2]; int y02 = (int) line2 [3]; int x12 = (int) (x02 + 300 * line2 [0]); int y12 = (int) (y02 + 300 * line2 [1]); int x22 = (int) (x02-300 * line2 [0]); int y22 = (int) (y02-300 * line2 [1]); cv: line (dst, Point (x11, y11), Point (x21, y21), Scalar (255), 1); // draw a straight line cv: line (dst, Point (x12, y12), Point (x22, y22), Scalar (255), 1); imshow ("feature extraction", dst); imwrite ("chuli.png", dst); waitKey ();}
       
      
     
    
   
  
 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.