Ask Your Question

Revision history [back]

How do i automatically load a new image once i have closed the previous image on OpenCV

I have a code which allows me to open an image from a folder, lets me draw and label the images and once i close the window, it will save the labeled image and the annotations in separate folders. Now the problem is that I want to open the next image automatically after I have closed the annotated image. Is there any way to do it? Here is my code:

    import cv2
    import numpy as np
    import math
    import os
drawing = False
colours = [(255, 0, 0), (0, 255, 0), (0, 0, 255), (204, 0, 204), (0, 155, 255), (0, 223, 255), (238, 220, 130),
           (255, 79, 79), (161, 0, 244), (32, 178, 170), (170, 178, 32)]
nn1=0
img_dir='./3/'
txt_dir='./texts/'
sample_dir='./samples/'
def draw_circle(event, x, y, flags, param):
    global x1, y1, drawing, radius, num, img, img2, z, circleProps, RGB
    if event == cv2.EVENT_LBUTTONDOWN:
        drawing = True
        x1, y1 = x, y
        radius = int(math.hypot(x - x1, y - y1))

        cv2.circle(img, (x1, y1), radius, (255, 0, 0), 1)




    elif event == cv2.EVENT_MOUSEMOVE:
        if drawing == True:
            a, b = x, y
            if a != x & b != y:
                img = img2.copy()
                radius = int(math.hypot(a - x1, b - y1))
                cv2.circle(img, (x1, y1), radius, (255, 0, 0), 1)




    elif event == cv2.EVENT_LBUTTONUP:
        drawing = False
        num += 1
        radius = int(math.hypot(x - x1, y - y1))

        z = (z + 1) % (len(colours) - 1)
        circle_drawn = cv2.circle(img, (x1, y1), 10, colours[z], 1)  # change radius
        RGB = cv2.mean(circle_drawn)
        circleProps += "  %d      " % num + "%d " % x1 + "%d    " % y1 + "15        " + "{0} \n".format(RGB)
        cv2.line(img, (x1, y1), (x1 + 3, y1), (0, 0, 0), 1)
        cv2.line(img, (x1, y1), (x1 - 3, y1), (0, 0, 0), 1)
        cv2.line(img, (x1, y1), (x1, y1 + 3), (0, 0, 0), 1)
        cv2.line(img, (x1, y1), (x1, y1 - 3), (0, 0, 0), 1)

        font = cv2.FONT_HERSHEY_PLAIN
        cv2.putText(img, str(num), (x1 + radius, y1 + radius), font, 1, (200, 255, 155), 1, cv2.LINE_AA)
        img2 = img.copy()

    elif event == cv2.EVENT_RBUTTONUP:
        pass


def handleKeyboard():
    if cv2.waitKey(20) == 38:
        print('38')


if __name__ == "__main__":
    num = 0
    z = 0
    #nn1=3
    counter='%04d' %nn1
    nn1 += 1
    windowName = 'DepthOut_'+counter
    circleProps = "|No. | x1 | y1 | radius | RGB \n"
    fileName="DepthOut_"+counter
    print('filename', fileName)
    im_gray = cv2.imread(img_dir+fileName+".bmp", cv2.IMREAD_COLOR)  # change img filename here
    img = cv2.applyColorMap(im_gray, cv2.COLORMAP_BONE)
    img2 = img.copy()

    cv2.namedWindow(windowName)
    cv2.setMouseCallback(windowName, draw_circle)

    noFiles=len(img_dir)
    print('number of files:', noFiles)


    while (True):
        cv2.imshow(windowName, img)
        cv2.rectangle(img, (0, 0), (50, 50), (0, 0, 255), 2)
        # handleKeyboard()
        if cv2.waitKey(20) == 27:
            text_file = open(txt_dir+fileName+'.txt', "w")  # change txt filename here
            text_file.write(circleProps)
            cv2.imwrite(sample_dir+fileName+'.png', img)
            text_file.close()

            break

            nn1 += 1

    print('nn1:', nn1)



cv2.destroyAllWindows()
click to hide/show revision 2
retagged

