Dealing with pixels in contours (OpenCV)?

后端 未结 3 1242
一生所求
一生所求 2020-12-30 06:49

I have retrieved a contour from an image and want to specifically work on the pixels in the contour. I need to find the sum (not area) of the pixel values in the contour. Op

相关标签:
3条回答
  • 2020-12-30 07:19

    If I understand right you want to sum all the pixel intensities from a gray image that are inside a contour. If so, the method that i think of is to draw that contour on a blank image and fill it , in so making yourself a mask. After that to optimize the process you can also compute the bounding rect of the contour with :

    CvRect cvBoundingRect(CvArr* points, int update=0 );
    

    After this you can make an intermediate image with :

    void cvAddS(const CvArr* src, CvScalar value, CvArr* dst, const CvArr* mask=NULL);
    

    using the value 0, the mask obtained from the contour and setting before as ROI the bounding rect.

    After this, a sum on the resulting image will be a little faster.

    0 讨论(0)
  • 2020-12-30 07:27

    First get all of your contours. Use this information to create a binary image with the white parts being the contour's outline and area. Perform an AND operation on the two images. The result will be the contours and area on a black background. Then just sum all of the pixels in this image.

    0 讨论(0)
  • 2020-12-30 07:29

    To access the contour's point individually follow the code

    vector<vector<Point> > contours;
    ...
    printf("\n Contours pixels \n");
    for(int a=0; a< contours.size(); a++)
    {  
       printf("\nThe contour NO = %d  size = %d \n",a, contours[a].size() );
       for( int b = 0; b < contours[a].size();  b++ )  
       {  
           printf(" [%d, %d] ",contours[a][b].x, contours[a][b].y ); 
       }
    }
    
    0 讨论(0)
提交回复
热议问题