Home>

Currently, I want to process images in the directory, create another directory called data in the current directory, and save it there.

Specifically, I want to save the thermographic image surrounded by the bounding box as a new image in the data folder.

The code I write doesn't do anything to it and save it.
Also, if you run it with the code belowEndless loopWill be.

Please tell me the processing that I envision. It's too difficult for me as a beginner.

import cv2
import numpy as np
import cv2
import glob
def contour_extraction(image):
    hsv = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
    binary = cv2.inRange(hsv, (20, 0, 0), (50, 255, 255))
    #Contour extraction
    contours = cv2.findContours(binary, cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE)[1]
    #Leave only contours with a certain area or more.
    area_thresh = 10000
    contours = list(filter(lambda x: cv2.contourArea(x) >area_thresh, contours))
    create_box(contours)
def create_box(contours):
    for cnt in contours:
        # Get the rectangle circumscribing the contour.
        x, y, width, height = cv2.boundingRect(cnt)
        print(x, y, width, height)
        # draw.
        cv2.rectangle(image, (x, y), (x + width, y + height), color=(0, 255, 0), thickness=2)
        for images in image:
            cv2.imwrite('Users/XXXX/Desktop/Thermo/data/hogehoge.jpg', images)

path ='./*.jpg'
files = glob.glob(path)
for f in files:
    print(f)
    image = cv2.imread(f)
    contour_extraction(image)
print('end')


Thanks for your cooperation.

  • Answer # 1

    import cv2
    import numpy as np
    import cv2
    import glob
    img_num = 0 # Variable for image numbering
    def contour_extraction(image):
        hsv = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
        binary = cv2.inRange(hsv, (20, 0, 0), (50, 255, 255))
        #Contour extraction
        contours = cv2.findContours(binary, cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE)[1]
        #Leave only contours with a certain area or more.
        area_thresh = 10000
        contours = list(filter(lambda x: cv2.contourArea(x) >area_thresh, contours))
        create_box(contours)
    def create_box(contours):
        global img_num
        for cnt in contours:
            # Get the rectangle circumscribing the contour.
            x, y, width, height = cv2.boundingRect(cnt)
            print(x, y, width, height)
            # draw.
            cv2.rectangle(image, (x, y), (x + width, y + height), color=(0, 255, 0), thickness=2)
            #for images in image: It seems that the loop processing here is not necessary
            save_name ='Users/XXXX/Desktop/Thermo/data/' + img_num +'.jpg' # defines the save name of the image
            cv2.imwrite(save_name, image)
            img_num += 1
    path ='./*.jpg'
    files = glob.glob(path)
    for f in files:
        print(f)
        image = cv2.imread(f)
        contour_extraction(image)
    print('end')


    It's a long time question, but I'm happy if you get used to it.

  • Answer # 2

    Create a directory called data first, and save the file name when savingdata/hogehoge.jpgI wonder if you can do what you intended by doing something like this.