-2

I getting counters ellipses in somw picture.How to crop and save each finded ellipses to file? Code:

import numpy as np
import cv2 as cv

hsv_min = np.array((0, 77, 17), np.uint8)
hsv_max = np.array((208, 255, 255), np.uint8)

if __name__ == '__main__':
    fn = 'buttons/bar.png'
    img = cv.imread(fn)
    hsv = cv.cvtColor(img, cv.COLOR_BGR2HSV)
    thresh = cv.inRange(hsv, hsv_min, hsv_max)
    contours0, hierarchy = cv.findContours(thresh.copy(), cv.RETR_TREE, cv.CHAIN_APPROX_SIMPLE)
    for cnt in contours0:
        if len(cnt) > 4:
            print(cnt)
            ellipse = cv.fitEllipse(cnt)
            cv.ellipse(img, ellipse, (0, 255, 255), 2)

    cv.imshow('contours', img)

    cv.waitKey()
    cv.destroyAllWindows()
Tomain
  • 75
  • 1
  • 1
  • 6

1 Answers1

0

To save an image in OpenCV, use imwrite:

filename = 'savedImage.jpg'
cv2.imwrite(filename, img)

If you just want to save each ellipse in a separate image, simply create a new image for each ellipse in the for loop and then use imwrite to save them to the newly created image:

file_num = 0
for cnt in contours0:
    if len(cnt) > 4:
        # create a new image with same dimensions as original image
        ellipse_img = np.zeros((img.shape[0], img.shape[1], 3), dtype = "uint8")
        print(cnt)
        ellipse = cv.fitEllipse(cnt)
        cv.ellipse(ellipse_img, ellipse, (255, 255, 255), 2)

        # save new image
        filename = 'ellipse' + str(file_num) + '.jpg'
        cv.imwrite(filename, ellipse_img)

        # increment counter in filename
        file_num += 1

Additionally, if you want to mask the image as well, showing only the areas inside of the ellipses:

file_num = 0
for cnt in contours0:
    if len(cnt) > 4:
        # create a new image and mask with same dimensions as original image
        ellipse_img = np.zeros_like(img)
        mask = np.zeros_like(img)

        # fit ellipse to the contour, draw filled ellipse on mask, and apply mask to image 
        ellipse = cv.fitEllipse(cnt)
        mask = cv.ellipse(mask, ellipse, (255, 255, 255), -1)
        ellipse_img[mask == 255] = img[mask == 255]

        # save masked image
        filename = 'ellipse' + str(file_num) + '.jpg'
        cv.imwrite(filename, ellipse_img)

        # increment counter in filename
        file_num += 1

Further resources:

How to crop the internal area of a contour? | Documentation of ellipse

adamgy
  • 4,543
  • 3
  • 16
  • 31
  • It should be the image with the single ellipse that you are trying to save. I've edited my answer, check out if it works for you. – adamgy May 30 '20 at 18:14
  • Thanks a lot. Also how to save a picture inside the ellipse? – Tomain May 30 '20 at 18:16
  • You mean you want to crop the image to show only the part that is inside of the ellipse? – adamgy May 30 '20 at 18:18
  • yeaph, i mean it – Tomain May 30 '20 at 18:21
  • I think this question would be a good starting point: [How to crop the internal area of a contour?](https://stackoverflow.com/questions/28759253/how-to-crop-the-internal-area-of-a-contour) – adamgy May 30 '20 at 18:28
  • Hmm to hard for me. Could you give an code to crop in my code? – Tomain May 30 '20 at 19:08
  • I've added some code that masks the image using the ellipses. However if you want to learn this stuff and be able to solve these problems on your own, I suggest diving into the documentation and looking at existing solutions. Solving problems on your own using existing resources is a very powerful way to learn. – adamgy May 30 '20 at 19:47
  • Thanks, so i receive this image https://prnt.sc/sr3vmo, but how to get image like https://prnt.sc/sr3vuu? – Tomain May 31 '20 at 13:29