Home > Software engineering >  How to print results from multiple images to CSV file in python?
How to print results from multiple images to CSV file in python?

Time:12-11

I still new with python, and I want to print several propreties of multiple images to a csv file. I have tried How to f.write .append results to CSV but I still can't figure out where I went wrong. So, I very much appreciate your help. Here is my code

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])

Thanks in advance

CodePudding user response:

As stated at https://docs.python.org/3/library/csv.html if csv writter is used with file, file needs to opened with newline.

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

CodePudding user response:

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])
  • Related