This article mainly introduces advanced morphological processing of python digital image processing. Now I will share it with you and give you a reference. Let’s take a look together
Morphological processing, in addition to the most basic expansion, erosion, opening/closing operations, black/white hat processing, there are also some more advanced applications, such as convex hulls and connected region markers , delete small areas, etc.
1. Convex Hull
The convex hull refers to a convex polygon that contains all the white pixels in the image.
The function is:
skimage.morphology.convex_hull_image(image)
Copy after login
The input is a binary image and the output is a logical binary image. The points within the convex hull are True, otherwise they are False
##convex_hull_image( ) considers all targets in the picture as a whole, so only one minimum convex polygon is calculated. If there are multiple target objects in the picture and each object needs to calculate a minimum convex polygon, you need to use the convex_hull_object() function. Function format:
In a binary image, if two pixels are adjacent and have the same value (both 0 or 1), then the two pixels are considered to be in a connected area. All pixels in the same connected area are marked with the same value. This process is called connected area marking. When judging whether two pixels are adjacent, we usually use 4-connected or 8-connected judgment. In an image, the smallest unit is a pixel, and each pixel is surrounded by 8 adjacent pixels. There are two common adjacency relationships: 4-adjacency and 8-adjacency. 4 is adjacent to a total of 4 points, namely up, down, left and right, as shown in the left picture below. 8 There are 8 adjacent points in total, including points at diagonal positions, as shown in the right figure below.
In the skimage package, we use the label() function under the measure submodule to implement connected area labeling.
Function format:
skimage.measure.label(image,connectivity=None)
Copy after login
The image in the parameter represents the binary image that needs to be processed, connectivity represents the connection mode, 1 represents 4 adjacencies , 2 represents 8 adjacencies.
Output an array of labels (labels), starting from 0.
import numpy as np
import scipy.ndimage as ndi
from skimage import measure,color
import matplotlib.pyplot as plt
#编写一个函数来生成原始二值图像
def microstructure(l=256):
n = 5
x, y = np.ogrid[0:l, 0:l] #生成网络
mask = np.zeros((l, l))
generator = np.random.RandomState(1) #随机数种子
points = l * generator.rand(2, n**2)
mask[(points[0]).astype(np.int), (points[1]).astype(np.int)] = 1
mask = ndi.gaussian_filter(mask, sigma=l/(4.*n)) #高斯滤波
return mask > mask.mean()
data = microstructure(l=128)*1 #生成测试图片
labels=measure.label(data,connectivity=2) #8连通区域标记
dst=color.label2rgb(labels) #根据不同的标记显示不同的颜色
print('regions number:',labels.max()+1) #显示连通区域块数(从0开始标记)
fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(8, 4))
ax1.imshow(data, plt.cm.gray, interpolation='nearest')
ax1.axis('off')
ax2.imshow(dst,interpolation='nearest')
ax2.axis('off')
fig.tight_layout()
plt.show()
Copy after login
In the code, by multiplying by 1 in some places, the bool array can be quickly converted to an int array.
The result is as shown in the figure: there are 10 connected areas, marked 0-9
If you want to operate on each connected area separately, such as calculating Area, circumscribed rectangle, convex hull area, etc., you need to call the regionprops() function of the measure submodule. The format of this function is:
skimage.measure.regionprops(label_image)
Copy after login
Returns the attribute list of all connected blocks. The commonly used attribute list is as follows:
The above is the detailed content of Advanced morphological processing of python digital image processing. For more information, please follow other related articles on the PHP Chinese website!
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
VS Code can be used to write Python and provides many features that make it an ideal tool for developing Python applications. It allows users to: install Python extensions to get functions such as code completion, syntax highlighting, and debugging. Use the debugger to track code step by step, find and fix errors. Integrate Git for version control. Use code formatting tools to maintain code consistency. Use the Linting tool to spot potential problems ahead of time.
In VS Code, you can run the program in the terminal through the following steps: Prepare the code and open the integrated terminal to ensure that the code directory is consistent with the terminal working directory. Select the run command according to the programming language (such as Python's python your_file_name.py) to check whether it runs successfully and resolve errors. Use the debugger to improve debugging efficiency.
VS Code can run on Windows 8, but the experience may not be great. First make sure the system has been updated to the latest patch, then download the VS Code installation package that matches the system architecture and install it as prompted. After installation, be aware that some extensions may be incompatible with Windows 8 and need to look for alternative extensions or use newer Windows systems in a virtual machine. Install the necessary extensions to check whether they work properly. Although VS Code is feasible on Windows 8, it is recommended to upgrade to a newer Windows system for a better development experience and security.
VS Code extensions pose malicious risks, such as hiding malicious code, exploiting vulnerabilities, and masturbating as legitimate extensions. Methods to identify malicious extensions include: checking publishers, reading comments, checking code, and installing with caution. Security measures also include: security awareness, good habits, regular updates and antivirus software.
VS Code is the full name Visual Studio Code, which is a free and open source cross-platform code editor and development environment developed by Microsoft. It supports a wide range of programming languages and provides syntax highlighting, code automatic completion, code snippets and smart prompts to improve development efficiency. Through a rich extension ecosystem, users can add extensions to specific needs and languages, such as debuggers, code formatting tools, and Git integrations. VS Code also includes an intuitive debugger that helps quickly find and resolve bugs in your code.
Python excels in automation, scripting, and task management. 1) Automation: File backup is realized through standard libraries such as os and shutil. 2) Script writing: Use the psutil library to monitor system resources. 3) Task management: Use the schedule library to schedule tasks. Python's ease of use and rich library support makes it the preferred tool in these areas.
VS Code not only can run Python, but also provides powerful functions, including: automatically identifying Python files after installing Python extensions, providing functions such as code completion, syntax highlighting, and debugging. Relying on the installed Python environment, extensions act as bridge connection editing and Python environment. The debugging functions include setting breakpoints, step-by-step debugging, viewing variable values, and improving debugging efficiency. The integrated terminal supports running complex commands such as unit testing and package management. Supports extended configuration and enhances features such as code formatting, analysis and version control.
Yes, VS Code can run Python code. To run Python efficiently in VS Code, complete the following steps: Install the Python interpreter and configure environment variables. Install the Python extension in VS Code. Run Python code in VS Code's terminal via the command line. Use VS Code's debugging capabilities and code formatting to improve development efficiency. Adopt good programming habits and use performance analysis tools to optimize code performance.