set_noise =10; BO = bwareaopen(BW,set_noise); 用python怎样获得相同结果
时间: 2024-03-23 13:43:10 浏览: 20
这个 MATLAB 代码的作用是对二值化图像 `BW` 进行开运算,移除面积小于 `set_noise` 的连通区域。
在 Python 中,你可以使用 `scikit-image` 库来实现类似的功能。具体代码如下:
```python
from skimage import morphology
set_noise = 10
BO = morphology.remove_small_objects(BW, set_noise)
```
其中 `morphology.remove_small_objects` 函数可以移除面积小于指定阈值的连通区域,其第一个参数为输入的二值化图像,第二个参数为阈值。
相关问题
v = VideoReader('shoujilux7.mp4'); while hasFrame(v) frame = readFrame(v); gray_frame = rgb2gray(frame); % gamma校正 gamma = 1.5; gamma_corrected = imadjust(gray_frame,[],[],gamma); % 高斯滤波 sigma = 1; hsize = ceil(6*sigma); h = fspecial('gaussian', hsize, sigma); filtered_frame = imfilter(gamma_corrected, h); % Otsu阈值分割 T = graythresh(filtered_frame); [m, n] = size(filtered_frame); E = bwareaopen(im2bw(filtered_frame, T), round(m*n/1000), 8); % Canny边缘检测 canny_edge = edge(E, 'canny'); % 形态学膨胀 se = strel('disk', 2); dilated_edge = imdilate(canny_edge, se); % 连通域分析 stats = regionprops('table', dilated_edge, 'Area', 'Centroid'); % 筛选面积最大的连通区域 [~, idx] = max(stats.Area); centroid = stats.Centroid(idx, :); % 显示帧和质心 imshow(dilated_edge); hold on; plot(centroid(1), centroid(2), 'r+', 'MarkerSize', 10); hold off; %获取图像中心点的像素坐标 x_res=1920; y_res=1080; v_fov=46; f=50; x_c = x_res / 2; y_c = y_res / 2; %将图像中心点的像素坐标转换为相对坐标 x_c_rel = - (x_c - 1); y_c_rel = y_c - 1; %将相对坐标转换为实际坐标 x = (x_c_rel / x_res) * 2 * tan(10 / 2) * f; y = (y_c_rel / y_res) * 2 * tan(9 / 2) * f; end代码修改显示坐标
v = VideoReader('shoujilux7.mp4');
while hasFrame(v)
frame = readFrame(v);
gray_frame = rgb2gray(frame);
% gamma校正
gamma = 1.5;
gamma_corrected = imadjust(gray_frame,[],[],gamma);
% 高斯滤波
sigma = 1;
hsize = ceil(6*sigma);
h = fspecial('gaussian', hsize, sigma);
filtered_frame = imfilter(gamma_corrected, h);
% Otsu阈值分割
T = graythresh(filtered_frame);
[m, n] = size(filtered_frame);
E = bwareaopen(im2bw(filtered_frame, T), round(m*n/1000), 8);
% Canny边缘检测
canny_edge = edge(E, 'canny');
% 形态学膨胀
se = strel('disk', 2);
dilated_edge = imdilate(canny_edge, se);
% 连通域分析
stats = regionprops('table', dilated_edge, 'Area', 'Centroid');
% 筛选面积最大的连通区域
[~, idx] = max(stats.Area);
centroid = stats.Centroid(idx, :);
% 显示帧和质心
imshow(dilated_edge);
hold on;
plot(centroid(1), centroid(2), 'r+', 'MarkerSize', 10);
hold off;
%获取图像中心点的像素坐标
x_res=1920;
y_res=1080;
v_fov=46;
f=50;
x_c = x_res / 2;
y_c = y_res / 2;
%将图像中心点的像素坐标转换为相对坐标
x_c_rel = - (x_c - 1);
y_c_rel = y_c - 1;
%将相对坐标转换为实际坐标
x = (x_c_rel / x_res) * 2 * tan(v_fov / 2) * f;
y = (y_c_rel / y_res) * 2 * tan(v_fov * y_res / x_res / 2) * f;
% 显示实际坐标
disp(['x: ', num2str(x), ', y: ', num2str(y)]);
end
v = VideoReader('shoujilux7.mp4'); while hasFrame(v) frame = readFrame(v); gray_frame = rgb2gray(frame); % gamma校正 gamma = 1.5; gamma_corrected = imadjust(gray_frame,[],[],gamma); % 高斯滤波 sigma = 1; hsize = ceil(6sigma); h = fspecial('gaussian', hsize, sigma); filtered_frame = imfilter(gamma_corrected, h); % Otsu阈值分割 T = graythresh(filtered_frame); [m, n] = size(filtered_frame); E = bwareaopen(im2bw(filtered_frame, T), round(mn/1000), 8); % Canny边缘检测 canny_edge = edge(E, 'canny'); % 形态学膨胀 se = strel('disk', 2); dilated_edge = imdilate(canny_edge, se); % 连通域分析 stats = regionprops('table', dilated_edge, 'Area', 'Centroid'); % 筛选面积最大的连通区域 [~, idx] = max(stats.Area); centroid = stats.Centroid(idx, :); % 显示帧和质心 imshow(dilated_edge); hold on; plot(centroid(1), centroid(2), 'r+', 'MarkerSize', 10); hold off; %获取图像中心点的像素坐标 x_res=1920; y_res=1080; v_fov=46; f=50; x_c = x_res / 2; y_c = y_res / 2; %将图像中心点的像素坐标转换为相对坐标 x_c_rel = - (x_c - 1); y_c_rel = y_c - 1; %将相对坐标转换为实际坐标 x = (x_c_rel / x_res) * 2 * tan(10 / 2) * f; y = (y_c_rel / y_res) * 2 * tan(9 / 2) * f; end代码修改显示质心坐标
v = VideoReader('shoujilux7.mp4');
while hasFrame(v)
frame = readFrame(v);
gray_frame = rgb2gray(frame);
% gamma校正
gamma = 1.5;
gamma_corrected = imadjust(gray_frame,[],[],gamma);
% 高斯滤波
sigma = 1;
hsize = ceil(6*sigma);
h = fspecial('gaussian', hsize, sigma);
filtered_frame = imfilter(gamma_corrected, h);
% Otsu阈值分割
T = graythresh(filtered_frame);
[m, n] = size(filtered_frame);
E = bwareaopen(im2bw(filtered_frame, T), round(m*n/1000), 8); %修改面积计算公式
% Canny边缘检测
canny_edge = edge(E, 'canny');
% 形态学膨胀
se = strel('disk', 2);
dilated_edge = imdilate(canny_edge, se);
% 连通域分析
stats = regionprops('table', dilated_edge, 'Area', 'Centroid');
% 筛选面积最大的连通区域
[~, idx] = max(stats.Area);
centroid = stats.Centroid(idx, :);
% 显示帧和质心
imshow(dilated_edge);
hold on;
plot(centroid(1), centroid(2), 'r+', 'MarkerSize', 10);
hold off;
% 获取图像中心点的像素坐标
x_res = 1920;
y_res = 1080;
v_fov = 46;
f = 50;
x_c = x_res / 2;
y_c = y_res / 2;
% 将图像中心点的像素坐标转换为相对坐标
x_c_rel = - (x_c - 1);
y_c_rel = y_c - 1;
% 将相对坐标转换为实际坐标并显示
x = (x_c_rel / x_res) * 2 * tan(10 / 2) * f;
y = (y_c_rel / y_res) * 2 * tan(9 / 2) * f;
fprintf('当前帧的质心坐标为:(%f, %f)\n', centroid(1), centroid(2));
fprintf('当前帧的实际坐标为:(%f, %f)\n', x, y);
end
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)