updated 2018-06-07 02:58:39 -0600

berak gravatar image

How do i automatically load a new image once i have closed the previous image on OpenCV

I have a code which allows me to open an image from a folder, lets me draw and label the images and once i close the window, it will save the labeled image and the annotations in separate folders. Now the problem is that I want to open the next image automatically after I have closed the annotated image. Is there any way to do it? Here is my code:

    import cv2
    import numpy as np
    import math
    import os
drawing = False
colours = [(255, 0, 0), (0, 255, 0), (0, 0, 255), (204, 0, 204), (0, 155, 255), (0, 223, 255), (238, 220, 130),
           (255, 79, 79), (161, 0, 244), (32, 178, 170), (170, 178, 32)]
nn1=0
img_dir='./3/'
txt_dir='./texts/'
sample_dir='./samples/'
def draw_circle(event, x, y, flags, param):
    global x1, y1, drawing, radius, num, img, img2, z, circleProps, RGB
    if event == cv2.EVENT_LBUTTONDOWN:
        drawing = True
        x1, y1 = x, y
        radius = int(math.hypot(x - x1, y - y1))

        cv2.circle(img, (x1, y1), radius, (255, 0, 0), 1)




    elif event == cv2.EVENT_MOUSEMOVE:
        if drawing == True:
            a, b = x, y
            if a != x & b != y:
                img = img2.copy()
                radius = int(math.hypot(a - x1, b - y1))
                cv2.circle(img, (x1, y1), radius, (255, 0, 0), 1)




    elif event == cv2.EVENT_LBUTTONUP:
        drawing = False
        num += 1
        radius = int(math.hypot(x - x1, y - y1))

        z = (z + 1) % (len(colours) - 1)
        circle_drawn = cv2.circle(img, (x1, y1), 10, colours[z], 1)  # change radius
        RGB = cv2.mean(circle_drawn)
        circleProps += "  %d      " % num + "%d " % x1 + "%d    " % y1 + "15        " + "{0} \n".format(RGB)
        cv2.line(img, (x1, y1), (x1 + 3, y1), (0, 0, 0), 1)
        cv2.line(img, (x1, y1), (x1 - 3, y1), (0, 0, 0), 1)
        cv2.line(img, (x1, y1), (x1, y1 + 3), (0, 0, 0), 1)
        cv2.line(img, (x1, y1), (x1, y1 - 3), (0, 0, 0), 1)

        font = cv2.FONT_HERSHEY_PLAIN
        cv2.putText(img, str(num), (x1 + radius, y1 + radius), font, 1, (200, 255, 155), 1, cv2.LINE_AA)
        img2 = img.copy()

    elif event == cv2.EVENT_RBUTTONUP:
        pass


def handleKeyboard():
    if cv2.waitKey(20) == 38:
        print('38')


if __name__ == "__main__":
    num = 0
    z = 0
    #nn1=3
    counter='%04d' %nn1
    nn1 += 1
    windowName = 'DepthOut_'+counter
    circleProps = "|No. | x1 | y1 | radius | RGB \n"
    fileName="DepthOut_"+counter
    print('filename', fileName)
    im_gray = cv2.imread(img_dir+fileName+".bmp", cv2.IMREAD_COLOR)  # change img filename here
    img = cv2.applyColorMap(im_gray, cv2.COLORMAP_BONE)
    img2 = img.copy()

    cv2.namedWindow(windowName)
    cv2.setMouseCallback(windowName, draw_circle)

    noFiles=len(img_dir)
    print('number of files:', noFiles)


    while (True):
        cv2.imshow(windowName, img)
        cv2.rectangle(img, (0, 0), (50, 50), (0, 0, 255), 2)
        # handleKeyboard()
        if cv2.waitKey(20) == 27:
            text_file = open(txt_dir+fileName+'.txt', "w")  # change txt filename here
            text_file.write(circleProps)
            cv2.imwrite(sample_dir+fileName+'.png', img)
            text_file.close()

            break

            nn1 += 1

    print('nn1:', nn1)



cv2.destroyAllWindows()