Filter tuple list of contour coordinates
up vote
0
down vote
favorite
I am a bit confused on how I can filter out the contour coordinates (which are in a tuple) and keep only the part of the contour that corresponds to the top segmented layer of this image.
My code is this
img = cv2.imread(image_path)
img = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
blur = cv2.GaussianBlur(img, (1, 1), 1000)
flag, thresh = cv2.threshold(blur, 120, 255, cv2.THRESH_BINARY)
thresh = cv2.dilate(thresh, None)
thresh = cv2.dilate(thresh, None)
thresh = cv2.erode(thresh, None)
thresh = cv2.erode(thresh, None)
im2, contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
contours = sorted(contours, key=cv2.contourArea, reverse=True)[:1]
perimeters = [cv2.arcLength(contours[i], True) for i in range(len(contours))]
listindex = [i for i in range(1) if perimeters[i] > perimeters[0] / 2]
num_of_layers = len(listindex)
imgcont = img.copy()
[cv2.drawContours(imgcont, [contours[i]], 0, (0, 255, 0), 5) for i in listindex]
M = cv2.moments(contours[0])
cx = int(M['m10'] / M['m00'])
cy = int(M['m01'] / M['m00'])
print(cx, cy)
plt.imshow(imgcont, cmap='gray')
python contour
add a comment |
up vote
0
down vote
favorite
I am a bit confused on how I can filter out the contour coordinates (which are in a tuple) and keep only the part of the contour that corresponds to the top segmented layer of this image.
My code is this
img = cv2.imread(image_path)
img = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
blur = cv2.GaussianBlur(img, (1, 1), 1000)
flag, thresh = cv2.threshold(blur, 120, 255, cv2.THRESH_BINARY)
thresh = cv2.dilate(thresh, None)
thresh = cv2.dilate(thresh, None)
thresh = cv2.erode(thresh, None)
thresh = cv2.erode(thresh, None)
im2, contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
contours = sorted(contours, key=cv2.contourArea, reverse=True)[:1]
perimeters = [cv2.arcLength(contours[i], True) for i in range(len(contours))]
listindex = [i for i in range(1) if perimeters[i] > perimeters[0] / 2]
num_of_layers = len(listindex)
imgcont = img.copy()
[cv2.drawContours(imgcont, [contours[i]], 0, (0, 255, 0), 5) for i in listindex]
M = cv2.moments(contours[0])
cx = int(M['m10'] / M['m00'])
cy = int(M['m01'] / M['m00'])
print(cx, cy)
plt.imshow(imgcont, cmap='gray')
python contour
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I am a bit confused on how I can filter out the contour coordinates (which are in a tuple) and keep only the part of the contour that corresponds to the top segmented layer of this image.
My code is this
img = cv2.imread(image_path)
img = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
blur = cv2.GaussianBlur(img, (1, 1), 1000)
flag, thresh = cv2.threshold(blur, 120, 255, cv2.THRESH_BINARY)
thresh = cv2.dilate(thresh, None)
thresh = cv2.dilate(thresh, None)
thresh = cv2.erode(thresh, None)
thresh = cv2.erode(thresh, None)
im2, contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
contours = sorted(contours, key=cv2.contourArea, reverse=True)[:1]
perimeters = [cv2.arcLength(contours[i], True) for i in range(len(contours))]
listindex = [i for i in range(1) if perimeters[i] > perimeters[0] / 2]
num_of_layers = len(listindex)
imgcont = img.copy()
[cv2.drawContours(imgcont, [contours[i]], 0, (0, 255, 0), 5) for i in listindex]
M = cv2.moments(contours[0])
cx = int(M['m10'] / M['m00'])
cy = int(M['m01'] / M['m00'])
print(cx, cy)
plt.imshow(imgcont, cmap='gray')
python contour
I am a bit confused on how I can filter out the contour coordinates (which are in a tuple) and keep only the part of the contour that corresponds to the top segmented layer of this image.
My code is this
img = cv2.imread(image_path)
img = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
blur = cv2.GaussianBlur(img, (1, 1), 1000)
flag, thresh = cv2.threshold(blur, 120, 255, cv2.THRESH_BINARY)
thresh = cv2.dilate(thresh, None)
thresh = cv2.dilate(thresh, None)
thresh = cv2.erode(thresh, None)
thresh = cv2.erode(thresh, None)
im2, contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
contours = sorted(contours, key=cv2.contourArea, reverse=True)[:1]
perimeters = [cv2.arcLength(contours[i], True) for i in range(len(contours))]
listindex = [i for i in range(1) if perimeters[i] > perimeters[0] / 2]
num_of_layers = len(listindex)
imgcont = img.copy()
[cv2.drawContours(imgcont, [contours[i]], 0, (0, 255, 0), 5) for i in listindex]
M = cv2.moments(contours[0])
cx = int(M['m10'] / M['m00'])
cy = int(M['m01'] / M['m00'])
print(cx, cy)
plt.imshow(imgcont, cmap='gray')
python contour
python contour
asked Nov 10 at 20:42
laza
500722
500722
add a comment |
add a comment |
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53243224%2ffilter-tuple-list-of-contour-coordinates%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown