Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Programming Articles
Page 411 of 2547
Performing an opening operation on an image using OpenCV
In this program, we will perform the opening operation on an image using OpenCV. Opening removes small objects from the foreground of an image, placing them in the background. This technique can also be used to find specific shapes in an image. Opening is mathematically defined as erosion followed by dilation. The function we use for this task is cv2.morphologyEx(image, cv2.MORPH_OPEN, kernel). Original Image Algorithm Step 1: Import cv2 and numpy Step 2: Read the image Step 3: Define the kernel (structuring element) Step 4: Pass the image and kernel to cv2.morphologyEx() function ...
Read MorePlotting dates on the X-axis with Python\'s Matplotlib
Using Pandas, we can create a dataframe and set datetime values as the index. Matplotlib's gcf().autofmt_xdate() automatically formats date labels on the X-axis for better readability. Steps to Plot Dates on X-axis Create a list of date strings and convert them to datetime using pd.to_datetime() Prepare your data values (e.g., [1, 2, 3]) Create a DataFrame and assign the data to a column Set the DataFrame index using the datetime values Plot the DataFrame using plt.plot() Format the X-axis dates using plt.gcf().autofmt_xdate() Display the plot with plt.show() Example import pandas as pd import ...
Read MoreDilating images using the OpenCV function dilate()
In this tutorial, we will learn how to dilate an image using the dilate() function in OpenCV. Dilation is a morphological operation that adds pixels to the boundaries of objects in an image, effectively expanding or thickening white regions and shrinking black regions. What is Image Dilation? Dilation expands the foreground objects in a binary or grayscale image. It uses a kernel (structuring element) that slides over the image, and for each position, it replaces the center pixel with the maximum value in the kernel's neighborhood. Syntax cv2.dilate(src, kernel, iterations=1, borderType=cv2.BORDER_CONSTANT, borderValue=0) Parameters ...
Read MoreEroding an image using the OpenCV function erode()
In this program, we will erode an image using the OpenCV function erode(). Erosion of an image means to shrink the image by reducing the size of white regions or foreground objects. If any of the pixels in a kernel is 0, then all the pixels in the kernel are set to 0. One condition before applying an erosion function on an image is that the image should be a grayscale image. What is Image Erosion? Image erosion is a morphological operation that reduces the boundaries of foreground objects (white pixels). It works by sliding a structuring element ...
Read MoreShow only certain items in legend Python Matplotlib
In Python Matplotlib, you can control which items appear in the legend by using the plt.legend() method with a list of labels. This allows you to show only specific plot elements in the legend rather than all plotted data. Basic Syntax The plt.legend() method accepts a list of labels to display ? plt.legend(["label1", "label2"], loc=location, frameon=True/False) Parameters labels − List of strings to show in the legend loc − Location of the legend (0 for best location) frameon − Boolean flag to show/hide legend border Example Here's how to ...
Read MoreBlurring an image using the OpenCV function blur()
In this tutorial, we will learn how to blur an image using the OpenCV blur() function. Blurring reduces image noise and detail by averaging pixel values in a neighborhood defined by a kernel. Algorithm Step 1: Import OpenCV and NumPy libraries Step 2: Load the input image Step 3: Define the kernel size for blurring Step 4: Apply the blur() function with image and kernel parameters Step 5: Display or save the blurred result Understanding the blur() Function The cv2.blur() function performs simple box filtering. It takes the average of all pixels in the ...
Read MoreDraw a filled polygon using the OpenCV function fillPoly()
In this tutorial, we will learn how to draw a filled polygon using OpenCV's fillPoly() function. This function fills a polygon defined by a set of vertices with a specified color. Syntax cv2.fillPoly(image, pts, color) Parameters The fillPoly() function accepts the following parameters ? image ? The input image on which to draw the polygon pts ? Array of polygon vertices (points) color ? Fill color of the polygon in BGR format Algorithm Step 1: Import cv2 and numpy Step 2: Define the polygon vertices (endpoints) Step 3: ...
Read MoreMaking matplotlib scatter plots from dataframes in Python's pandas
Creating scatter plots from pandas DataFrames using matplotlib is a powerful way to visualize relationships between variables. We can use the DataFrame structure to organize our data and create colorful scatter plots with proper labeling. Steps to Create a Scatter Plot Import matplotlib and pandas libraries Create lists for your data variables (x-axis, y-axis, and colors) Build a pandas DataFrame from your data Create figure and axes objects using plt.subplots() Add axis labels using plt.xlabel() and plt.ylabel() Generate the scatter plot using ax.scatter() method Display the plot with plt.show() Example Here's how to create ...
Read MoreManually add legend Items Python Matplotlib
In Matplotlib, you can manually add legend items using the plt.legend() method. This allows you to create custom legends with specific labels, positions, and styling options like borders. Basic Syntax plt.legend(labels, loc=location, frameon=True/False) Parameters labels − List of strings for legend labels loc − Location of the legend (0 for best location) frameon − Boolean to show/hide the legend border Example Here's how to create a plot with manually added legend items ? import matplotlib.pyplot as plt # Set axis labels plt.xlabel("X-axis") plt.ylabel("Y-axis") # Plot two ...
Read MoreMake 3D plot interactive in Jupyter Notebook (Python & Matplotlib)
Interactive 3D plots in Jupyter Notebook allow you to rotate, zoom, and pan your visualizations. Matplotlib provides built-in interactivity when using the %matplotlib notebook or %matplotlib widget magic commands. Setting Up Interactive Mode To enable interactivity, use the appropriate magic command at the beginning of your notebook ? %matplotlib notebook # or use %matplotlib widget for newer versions import matplotlib.pyplot as plt import numpy as np Creating an Interactive 3D Sphere Here's how to create an interactive 3D wireframe sphere ? import matplotlib.pyplot as plt import numpy as np # ...
Read More