Ask Your Question
0

How to clip an image by extreme points?

asked 2017-10-02 08:34:03 -0600

Tarcisioflima gravatar image

Hello, I have found the contours of the object in the image with a cv2.findContours and I want to clip the image in the extreme points. However, the image returned is not correct.

Below is the code, I have implemented. Any hint?

def clipping_image( image ):
    '''
    Clip segmented imaged based on the contours of the leaf
    '''
    image = cv2.cvtColor( image, cv2.COLOR_BGR2GRAY );
    segmented, contours, hierarchy = cv2.findContours(image, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
    cnt = contours[0]
    xmin = min(tuple(cnt[cnt[:, :, 0].argmin()][0]))
    ymin = min(tuple(cnt[cnt[:, :, 1].argmin()][0]))
    xmax = max(tuple(cnt[cnt[:, :, 0].argmax()][0]))
    ymax = max(tuple(cnt[cnt[:, :, 1].argmax()][0]))

    clipped = segmented[xmin:xmax, ymin:ymax]

    return clipped

Thank you

edit retag flag offensive close merge delete

Comments

1

this may be helpful

x,y,w,h = cv2.boundingRect(contours[0])
sturkmen gravatar imagesturkmen ( 2017-10-02 09:01:08 -0600 )edit

also: segmented[ymin:ymax, xmin:xmax] (both opencv and numpy are row-major)

berak gravatar imageberak ( 2017-10-03 02:36:50 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
0

answered 2017-11-16 06:18:31 -0600

Tarcisioflima gravatar image

I was able to get the expected results changing my code as below:

def clipping_image( image ):
    '''
    Clip segmented imaged based on the contours of the leaf
    '''
    image = cv2.cvtColor( image, cv2.COLOR_BGR2GRAY );
    segmented, contours, hierarchy = cv2.findContours(image, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
    x, y = [], []

    for contour_line in contours:
        for contour in contour_line:
            x.append(contour[0][0])
            y.append(contour[0][1])

    x1, x2, y1, y2 = min(x), max(x), min(y), max(y)

    clipped = segmented[y1:y2, x1:x2]

    return clipped
edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2017-10-02 08:34:03 -0600

Seen: 5,494 times

Last updated: Nov 16 '17