首页 > 解决方案 > 如何获取多个图像的轮廓区域并在数据框中分离

问题描述

我正在尝试计算每个不同图像的轮廓区域(来自文件夹路径),然后转换为数据帧。有人可以给一些建议吗?

该脚本显示我可以计算所有图像的所有轮廓的面积,然后转换为所有轮廓的数据框,但我正在搜索为每个图像将它们分开。

第一个图像所有轮廓,第二个所有轮廓,第三个所有轮廓,等等。

有人可以指导我在循环中找到代码吗?

谢谢

import os
import cv2
import pandas as pd
import glob

#load images from specific path
##########################################################
try: 
    os.makedirs("D:\\NBC_new2020_\\data\\concat\\results")
    #os.makedirs("D:\\NBC_new2020_\\images")

except FileExistsError:
    # directory already exists
    pass   

try: 
    os.makedirs("D:\\NBC_new2020_\\images")
    #os.makedirs("D:\\NBC_new2020_\\images")

except FileExistsError:
    # directory already exists
    pass 
############################################################################

#calculate contour of all images from the path
#############################################################
img = []
contours = []
area = []

    for fn in glob.glob("D:\\NBC_new2020_\\images\\*.png"):
        img = cv2.imread(fn, 0)
        ret, thresh = cv2.threshold(img, 39, 255, 0)
        contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)

        for i in contours:
           area.append(cv2.contourArea(i))   



#print(contours)
dframe = pd.DataFrame(area)
print(dframe)

标签: pythonpandasdataframeimage-processingopencv-contour

解决方案


推荐阅读