661. Image Smoother

本文介绍了一种基于遍历数组实现的图像平滑算法。通过计算每个像素及其相邻像素的平均值来达到平滑效果。该算法适用于二维图像矩阵。

遍历一遍数组即可:

class Solution {
public:
    vector<vector<int>> imageSmoother(vector<vector<int>>& M) {
        vector<vector<int>> res;
        for(int i=0;i<M.size();i++)
        {
            vector<int> temp;
            for(int j=0;j<M[i].size();j++)
            {
                int count=1,sum=M[i][j];
                if(j-1>=0)
                {
                    count++;
                    sum+=M[i][j-1];
                }
               if(j+1<M[i].size())
                {
                    count++;
                    sum+=M[i][j+1];
                }
                if(i-1>=0)
                {
                    count++;
                    sum+=M[i-1][j];
                    if(j-1>=0)
                    {
                        count++;
                        sum+=M[i-1][j-1];
                    }
                    if(j+1<M[i].size())
                    {
                        count++;
                        sum+=M[i-1][j+1];
                    }
                    
                }
                if(i+1<M.size())
                {
                    count++;
                    sum+=M[i+1][j];
                    if(j-1>=0)
                    {
                        count++;
                        sum+=M[i+1][j-1];
                    }
                    if(j+1<M[i].size())
                    {
                        count++;
                        sum+=M[i+1][j+1];
                    }
                }
                
                
                temp.push_back(sum/count);
                
            }
            res.push_back(temp);
        }
        return res;
    }
};

 

