去掉 一个数据中心大于0的值
时间: 2024-03-12 09:47:10 浏览: 26
如果您想从一个向量或矩阵中删除大于0的元素,可以使用逻辑索引来实现。具体步骤如下:
1. 定义一个向量或矩阵,例如 `A = [-1 2 0; 3 -4 5; 6 0 8]`;
2. 使用逻辑运算符 `>` 得到一个逻辑数组,例如 `idx = A > 0`;
3. 将逻辑数组转换为一维逻辑向量,例如 `idxvec = any(idx,2)`,其中 `any` 函数返回每行中是否存在大于0的元素;
4. 使用逻辑索引删除包含大于0的元素的行或列,例如 `A = A(~idxvec,:)` 将删除包含大于0的元素的行,`A = A(:,~any(idx,1))` 将删除包含大于0的元素的列。
代码示例:
```matlab
A = [-1 2 0; 3 -4 5; 6 0 8];
idx = A > 0;
idxvec = any(idx,2);
A = A(~idxvec,:)
```
执行完上述代码后,A 的结果为:
```
ans =
-1 2 0
```
即删除了包含大于0的元素的行。
相关问题
用matlab实现:在红外图像中通过选取7*7大小的窗口以1的步长遍历整张图片获得图像块,将图像块内灰度值进行从小到大排序,得到一维数组并去掉最大值,其余数值进行相邻间差值,选取最大差值点中最大像素的坐标,若差值小于5则取消此数值在原始图像上将相关坐标像素标为0,差值大于5将其作为候选点保存坐标数据,并将其在原始图像坐标上标为1,输出以坐标像素以1为中心的17*17框图大小的结果图像
以下是用MATLAB实现红外图像处理的代码,实现从原始图像中找到可能的小目标区域:
```matlab
% 读取红外图像
img = imread('infrared_image.jpg');
% 定义窗口大小和步长
win_size = 7;
step_size = 1;
% 遍历整张图像,获取图像块并处理
[row, col] = size(img);
candidate_points = [];
for i = 1:step_size:row-win_size+1
for j = 1:step_size:col-win_size+1
% 获取当前窗口内的图像块
img_block = img(i:i+win_size-1, j:j+win_size-1);
% 将图像块内的灰度值进行排序,并去掉最大值
sorted_block = sort(img_block(:));
sorted_block = sorted_block(1:end-1);
% 计算相邻间的差值
diff_block = diff(sorted_block);
% 找到差值最大的点
[max_diff, max_idx] = max(diff_block);
max_val = sorted_block(max_idx+1);
% 判断差值是否小于5,如果是则取消此数值
if max_diff < 5
img_block = zeros(win_size, win_size);
else
% 将差值大于5的点保存为候选点
candidate_points = [candidate_points; max_val, i+max_idx, j+find(img_block==max_val)-1];
% 在原始图像上标记候选点
img(i+max_idx, j+find(img_block==max_val)-1) = 1;
end
end
end
% 对候选点进行进一步处理,确定真正的小目标区域
for i = 1:size(candidate_points, 1)
% 获取以候选点为中心的17*17图像块
center_x = candidate_points(i, 2);
center_y = candidate_points(i, 3);
img_block = img(center_x-8:center_x+8, center_y-8:center_y+8);
% 统计像素值为1的数量
num_pixels = sum(img_block(:));
% 如果像素值为1的数量小于10,则取消此候选点
if num_pixels < 10
img(center_x, center_y) = 0;
end
end
% 显示处理后的图像
imshow(img);
```
这段代码实现了对红外图像的处理,首先遍历整张图像,以7*7的窗口和步长为1的方式获取图像块,然后对每个图像块进行处理。处理过程包括将图像块内的灰度值进行排序,并去掉最大值,计算相邻间的差值,找到差值最大的点。如果差值小于5,则取消此数值;如果差值大于5,则将其作为候选点保存坐标数据,并在原始图像坐标上标为1。然后,对候选点进行进一步处理,确定真正的小目标区域。最终,将处理后的图像显示出来。
Opencv c++代码实现分水岭分割算法,并将其用于下面图像分割,其算法:(1)先使用 Otsu's 二值化对图像进行二值化 (2)使用开运算去除图像中的细小白色噪点 (3)通过距离变换来确定前景图像 (4)可以看到硬币的中心像素值最大(中心离背景像素最远)。对其进行二值处理就得到了分离的前景图。 (5)通过膨胀运算,使得一部分背景成为了物体到的边界,得到的图像中的黑色区域肯定是真实背景。 (6)使用膨胀图减去前景图,得到不确定区域,这部分区域不确定是硬币还是背景,这些区域通常在前景和背景接触的区域(或者两个不同硬币接触的区域),称之为边界。通过分水岭算法应该能找到确定的边界。 (7)现在可以确定哪些是硬币区域,哪些是背景区域。然后需要创建标记(marker,它是一个与原始图像大小相同的矩阵,int32数据类型),表示其中的每个区域。分水岭算法将标记的0的区域视为不确定区域,将标记为1的区域视为背景区域,将标记大于1的正整数表示我们想得到的前景。 (8)可以使用 connectedComponents() 来实现这个功能,它是用0标记图像的背景,用大于0的整数标记其他对象。但是OpenCV的分水岭分割函数,会用0表示不确定区域,所以需要对标记统一加一,然后将上一步计算的不确定区域部分标记为0. (9)现在可以调用watershed函数分割图像,如下: watershed(src, markers); (10)maker中标记为-1的地方就是分界线,makers中的最大值减1就是统计的个数。
以下是 Opencv c++ 代码实现分水岭分割算法,实现对图像的分割:
```cpp
#include <opencv2/opencv.hpp>
#include <iostream>
using namespace cv;
using namespace std;
int main()
{
Mat img = imread("coins.jpg", IMREAD_GRAYSCALE);
if (img.empty())
{
cout << "Could not read the image!" << endl;
return -1;
}
// Otsu's 二值化
Mat binary;
threshold(img, binary, 0, 255, THRESH_BINARY | THRESH_OTSU);
// 开运算去除细小白色噪点
Mat kernel = getStructuringElement(MORPH_ELLIPSE, Size(5, 5));
morphologyEx(binary, binary, MORPH_OPEN, kernel);
// 距离变换
Mat dist;
distanceTransform(binary, dist, DIST_L2, 3);
// 取硬币中心像素值最大的区域作为前景图像
normalize(dist, dist, 0, 1, NORM_MINMAX);
threshold(dist, dist, 0.5, 1.0, THRESH_BINARY);
// 膨胀运算
Mat kernel2 = getStructuringElement(MORPH_ELLIPSE, Size(9, 9));
dilate(dist, dist, kernel2);
// 计算不确定区域
Mat sure_bg;
threshold(dist, sure_bg, 0.5, 1.0, THRESH_BINARY_INV);
Mat sure_fg;
threshold(dist, sure_fg, 0.5, 1.0, THRESH_BINARY);
// 不确定区域
Mat uncertain;
Mat uncertain_temp;
subtract(sure_bg, binary, uncertain_temp);
subtract(uncertain_temp, sure_fg, uncertain);
// 创建标记
Mat marker(binary.size(), CV_32S);
marker.setTo(0);
marker.setTo(1, sure_bg);
marker.setTo(2, uncertain);
marker.setTo(3, sure_fg);
// 分水岭算法
watershed(img, marker);
// 统计硬币个数
int coin_count = 0;
for (int i = 0; i < marker.rows; i++)
{
for (int j = 0; j < marker.cols; j++)
{
if (marker.at<int>(i, j) == -1)
{
coin_count++;
}
}
}
cout << "The number of coins is: " << coin_count << endl;
// 显示结果
Mat result;
convertScaleAbs(marker, result);
imshow("Result", result);
waitKey(0);
return 0;
}
```
这个程序实现了对硬币图像的分割,并统计了硬币的个数。
相关推荐
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.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)