How to access opencv contour point indexes in python?

Asked
Active3 hr before
Viewed126 times

6 Answers

accessopencvpythoncontour
90%

The following are 30 code examples for showing how to use cv2,contourArea()

Example_snippet/controller/utility/_access.js/ def segment(image, threshold=2. . .
def segment(image, threshold = 25):
   global bg
# find the absolute difference between background and current frame
diff = cv2.absdiff(bg.astype("uint8"), image)

# threshold the diff image so that we get the foreground
thresholded = cv2.threshold(diff, threshold, 255, cv2.THRESH_BINARY)[1]

# get the contours in the thresholded image(_, cnts, _) = cv2.findContours(thresholded.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)

#
return None,
   if no contours detected
if len(cnts) == 0:
   return
else :
   # based on contour area, get the maximum contour which is the hand
segmented = max(cnts, key = cv2.contourArea)
return (thresholded, segmented)

#-- -- -- -- -- -- -- -- -
# MAIN FUNCTION
#-- -- -- -- -- -- -- -- -
load more v
88%

Contour detection can also fail, when the background of the object in the image is full of lines,,The contour-detection algorithms in OpenCV work very well, when the image has a dark background and a well-defined object-of-interest

Example_snippet/controller/utility/_access.js/ import cv2 # read the image i. . .
import cv2

# read the image
image = cv2.imread('input/image_1.jpg')
load more v
72%

Example 2: Using cv2,RETR_EXTERNAL as a Contour retrieval mode and cv2

Example_snippet/controller/utility/_access.js/ boundary points(x,y). . .
boundary points(x, y)
load more v
65%

Is there way to access the contour[i][j] in python?,I am struggling to translate this c++ into python, because the data structures are different, It's being hard making comparisions,OpenCV Python findCountours can be used like this

Example_snippet/controller/utility/_access.js/ static double distanceBtwPoint. . .
static double distanceBtwPoints(const cv::Point a, const cv::Point b)
{
     double xDiff = a.x - b.x;
     double yDiff = a.y - b.y;

     return std::sqrt((xDiff * xDiff) + (yDiff * yDiff));
}

static int findNearestPointIndex(const cv::Point pt, const vector<Point> points)
{
    int nearestpointindex = 0;
    double distance;
    double mindistance = 1e+9;

    for ( size_t i = 0; i < points.size(); i++)
    {
        distance = distanceBtwPoints(pt,points[i]);

        if( distance < mindistance )
        {
            mindistance =  distance;
            nearestpointindex = i;
        }
    }
    return nearestpointindex;
}

int main( int argc, char** argv )
{
    Point pt0;
    int shift=0; // optional value for drawing scaled
    Scalar color = Scalar(0,0,0);

    char* filename = argc >= 2 ? argv[1] : (char*)"test.png";
    Mat img = imread(filename);
    if (img.empty())
        return -1;

    vector<vector<Point> > contours;
    vector<Point> contour;
    findContours( bw, contours, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_NONE );

    contour = contours[0];
    for ( size_t i = 0; i < contours.size(); i++)
    {
        if( contour.size() < contours[i].size() )
            contour = contours[i];
    }

    for ( size_t i = 0; i < contours.size(); i++)
    {
        if( contour != contours[i] && contours[i].size() > 10 )
        {
            for ( size_t j = 0; j <  contours[i].size(); j++)
            {
                pt0 = contours[i][j];
               line(src,pt0,contour[findNearestPointIndex(pt0,contour)],color,1,LINE_8,shift);
            }
        }
    }
}
load more v
75%

Today, I’m going to share my solution to find extreme points along a contour with OpenCV and Python,,Where our goal is to compute the extreme points along the contour of the hand in the image

Example_snippet/controller/utility/_opencv.js/ # import the necessary package. . .
#
import the necessary packages
import imutils
import cv2

# load the image, convert it to grayscale, and blur it slightly
image = cv2.imread("hand_01.png")
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
gray = cv2.GaussianBlur(gray, (5, 5), 0)

# threshold the image, then perform a series of erosions +
   # dilations to remove any small regions of noise
thresh = cv2.threshold(gray, 45, 255, cv2.THRESH_BINARY)[1]
thresh = cv2.erode(thresh, None, iterations = 2)
thresh = cv2.dilate(thresh, None, iterations = 2)

# find contours in thresholded image, then grab the largest
# one
cnts = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL,
   cv2.CHAIN_APPROX_SIMPLE)
cnts = imutils.grab_contours(cnts)
c = max(cnts, key = cv2.contourArea)
load more v
40%

OpenCV Python findCountours can be used like this, How can I impose this condition ? :` if( contour != contours[i] && contours[i],size() > 10 )` – marco Sep 16 '15 at 12:38 ,I am struggling to translate this c++ into python, because the data structures are different

Example_snippet/controller/utility/_opencv.js/ import cv2 contours, hierarchy. . .
import cv2
contours, hierarchy = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
# contours = [array([
   [
      [x1, y1]
   ], ..., [
      [xn, yn]
   ]
]), array([
   [
      [x1, y1]
   ], ..., [
      [xn, yn]
   ]
])]
contour = contours[0] # contours[i], where i = index of the contour
# contour = [
   [
      [x1, y1]
   ],
   [
      [x2, y2]
   ], ..., [
      [xn, yn]
   ]
]
# contour[0] = [
   [x1, y1]
]
# contour[0][0] = [x1, y1]
# contour[0][0][0] = x1
# contour[0][0][1] = y1
load more v