对于该段代码:# Import necessary libraries for GUI and image processing import tkinter as tk from tkinter import filedialog, ttk, messagebox import numpy as np from PIL import Image, ImageTk, ImageDraw import math import time # Class to display algorithm information in a separate window class AlgorithmInfoWindow: def __init__(self, parent): # Create a new window with fade-in effect self.window = tk.Toplevel(parent) self.window.title("Canny Edge Detection - Algorithm Information") self.window.geometry("800x600") self.window.configure(bg='#f0f0f0') # Make the window float on top of the parent window self.window.transient(parent) self.window.grab_set() # Set initial transparency for fade-in effect self.window.attributes('-alpha', 0.0) # Configure styles for labels and frames style = ttk.Style() style.configure('Info.TLabel', font=('Helvetica', 11), background='#f0f0f0', wraplength=700) style.configure('InfoTitle.TLabel', font=('Helvetica', 14, 'bold'), background='#f0f0f0', foreground='#2c3e50') style.configure('InfoSection.TFrame', background='#ffffff', relief='solid') style.configure('Hover.TFrame', background='#e8f0fe') # Create main frame with custom canvas for smooth scrolling main_frame = ttk.Frame(self.window, style='InfoSection.TFrame') main_frame.pack(fill=tk.BOTH, expand=True, padx=20, pady=20) # Create canvas with custom scrolling self.canvas = tk.Canvas(main_frame, bg='#ffffff', highlightthickness=0, relief='flat') scrollbar = ttk.Scrollbar(main_frame, orient="vertical", command=self.smooth_scroll) self.content_frame = ttk.Frame(self.canvas, style='InfoSection.TFrame') # Configure scrolling self.canvas.configure(yscrollcommand=scrollbar.set) # Pack scrollbar and canvas scrollbar.pack(side="right", fill="y") self.canvas.pack(side="left", fill="both", expand=True) # Create window in canvas self.canvas_frame = self.canvas.create_window( (0, 0), window=self.content_frame, anchor="nw", width=self.canvas.winfo_reqwidth() ) # Add sections with animation delays self.sections = [ ("Canny Edge Detection Algorithm", """The Canny edge detection algorithm is a multi-stage algorithm developed by John F. Canny in 1986. It is considered one of the most robust edge detection algorithms."""), ("1. Grayscale Conversion", """Convert the image to grayscale using weighted sum: gray = 0.2989 * R + 0.5870 * G + 0.1140 * B This weights are based on human perception of color."""), ("2. Gaussian Blur", """Apply Gaussian blur to reduce noise: - Create 5x5 Gaussian kernel using the formula: G(x,y) = (1/2πσ²)e^(-(x²+y²)/2σ²) - Convolve image with kernel - Reduces noise while preserving edges"""), ("3. Gradient Calculation", """Calculate intensity gradients: - Apply Sobel operators in x and y directions - Find gradient magnitude: √(Gx² + Gy²) - Find gradient direction: θ = arctan(Gy/Gx) Sobel operators: X = [[-1, 0, 1], [-2, 0, 2], [-1, 0, 1]] Y = [[-1, -2, -1], [ 0, 0, 0], [ 1, 2, 1]]"""), ("4. Non-Maximum Suppression", """Thin edges by suppressing non-maximum values: 1. Round gradient direction to nearest 45° 2. Compare with pixels in gradient direction 3. Suppress if not local maximum This creates thin, precise edges."""), ("5. Double Thresholding", """Identify strong and weak edges: - High threshold (strong): typically 0.15 * max - Low threshold (weak): typically 0.05 * max Creates three categories: - Strong edges (keep) - Weak edges (evaluate) - Non-edges (discard)"""), ("6. Edge Tracking by Hysteresis", """Connect edges using hysteresis: 1. Start with strong edges 2. Recursively add connected weak edges 3. Remove isolated weak edges This creates continuous edge lines.""") ] # Add sections with animation self.section_frames = [] for i, (title, content) in enumerate(self.sections): self.window.after(i * 100, lambda t=title, c=content: self.add_section_with_animation(t, c)) # Configure canvas scrolling self.content_frame.bind('<Configure>', self.on_frame_configure) self.canvas.bind('<Configure>', self.on_canvas_configure) # Bind mouse wheel for smooth scrolling self.canvas.bind_all('<MouseWheel>', self.on_mousewheel) # Close button with hover effect self.close_btn = ttk.Button(self.window, text="Close", command=self.close_with_animation, style='Custom.TButton') self.close_btn.pack(pady=10) # Start fade-in animation self.fade_in() def fade_in(self, alpha=0.0): """Animate window fade in""" if alpha < 1.0: alpha += 0.1 self.window.attributes('-alpha', alpha) self.window.after(20, lambda: self.fade_in(alpha)) def fade_out(self, alpha=1.0): """Animate window fade out""" if alpha > 0: alpha -= 0.1 self.window.attributes('-alpha', alpha) self.window.after(20, lambda: self.fade_out(alpha)) else: self.window.destroy() def close_with_animation(self): """Close window with fade-out animation""" self.fade_out() def add_section_with_animation(self, title, content): """Add a section with slide-in animation""" frame = ttk.Frame(self.content_frame, style='InfoSection.TFrame') frame.pack(fill=tk.X, pady=10, padx=10) frame.pack_propagate(False) # Prevent size changes # Create content title_label = ttk.Label(frame, text=title, style='InfoTitle.TLabel') title_label.pack(anchor='w', pady=(5, 0)) content_label = ttk.Label(frame, text=content, style='Info.TLabel') content_label.pack(anchor='w', pady=(5, 10)) # Add hover effect frame.bind('<Enter>', lambda e: self.on_section_hover(frame, True)) frame.bind('<Leave>', lambda e: self.on_section_hover(frame, False)) # Animate frame height frame.update() required_height = title_label.winfo_reqheight() + content_label.winfo_reqheight() + 20 frame.configure(height=1) self.animate_frame_height(frame, required_height) self.section_frames.append(frame) def animate_frame_height(self, frame, target_height, current_height=1): """Animate frame height smoothly""" if current_height < target_height: current_height += (target_height - current_height) * 0.2 if current_height < target_height - 1: frame.configure(height=int(current_height)) self.window.after(10, lambda: self.animate_frame_height(frame, target_height, current_height)) else: frame.configure(height=target_height) frame.pack_propagate(True) def on_section_hover(self, frame, entering): """Handle section hover effect""" frame.configure(style='Hover.TFrame' if entering else 'InfoSection.TFrame') def smooth_scroll(self, *args): """Implement smooth scrolling""" if len(args) > 1: self.canvas.yview_moveto(args[1]) else: # Use smoother scrolling with acceleration amount = int(args[0]) # Apply scrolling with acceleration effect if amount != 0: for i in range(3): factor = 0.7 ** i # Decreasing factor for deceleration scroll_amount = int(amount * factor) if amount * factor >= 1 or amount * factor <= -1 else amount self.window.after(i * 5, lambda a=scroll_amount: self.canvas.yview_scroll(a, 'units')) def on_mousewheel(self, event): """Handle smooth mousewheel scrolling with improved animation""" # Get the delta value and normalize it delta = -1 * (event.delta // 120) # Use more steps with smaller increments for smoother animation steps = 15 # Increased steps for smoother animation # Apply scrolling with cubic deceleration curve for i in range(steps): factor = 1 - (i / steps) ** 3 # Cubic deceleration for smoother stop scroll_amount = int(delta * 2 * factor) # Multiply by 2 for better initial momentum scroll_amount = max(1, scroll_amount) if scroll_amount > 0 else min(-1, scroll_amount) # Apply with increasing delay for natural deceleration self.window.after(i * 5, lambda a=scroll_amount: self.canvas.yview_scroll(a, 'units')) def on_frame_configure(self, event=None): """Reset scroll region when content frame size changes""" self.canvas.configure(scrollregion=self.canvas.bbox("all")) def on_canvas_configure(self, event): """Update canvas window size when canvas is resized""" self.canvas.itemconfig(self.canvas_frame, width=event.width) # Main class for the Canny Edge Detection tool class CannyEdgeDetector: def __init__(self): # Initialize the main window self.window = tk.Tk() self.window.title("Canny Edge Detection Tool") self.window.geometry("1200x800") self.window.configure(bg='#f0f0f0') # Set theme for the application style = ttk.Style() style.theme_use('clam') # Configure styles for frames and buttons style.configure('Custom.TFrame', background='#f0f0f0') style.configure('Custom.TButton', padding=10, font=('Helvetica', 10, 'bold')) style.configure('Title.TLabel', font=('Helvetica', 28, 'bold'), # Increased font size background='#f0f0f0', foreground='#2c3e50') style.configure('Subtitle.TLabel', font=('Helvetica', 12), background='#f0f0f0', foreground='#34495e') style.configure('Progress.Horizontal.TProgressbar', background='#2ecc71', troughcolor='#ecf0f1', bordercolor='#bdc3c7') # Create main canvas for scrolling self.main_canvas = tk.Canvas(self.window, bg='#f0f0f0', highlightthickness=0) self.scrollbar = ttk.Scrollbar(self.window, orient="vertical", command=self.smooth_scroll) self.main_canvas.configure(yscrollcommand=self.scrollbar.set) # Pack scrollbar and canvas self.scrollbar.pack(side="right", fill="y") self.main_canvas.pack(side="left", fill="both", expand=True) # Create main frame inside canvas self.main_frame = ttk.Frame(self.main_canvas, padding="20", style='Custom.TFrame') self.canvas_frame = self.main_canvas.create_window( (0, 0), window=self.main_frame, anchor="nw", width=self.main_canvas.winfo_reqwidth() ) # Title and buttons frame title_frame = ttk.Frame(self.main_frame, style='Custom.TFrame') title_frame.grid(row=0, column=0, columnspan=2, sticky='ew') title_frame.grid_columnconfigure(0, weight=1) # Make middle column expandable # Title (centered) self.title_label = ttk.Label(title_frame, text="Canny Edge Detection", style='Title.TLabel', anchor='center') self.title_label.grid(row=0, column=0, pady=(0, 20), sticky='ew') # Centered title # Button frame self.button_frame = ttk.Frame(self.main_frame, style='Custom.TFrame') self.button_frame.grid(row=1, column=0, columnspan=2, pady=(0, 20)) # Buttons (all in one line) self.choose_btn = ttk.Button(self.button_frame, text="Choose Image", command=self.load_image, style='Custom.TButton') self.choose_btn.grid(row=0, column=0, padx=10) self.process_btn = ttk.Button(self.button_frame, text="Process", command=self.process_image_with_progress, style='Custom.TButton') self.process_btn.grid(row=0, column=1, padx=10) self.reset_btn = ttk.Button(self.button_frame, text="Reset", command=self.reset_images, style='Custom.TButton') self.reset_btn.grid(row=0, column=2, padx=10) # Info button (next to reset button) self.info_btn = ttk.Button(self.button_frame, text="ℹ️ Algorithm Info", command=self.show_algorithm_info, style='Custom.TButton') self.info_btn.grid(row=0, column=3, padx=10) # Progress frame self.progress_frame = ttk.Frame(self.main_frame, style='Custom.TFrame') self.progress_frame.grid(row=2, column=0, columnspan=2, pady=(0, 20)) # Progress bar self.progress_var = tk.DoubleVar() self.progress_bar = ttk.Progressbar(self.progress_frame, variable=self.progress_var, maximum=100, mode='determinate', length=400, style='Progress.Horizontal.TProgressbar') self.progress_bar.grid(row=0, column=0, padx=(0, 10)) # Progress percentage label self.progress_label = ttk.Label(self.progress_frame, text="0%", style='Subtitle.TLabel') self.progress_label.grid(row=0, column=1) # Hide progress frame initially self.progress_frame.grid_remove() # Status label self.status_label = ttk.Label(self.main_frame, text="", style='Subtitle.TLabel') self.status_label.grid(row=3, column=0, columnspan=2, pady=(0, 20)) # Image frames self.create_image_frame("Original Image", 4, 0) self.create_image_frame("Edge Detected Image", 4, 1) # Initialize variables self.current_image = None self.processed_image = None # Load default square image self.create_default_square_image() # Configure grid weights for responsive layout self.window.grid_rowconfigure(0, weight=1) self.window.grid_columnconfigure(0, weight=1) self.main_frame.grid_rowconfigure(4, weight=1) # Make image frames expandable self.main_frame.grid_columnconfigure(0, weight=1) self.main_frame.grid_columnconfigure(1, weight=1) # Bind events for smooth scrolling with improved responsiveness self.main_canvas.bind('<Configure>', self.on_canvas_configure) self.main_frame.bind('<Configure>', self.on_frame_configure) self.main_canvas.bind_all('<MouseWheel>', self.on_mousewheel) # Ensure buttons stay visible during processing self.button_frame.lift() self.progress_frame.lift() def create_default_square_image(self): """Create default square image as shown in screenshot""" # Create larger image to better fill the frame size = 400 # Increased size img = Image.new('RGB', (size, size), 'black') draw = ImageDraw.Draw(img) # Calculate sizes for squares outer_size = int(size * 0.6) # 60% of total size inner_size = int(outer_size * 0.3) # 30% of outer square # Calculate positions outer_offset = (size - outer_size) // 2 outer_box = [(outer_offset, outer_offset), (outer_offset + outer_size, outer_offset + outer_size)] # Draw outer white square draw.rectangle(outer_box, fill='white') # Calculate inner square position inner_offset = (size - inner_size) // 2 inner_box = [(inner_offset, inner_offset), (inner_offset + inner_size, inner_offset + inner_size)] # Draw inner black square draw.rectangle(inner_box, fill='black') self.current_image = np.array(img) self.display_image(img, self.original_image_label) def show_algorithm_info(self): """Show algorithm information window""" AlgorithmInfoWindow(self.window) def reset_images(self): """Reset to default square image""" self.create_default_square_image() if self.processed_image_label: self.processed_image_label.configure(image='') self.hide_progress() self.status_label.config(text="Reset complete") def update_progress(self, value, status_text): """Update progress bar, percentage and status text""" self.progress_var.set(value) self.progress_label.config(text=f"{int(value)}%") self.status_label.config(text=status_text) self.window.update() def hide_progress(self): """Hide progress elements""" self.progress_frame.grid_remove() self.status_label.config(text="") self.progress_var.set(0) self.progress_label.config(text="0%") def create_image_frame(self, title, row, column): """Create a frame for displaying images with a title and border""" # Create main frame with fixed size and gray background frame = ttk.Frame(self.main_frame, padding="10", relief="solid", borderwidth=1) frame.grid(row=row, column=column, padx=5, pady=(0, 10), sticky="nsew") # Force both frames to have identical width self.main_frame.grid_columnconfigure(0, weight=1, uniform="equal") # Use uniform to ensure equal size self.main_frame.grid_columnconfigure(1, weight=1, uniform="equal") # Use uniform to ensure equal size # Configure frame size with optimal dimensions for small screens frame.grid_propagate(False) # Prevent frame from resizing to content frame.configure(width=350, height=350) # Reduced dimensions for better small screen compatibility # Title with enhanced styling style = ttk.Style() style.configure('FrameTitle.TLabel', font=('Helvetica', 14, 'bold'), foreground='#2c3e50', background='#e0e0e0', padding=5) # Title container frame with background title_container = ttk.Frame(frame, style='Custom.TFrame') title_container.pack(fill="x", pady=(0, 10)) title_container.configure(height=40) title_label = ttk.Label(title_container, text=title, style='FrameTitle.TLabel', anchor='center') title_label.pack(fill="x", expand=True) # Create image container frame with gray background image_container = ttk.Frame(frame, style='Custom.TFrame') image_container.pack(expand=True, fill="both", padx=5, pady=5) image_container.configure(width=480, height=430) # Image label with gray background image_label = ttk.Label(image_container) image_label.pack(expand=True, fill="both") # Save button container save_container = ttk.Frame(frame, style='Custom.TFrame') save_container.pack(fill="x", pady=(5, 0)) # Save button with enhanced style style.configure('Save.TButton', font=('Helvetica', 10), padding=5) save_btn = ttk.Button(save_container, text="Save Image", style='Save.TButton', command=lambda: self.save_image(column)) save_btn.pack(pady=5) if column == 0: self.original_image_label = image_label else: self.processed_image_label = image_label def display_image(self, image, label): """ Display numpy array image in GUI label Args: image: numpy array of image data label: target label widget for display Maintains aspect ratio while fitting to display area """ if image: # Use fixed dimensions for both frames to ensure they're identical frame_width = 480 frame_height = 430 # Calculate scaling ratio while preserving aspect ratio img_width, img_height = image.size width_ratio = frame_width / img_width height_ratio = frame_height / img_height scale_ratio = min(width_ratio, height_ratio) # Calculate new size new_width = int(img_width * scale_ratio) new_height = int(img_height * scale_ratio) # Resize image resized_image = image.resize((new_width, new_height), Image.Resampling.LANCZOS) # Create new image with gray background final_image = Image.new('RGB', (frame_width, frame_height), '#f0f0f0') # Calculate position to center the image x_offset = (frame_width - new_width) // 2 y_offset = (frame_height - new_height) // 2 # Paste resized image onto background final_image.paste(resized_image, (x_offset, y_offset)) # Convert to PhotoImage and display photo = ImageTk.PhotoImage(final_image) label.configure(image=photo) label.image = photo # Keep reference def save_image(self, image_type): """Save the image to disk""" if image_type == 0 and self.current_image is not None: image_to_save = Image.fromarray(self.current_image) title = "Save Original Image" elif image_type == 1 and hasattr(self, 'processed_image'): image_to_save = Image.fromarray((self.processed_image * 255).astype(np.uint8)) title = "Save Edge Detected Image" else: messagebox.showwarning("Warning", "No image to save!") return # Ask for save location file_path = filedialog.asksaveasfilename( title=title, defaultextension=".png", filetypes=[ ("PNG files", "*.png"), ("JPEG files", "*.jpg"), ("All files", "*.*") ] ) if file_path: try: image_to_save.save(file_path) messagebox.showinfo("Success", "Image saved successfully!") except Exception as e: messagebox.showerror("Error", f"Failed to save image: {str(e)}") def process_image_with_progress(self): """ Execute edge detection pipeline with progress tracking Steps: 1. Input validation 2. Grayscale conversion 3. Gaussian blur 4. Gradient calculation 5. Non-max suppression 6. Double thresholding 7. Hysteresis edge tracking """ if self.current_image is None: messagebox.showwarning("Warning", "Please select an image first!") return self.progress_frame.grid() # Show progress frame self.status_label.grid() # Show status label try: # Convert to grayscale self.update_progress(20, "Converting to grayscale...") time.sleep(0.3) # Simulate processing time gray_image = self.to_grayscale(self.current_image) # Apply Gaussian blur self.update_progress(40, "Applying Gaussian blur...") time.sleep(0.3) blurred = self.apply_gaussian_blur(gray_image) # Calculate gradients self.update_progress(60, "Calculating gradients...") time.sleep(0.3) gradient_magnitude, gradient_direction = self.sobel_filters(blurred) # Apply non-maximum suppression self.update_progress(70, "Applying non-maximum suppression...") time.sleep(0.3) suppressed = self.non_maximum_suppression(gradient_magnitude, gradient_direction) # Apply double threshold self.update_progress(80, "Applying double threshold...") time.sleep(0.3) strong_edges, weak_edges = self.double_threshold(suppressed) # Apply hysteresis self.update_progress(90, "Applying hysteresis...") time.sleep(0.3) final_edges = self.hysteresis(strong_edges, weak_edges) # Store processed image self.processed_image = final_edges # Display result self.update_progress(100, "Complete!") display_image = Image.fromarray((final_edges * 255).astype(np.uint8)) self.display_image(display_image, self.processed_image_label) # Hide progress elements after a delay self.window.after(1000, self.hide_progress) except Exception as e: messagebox.showerror("Error", f"An error occurred: {str(e)}") self.hide_progress() def load_image(self): """ Load an image through file dialog Supported formats: JPEG, PNG, BMP Updates original image display Handles common file errors """ file_path = filedialog.askopenfilename( filetypes=[ ("Image files", "*.jpg;*.jpeg;*.png;*.bmp;*.gif"), ("All files", "*.*") ] ) if file_path: try: # Load image image = Image.open(file_path) # Convert to RGB if necessary if image.mode != 'RGB': image = image.convert('RGB') # Store original image for processing self.current_image = np.array(image) # Display image with proper scaling self.display_image(image, self.original_image_label) # Clear processed image if self.processed_image_label: self.processed_image_label.configure(image='') self.status_label.config(text="Image loaded successfully") except Exception as e: messagebox.showerror("Error", f"Failed to load image: {str(e)}") self.status_label.config(text="Failed to load image") def to_grayscale(self, image): """Convert RGB image to grayscale using manual implementation""" if len(image.shape) == 3: return np.dot(image[..., :3], [0.2989, 0.5870, 0.1140]).astype(np.float32) return image def gaussian_kernel(self, size, sigma=1.4): """Generate Gaussian kernel manually""" kernel = np.zeros((size, size)) center = size // 2 for x in range(size): for y in range(size): x_dist = x - center y_dist = y - center kernel[x, y] = (1 / (2 * np.pi * sigma ** 2)) * np.exp(-(x_dist ** 2 + y_dist ** 2) / (2 * sigma ** 2)) return kernel / np.sum(kernel) def apply_gaussian_blur(self, image, kernel_size=5): """Apply Gaussian blur manually""" kernel = self.gaussian_kernel(kernel_size) padding = kernel_size // 2 padded = np.pad(image, padding, mode='edge') output = np.zeros_like(image) for i in range(image.shape[0]): for j in range(image.shape[1]): output[i, j] = np.sum( padded[i:i + kernel_size, j:j + kernel_size] * kernel ) return output def sobel_filters(self, image): """Apply Sobel filters manually""" Gx = np.array([[-1, 0, 1], [-2, 0, 2], [-1, 0, 1]]) Gy = np.array([[-1, -2, -1], [0, 0, 0], [1, 2, 1]]) padding = 1 padded = np.pad(image, padding, mode='edge') gradient_x = np.zeros_like(image) gradient_y = np.zeros_like(image) for i in range(image.shape[0]): for j in range(image.shape[1]): gradient_x[i, j] = np.sum( padded[i:i + 3, j:j + 3] * Gx ) gradient_y[i, j] = np.sum( padded[i:i + 3, j:j + 3] * Gy ) gradient_magnitude = np.sqrt(gradient_x ** 2 + gradient_y ** 2) gradient_direction = np.arctan2(gradient_y, gradient_x) return gradient_magnitude, gradient_direction def non_maximum_suppression(self, gradient_magnitude, gradient_direction): """Apply non-maximum suppression""" height, width = gradient_magnitude.shape output = np.zeros_like(gradient_magnitude) # Convert angles from radians to degrees angle = gradient_direction * 180 / np.pi angle[angle < 0] += 180 for i in range(1, height - 1): for j in range(1, width - 1): q = 255 r = 255 # Angle 0 if (0 <= angle[i, j] < 22.5) or (157.5 <= angle[i, j] <= 180): q = gradient_magnitude[i, j + 1] r = gradient_magnitude[i, j - 1] # Angle 45 elif (22.5 <= angle[i, j] < 67.5): q = gradient_magnitude[i + 1, j - 1] r = gradient_magnitude[i - 1, j + 1] # Angle 90 elif (67.5 <= angle[i, j] < 112.5): q = gradient_magnitude[i + 1, j] r = gradient_magnitude[i - 1, j] # Angle 135 elif (112.5 <= angle[i, j] < 157.5): q = gradient_magnitude[i - 1, j - 1] r = gradient_magnitude[i + 1, j + 1] if (gradient_magnitude[i, j] >= q) and (gradient_magnitude[i, j] >= r): output[i, j] = gradient_magnitude[i, j] else: output[i, j] = 0 return output def double_threshold(self, image, low_ratio=0.05, high_ratio=0.15): """Apply double threshold""" high_threshold = image.max() * high_ratio low_threshold = high_threshold * low_ratio strong_edges = (image >= high_threshold) weak_edges = (image >= low_threshold) & (image < high_threshold) return strong_edges, weak_edges def hysteresis(self, strong_edges, weak_edges): """Apply hysteresis to connect edges""" height, width = strong_edges.shape output = np.copy(strong_edges) dx = [-1, -1, -1, 0, 0, 1, 1, 1] dy = [-1, 0, 1, -1, 1, -1, 0, 1] # Iterate until no more changes while True: previous = np.copy(output) for i in range(1, height - 1): for j in range(1, width - 1): if weak_edges[i, j]: # Check if any neighbor is a strong edge for k in range(8): if output[i + dx[k], j + dy[k]]: output[i, j] = True break if np.array_equal(previous, output): break return output def on_mousewheel(self, event): """Handle smooth mousewheel scrolling with improved animation""" # Get the delta value and normalize it delta = -1 * (event.delta // 120) # Use more steps with smaller increments for smoother animation steps = 15 # Increased number of steps for even smoother scrolling # Apply scrolling with acceleration and deceleration for i in range(steps): # Calculate a smooth deceleration curve factor = 1 - (i / steps) ** 2 # Quadratic deceleration for natural feel scroll_amount = max(1, int(delta * factor)) if delta > 0 else min(-1, int(delta * factor)) # Apply with increasing delay for natural deceleration self.window.after(i * 4, lambda a=scroll_amount: self.main_canvas.yview_scroll(a, 'units')) def smooth_scroll(self, *args): """Implement smooth scrolling""" if len(args) > 1: self.main_canvas.yview_moveto(args[1]) else: # Use smoother scrolling with acceleration amount = int(args[0]) # Apply scrolling with acceleration effect if amount != 0: for i in range(5): # Increased range for smoother scrolling factor = 0.8 ** i # Adjusted factor for better deceleration scroll_amount = int(amount * factor) if amount * factor >= 1 or amount * factor <= -1 else amount self.window.after(i * 4, lambda a=scroll_amount: self.main_canvas.yview_scroll(a, 'units')) def on_frame_configure(self, event=None): """Reset scroll region when content frame size changes""" self.main_canvas.configure(scrollregion=self.main_canvas.bbox("all")) # Ensure the canvas is large enough to accommodate all content self.main_frame.update_idletasks() def on_canvas_configure(self, event): """Update canvas window size when canvas is resized""" self.main_canvas.itemconfig(self.canvas_frame, width=event.width) # Ensure the canvas window is properly sized for future content additions self.main_canvas.configure(width=event.width) def run(self): self.window.mainloop() if __name__ == "__main__": app = CannyEdgeDetector() app.run()如何在训练阶段,使用农田边缘区域的掩码标注数据,限定模型仅在边缘区域进行目标检测,忽略农田内部区域。
最新发布
07-31
import requests import time import hashlib import base64 import cv2 import numpy as np import matplotlib.pyplot as plt import threading import json import os import random from datetime import datetime from collections import deque class FaceExpressionAnalyzer: def __init__(self, appid="", api_key="", url="http://tupapi.xfyun.cn/v1/expression", simulation_mode=False): """初始化表情分析器,设置API认证信息和URL""" self.appid = appid self.api_key = api_key self.url = url self.emotion_history = [] self.timestamps = [] self.running = False self.simulation_mode = simulation_mode # 模拟模式开关 self.emotion_smoother = deque(maxlen=5) # 用于平滑情绪输出 # 中英文情绪映射 self.emotion_map = { 'angry': '生气', 'disgust': '厌恶', 'fear': '害怕', 'happy': '开心', 'sad': '悲伤', 'surprise': '惊讶', 'neutral': '中性' } print(f"表情分析器初始化完成,{'模拟模式已启用' if simulation_mode else 'API模式已启用'}") def get_header(self, image_name, image_url=None, image_data=None): """生成API请求头,包含认证信息""" cur_time = str(int(time.time())) # 根据图片数据类型构建不同的请求参数 if image_data is not None: param = json.dumps({"image_name": image_name, "image_url": "", "image_data": image_data}) else: param = json.dumps({"image_name": image_name, "image_url": image_url}) param_base64 = base64.b64encode(param.encode('utf-8')).decode('utf-8') checksum = hashlib.md5((self.api_key + cur_time + param_base64).encode('utf-8')).hexdigest() header = { 'X-CurTime': cur_time, 'X-Param': param_base64, 'X-Appid': self.appid, 'X-CheckSum': checksum, 'Content-Type': 'application/x-www-form-urlencoded; charset=utf-8' } return header def simulate_api_response(self, face_count=1): """模拟API响应,支持多人脸识别""" time.sleep(0.5) # 模拟网络延迟 faces = [] for i in range(face_count): expression = random.choice(list(self.emotion_map.keys())) faces.append({ "expression": expression, "face_rectangle": { "top": random.randint(50, 300), "left": random.randint(50 + i*150, 200 + i*150), "width": random.randint(100, 200), "height": random.randint(100, 200) } }) return { "code": 0, "desc": "success", "sid": "simulated_sid", "data": { "faces": faces, "face_num": face_count } } def analyze_image_url(self, image_name, image_url): """分析网络图片中的人脸表情""" if self.simulation_mode: return self.simulate_api_response(random.randint(1, 3)) try: header = self.get_header(image_name, image_url) response = requests.post(self.url, headers=header, timeout=10) return self._parse_response(response) except Exception as e: return {"code": -4, "desc": f"请求异常: {str(e)}"} def analyze_local_image(self, image_name, image_path): """分析本地图片中的人脸表情""" if self.simulation_mode: return self.simulate_api_response(random.randint(1, 3)) try: # 读取图片并转为base64编码 if not os.path.exists(image_path): return {"code": -5, "desc": f"文件不存在: {image_path}"} img = cv2.imread(image_path) if img is None: return {"code": -6, "desc": "无法读取图片文件"} # 压缩图片直到大小合适 quality = 90 while True: _, buffer = cv2.imencode('.jpg', img, [cv2.IMWRITE_JPEG_QUALITY, quality]) image_data = buffer.tobytes() if len(image_data) <= 800 * 1024 or quality <= 10: break quality -= 10 image_base64 = base64.b64encode(image_data).decode('utf-8') header = self.get_header(image_name, image_url="", image_data=image_base64) response = requests.post(self.url, headers=header, timeout=10) return self._parse_response(response) except Exception as e: return {"code": -7, "desc": f"图片处理错误: {str(e)}"} def _parse_response(self, response): """安全解析API响应""" try: response.raise_for_status() data = response.json() # 统一API响应格式 if 'data' in data and 'expression' in data['data']: # 处理单脸响应格式 data['data'] = { "face_num": 1, "faces": [{ "expression": data['data']['expression'], "face_rectangle": data['data']['face_rectangle'] }] } return data except requests.exceptions.JSONDecodeError: return {"code": -1, "desc": "JSON解析错误", "raw_response": response.text[:200]} except requests.exceptions.HTTPError as e: return {"code": -2, "desc": f"HTTP错误: {str(e)}", "status_code": response.status_code} except Exception as e: return {"code": -3, "desc": f"请求异常: {str(e)}"} def translate_emotion(self, emotion_en): """将英文情绪翻译为中文""" return self.emotion_map.get(emotion_en, emotion_en) def capture_and_analyze(self): """从摄像头捕获图像并分析表情,支持多人脸""" cap = cv2.VideoCapture(0) if not cap.isOpened(): print("无法打开摄像头") return None, None, None # 尝试多次捕获 for _ in range(3): ret, frame = cap.read() if ret: break time.sleep(0.1) cap.release() if not ret: print("无法捕获图像") return None, None, None # 保存临时图像 timestamp = datetime.now().strftime("%Y%m%d_%H%M%S") image_name = f"face_{timestamp}.jpg" image_path = f"temp_{timestamp}.jpg" cv2.imwrite(image_path, frame) # 分析图像 result = self.analyze_local_image(image_name, image_path) # 删除临时文件 try: os.remove(image_path) except: pass return result, frame, image_name def start_tracking(self, interval=3): """开始定时拍照并分析表情,支持多人脸""" if self.running: print("追踪已在运行中") return self.running = True self.emotion_history = [] self.timestamps = [] print(f"开始表情追踪,每{interval}秒拍摄一次...") def track_loop(): while self.running: result, frame, image_name = self.capture_and_analyze() timestamp = datetime.now().strftime("%H:%M:%S") if result and 'code' in result and result['code'] == 0: if 'data' in result and 'face_num' in result['data']: face_num = result['data']['face_num'] print(f"检测到 {face_num} 张人脸") # 处理每张人脸 expressions = [] for face in result['data']['faces']: expression_en = face['expression'] expression_cn = self.translate_emotion(expression_en) expressions.append(expression_cn) # 绘制人脸框和表情 rect = face['face_rectangle'] if isinstance(rect, list): rect = rect[0] # 处理单脸格式 top = rect['top'] left = rect['left'] width = rect['width'] height = rect['height'] # 在图像上绘制结果 cv2.rectangle(frame, (left, top), (left+width, top+height), (0, 255, 0), 2) cv2.putText(frame, expression_cn, (left, top-10), cv2.FONT_HERSHEY_SIMPLEX, 0.8, (0, 255, 0), 2) # 平滑处理主要表情 if expressions: self.emotion_smoother.append(expressions[0]) smoothed_expression = max(set(self.emotion_smoother), key=list(self.emotion_smoother).count) self.emotion_history.append(smoothed_expression) self.timestamps.append(timestamp) print(f"时间: {timestamp}, 主要表情: {smoothed_expression}") if frame is not None: cv2.imshow('多人脸表情分析', frame) cv2.waitKey(1) else: print(f"分析成功但未检测到人脸: {result}") elif result: print(f"分析失败: {result.get('desc', '未知错误')}") else: print("未获取到分析结果") # 等待指定间隔,但允许提前退出 start_time = time.time() while self.running and (time.time() - start_time) < interval: time.sleep(0.1) self.tracking_thread = threading.Thread(target=track_loop) self.tracking_thread.daemon = True self.tracking_thread.start() def stop_tracking(self): """停止表情追踪""" if not self.running: print("追踪未运行") return self.running = False if hasattr(self, 'tracking_thread') and self.tracking_thread.is_alive(): self.tracking_thread.join(timeout=2.0) cv2.destroyAllWindows() print("已停止表情追踪") def plot_emotion_history(self): """绘制情感变化历史图表""" if not self.emotion_history: print("没有情感历史数据") return # 中文情绪列表 emotions_cn = list(self.emotion_map.values()) emotion_to_index = {emotion: idx for idx, emotion in enumerate(emotions_cn)} # 为每个情绪分配颜色 colors = ['red', 'green', 'purple', 'yellow', 'blue', 'orange', 'gray'] emotion_colors = {emotion: color for emotion, color in zip(emotions_cn, colors)} plt.figure(figsize=(12, 6)) # 饼图:表情分布 plt.subplot(1, 2, 1) emotion_counts = {emotion: 0 for emotion in emotions_cn} for emotion in self.emotion_history: if emotion in emotion_counts: emotion_counts[emotion] += 1 # 过滤掉计数为0的表情 filtered_emotions = [e for e in emotions_cn if emotion_counts[e] > 0] filtered_counts = [emotion_counts[e] for e in filtered_emotions] filtered_colors = [emotion_colors[e] for e in filtered_emotions] plt.pie(filtered_counts, labels=filtered_emotions, colors=filtered_colors, autopct='%1.1f%%') plt.title('表情分布') # 折线图:情感变化时间线 plt.subplot(1, 2, 2) emotion_indices = [emotion_to_index[e] for e in self.emotion_history] plt.plot(self.timestamps, emotion_indices, 'o-') # 设置Y轴为表情标签 plt.yticks(range(len(emotions_cn)), emotions_cn) plt.title('情感变化时间线') plt.xlabel('时间') plt.ylabel('表情') plt.xticks(rotation=45) plt.tight_layout() plt.show() # 主函数 def main(): print("="*40) print("多人脸表情分析系统") print("="*40) # 配置选项 print("\n请选择运行模式:") print("1. 模拟模式 (无需API密钥)") print("2. API模式 (需要有效API密钥)") mode_choice = input("请输入选择 (1/2): ").strip() simulation_mode = mode_choice == "1" if simulation_mode: analyzer = FaceExpressionAnalyzer(simulation_mode=True) else: APPID = input("请输入APPID: ").strip() API_KEY = input("请输入API_KEY: ").strip() if not APPID or not API_KEY: print("警告: APPID或API_KEY为空,将切换到模拟模式") analyzer = FaceExpressionAnalyzer(simulation_mode=True) else: analyzer = FaceExpressionAnalyzer(APPID, API_KEY) while True: print("\n===== 主菜单 =====") print("1. 分析网络图片") print("2. 分析本地图片") print("3. 拍照分析") print("4. 开始定时分析") print("5. 停止定时分析") print("6. 查看情感历史图表") print("0. 退出") choice = input("请选择功能: ").strip() if choice == "1": image_name = input("输入图片名称 (默认: test.jpg): ") or "test.jpg" image_url = input("输入图片URL: ") if image_url: result = analyzer.analyze_image_url(image_name, image_url) print("\n分析结果:") self._display_results(result) else: print("错误: URL不能为空") elif choice == "2": image_name = input("输入图片名称 (默认: test.jpg): ") or "test.jpg" image_path = input("输入图片路径: ") if image_path: result = analyzer.analyze_local_image(image_name, image_path) print("\n分析结果:") self._display_results(result) else: print("错误: 图片路径不能为空") elif choice == "3": print("请面对摄像头准备拍照...") result, frame, image_name = analyzer.capture_and_analyze() if result: print("\n分析结果:") self._display_results(result) if frame is not None: # 在图像上绘制结果 if 'code' in result and result['code'] == 0 and 'data' in result: for face in result['data']['faces']: expr_en = face['expression'] expr_cn = analyzer.translate_emotion(expr_en) # 绘制人脸框 rect = face['face_rectangle'] if isinstance(rect, list): rect = rect[0] # 处理单脸格式 top = rect['top'] left = rect['left'] width = rect['width'] height = rect['height'] cv2.rectangle(frame, (left, top), (left+width, top+height), (0, 255, 0), 2) cv2.putText(frame, f"表情: {expr_cn}", (left, top-10), cv2.FONT_HERSHEY_SIMPLEX, 0.8, (0, 255, 0), 2) cv2.imshow('多人脸表情分析', frame) cv2.waitKey(0) cv2.destroyAllWindows() elif choice == "4": interval = input("输入采集间隔(秒,默认3秒): ").strip() try: interval = int(interval) if interval else 3 analyzer.start_tracking(interval) except ValueError: print("无效的间隔时间,使用默认值3秒") analyzer.start_tracking() elif choice == "5": analyzer.stop_tracking() elif choice == "6": analyzer.plot_emotion_history() elif choice == "0": if analyzer.running: analyzer.stop_tracking() print("程序已退出") break else: print("无效选择,请输入0-6之间的数字") def _display_results(self, result): """显示分析结果(中文)""" if not result: print("无有效结果") return if 'code' in result and result['code'] == 0: if 'data' in result and 'face_num' in result['data']: face_num = result['data']['face_num'] print(f"检测到 {face_num} 张人脸:") for i, face in enumerate(result['data']['faces'], 1): expr_en = face['expression'] expr_cn = self.translate_emotion(expr_en) print(f"人脸 {i}: {expr_cn}") else: print("分析成功但未检测到人脸") else: error_desc = result.get('desc', '未知错误') print(f"分析失败: {error_desc}") if __name__ == "__main__": main() 讲解一下这个代码
06-20
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值