如何打印结果从多个图像到CSV文件在python?

wqlqzqxt  于 2022-12-15  发布在  Python
关注(0)|答案(2)|浏览(118)

我对python还是个新手,我想把多张图片的几个属性打印到一个csv文件中。我试过How to f.write .append results to CSV,但我还是找不出哪里出错了。所以,我非常感谢你的帮助。这是我的代码

import csv
import cv2
import glob
import numpy as np
    
    
filename1s = []
widths = []
heights = []
areas = []
rect_areas = []
equi_diameters = []
aspect_ratios = []
extents = []
solidities = []
 
path = 'images/*.png'
  
with open('file.csv','w') as f:
    csv_out = csv.writer(f)
    
    for filename1 in glob.glob(path):
        imge=cv2.imread(filename1) 
        filename1s.append(imge)
        
        img_maskedgray = cv2.cvtColor(imge, cv2.COLOR_BGR2GRAY)
        contours2 = cv2.findContours(img_maskedgray, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)

        contours2 = contours2[0] if len(contours2) == 2 else contours2[1]
        big_contour2 = max(contours2, key=cv2.contourArea, default=None)

        area = cv2.contourArea(big_contour2)
        x,y,width,height = cv2.boundingRect(big_contour2)
        aspect_ratio = float(width)/height # ratio of width to height of bounding rect of the object.
        rect_area = width*height # the ratio of contour area to bounding rectangle area
        extent = float(area)/rect_area
        hull = cv2.convexHull(big_contour2)
        hull_area = cv2.contourArea(hull)
        solidity = float(area)/hull_area
        equi_diameter = np.sqrt(4*area/np.pi) # diameter of the circle whose area is same as the contour area
        
        
        widths.append(width)
        heights.append(height)
        areas.append(area)
        rect_areas.append(rect_area)
        equi_diameters.append(equi_diameter)
        aspect_ratios.append(aspect_ratio)
        extents.append(extent)
        solidities.append(solidity)
        
        csv_out.writerow([filename1, width, height, area, rect_area, equi_diameter, aspect_ratio, extent, solidity])

先谢了

pengsaosao

pengsaosao1#

https://docs.python.org/3/library/csv.html所述,如果csv writer与文件一起使用,则需要使用换行符打开文件。

with open('file.csv','w', newline='') as f:
3zwjbxry

3zwjbxry2#

import csv
import cv2
import glob
import numpy as np
    
    
filename1s = []
widths = []
heights = []
areas = []
rect_areas = []
equi_diameters = []
aspect_ratios = []
extents = []
solidities = []
 
path = 'images/*.png'
  
with open('file.csv','w') as f:
    csv_out = csv.writer(f)
    
    for filename1 in glob.glob(path):
        imge=cv2.imread(filename1) 
        filename1s.append(imge)
        
        img_maskedgray = cv2.cvtColor(imge, cv2.COLOR_BGR2GRAY)
        contours2 = cv2.findContours(img_maskedgray, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)

        contours2 = contours2[0] if len(contours2) == 2 else contours2[1]
        big_contour2 = max(contours2, key=cv2.contourArea, default=None)

        area = cv2.contourArea(big_contour2)
        x,y,width,height = cv2.boundingRect(big_contour2)
        aspect_ratio = float(width)/height # ratio of width to height of bounding rect of the object.
        rect_area = width*height # the ratio of contour area to bounding rectangle area
        extent = float(area)/rect_area
        hull = cv2.convexHull(big_contour2)
        hull_area = cv2.contourArea(hull)
        solidity = float(area)/hull_area
        equi_diameter = np.sqrt(4*area/np.pi) # diameter of the circle whose area is same as the contour area
        
        
        widths.append(width)
        heights.append(height)
        areas.append(area)
        rect_areas.append(rect_area)
        equi_diameters.append(equi_diameter)
        aspect_ratios.append(aspect_ratio)
        extents.append(extent)
        solidities.append(solidity)
        
        csv_out.writerow([filename1, width, height, area, rect_area, equi_diameter, aspect_ratio, extent, solidity])

相关问题