Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import cv2
- import numpy as np
- from fpdf import FPDF
- from pptx import Presentation
- from pptx.util import Inches
- import matplotlib.pyplot as plt
- # Load the image
- image_path = "C:/Users/HAL 9000/Downloads/image2.jpg"
- image = cv2.imread(image_path)
- # Convert to grayscale
- gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
- # Thresholding to create a binary image (air vs particles)
- _, binary_image = cv2.threshold(gray, 100, 255, cv2.THRESH_BINARY)
- # Detect circular particles using HoughCircles
- circles = cv2.HoughCircles(binary_image, cv2.HOUGH_GRADIENT, dp=1, minDist=20, param1=50, param2=30, minRadius=10, maxRadius=100)
- # If circles are found, convert them to integers
- if circles is not None:
- circles = np.round(circles[0, :]).astype("int")
- # Function to detect non-circular particles (based on contour area and perimeter)
- def detect_non_circular_particles(binary_image):
- contours, _ = cv2.findContours(binary_image, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
- non_circular_particles = []
- for contour in contours:
- # Approximate the contour to a polygon and compute its aspect ratio
- epsilon = 0.02 * cv2.arcLength(contour, True)
- approx = cv2.approxPolyDP(contour, epsilon, True)
- # Calculate the aspect ratio (width/height) of the bounding box
- x, y, w, h = cv2.boundingRect(approx)
- aspect_ratio = w / float(h) if h != 0 else 0
- # A non-circular particle will have an aspect ratio different from 1
- if aspect_ratio > 1.5 or aspect_ratio < 0.5:
- non_circular_particles.append(contour)
- return non_circular_particles
- # Detect non-circular particles
- non_circular_particles = detect_non_circular_particles(binary_image)
- # Calculate the statistics for circular particles
- circular_diameters = []
- if circles is not None:
- for circle in circles:
- radius = circle[2]
- circular_diameters.append(2 * radius)
- # Statistics for circular particles
- circular_particles_count = len(circular_diameters)
- average_diameter = np.mean(circular_diameters) if circular_particles_count > 0 else 0
- std_dev_diameter = np.std(circular_diameters) if circular_particles_count > 0 else 0
- # Area of circular and non-circular particles
- circular_area = np.pi * (np.array(circular_diameters) / 2) ** 2
- non_circular_area = sum([cv2.contourArea(cnt) for cnt in non_circular_particles])
- # Area ratio
- total_area = circular_area.sum() + non_circular_area
- area_ratio_circular = circular_area.sum() / total_area
- area_ratio_non_circular = non_circular_area / total_area
- # Saving the segmented images for report purposes
- # Save an image with circular particles marked
- image_with_circles = image.copy()
- if circles is not None:
- for circle in circles:
- cv2.circle(image_with_circles, (circle[0], circle[1]), circle[2], (0, 255, 0), 4)
- cv2.imwrite("C:/Users/HAL 9000/Downloads/circular_particles.png", image_with_circles)
- # Save an image with non-circular particles marked
- image_with_non_circular = image.copy()
- cv2.drawContours(image_with_non_circular, non_circular_particles, -1, (0, 0, 255), 2)
- cv2.imwrite("C:/Users/HAL 9000/Downloads/non_circular_particles.png", image_with_non_circular)
- # --- PDF Generation ---
- pdf = FPDF()
- pdf.add_page()
- # Add title
- pdf.set_font('Arial', 'B', 16)
- pdf.cell(200, 10, txt="Particle Analysis Report", ln=True, align='C')
- # Add statistics for circular particles
- pdf.ln(10)
- pdf.set_font('Arial', '', 12)
- pdf.cell(200, 10, txt=f"Number of Circular Particles: {circular_particles_count}")
- pdf.ln(10)
- pdf.cell(200, 10, txt=f"Average Diameter of Circular Particles: {average_diameter:.2f} units")
- pdf.ln(10)
- pdf.cell(200, 10, txt=f"Standard Deviation of Diameters: {std_dev_diameter:.2f} units")
- pdf.ln(10)
- # Add Area Ratio
- pdf.cell(200, 10, txt=f"Area Ratio (Circular Particles): {area_ratio_circular:.2f}")
- pdf.ln(10)
- pdf.cell(200, 10, txt=f"Area Ratio (Non-Circular Particles): {area_ratio_non_circular:.2f}")
- pdf.ln(10)
- # Add images to the PDF
- pdf.image("C:/Users/HAL 9000/Downloads/circular_particles.png", x=10, y=60, w=90)
- pdf.image("C:/Users/HAL 9000/Downloads/non_circular_particles.png", x=110, y=60, w=90)
- # Save the PDF
- output_pdf = "C:/Users/HAL 9000/Downloads/particle_analysis_report.pdf"
- pdf.output(output_pdf)
- print(f"PDF report generated and saved to {output_pdf}")
- #Generate PPTX presentation with slides for each segment
- presentation = Presentation()
- # Circular particles slide
- slide = presentation.slides.add_slide(presentation.slide_layouts[5])
- slide.shapes.title.text = "Circular Particles"
- slide.shapes.add_picture("C:/Users/HAL 9000/Downloads/circular_particles.png", Inches(1), Inches(1.5), Inches(6), Inches(4.5))
- textbox = slide.shapes.add_textbox(Inches(1), Inches(6), Inches(6), Inches(1))
- textbox.text = f"Number of Circular Particles: {circular_particles_count}\nAverage Diameter: {average_diameter:.2f} units"
- # Non-Circular particles slide
- slide = presentation.slides.add_slide(presentation.slide_layouts[5])
- slide.shapes.title.text = "Non-Circular Particles"
- slide.shapes.add_picture("C:/Users/HAL 9000/Downloads/non_circular_particles.png", Inches(1), Inches(1.5), Inches(6), Inches(4.5))
- textbox = slide.shapes.add_textbox(Inches(1), Inches(6), Inches(6), Inches(1))
- textbox.text = f"Area Ratio (Circular): {area_ratio_circular:.2f}\nArea Ratio (Non-Circular): {area_ratio_non_circular:.2f}"
- #Save the PPTX
- pptx_path = "C:/Users/HAL 9000/Downloads/particle_analysis_report.pptx"
- presentation.save(pptx_path)
- print(f"PPTX report generated and saved to {pptx_path}")
- plt.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement