Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/algorithm/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Image 如何根据矩阵值检测图像的一部分?_Image_Algorithm_Matlab_Image Processing_Matlab Figure - Fatal编程技术网

Image 如何根据矩阵值检测图像的一部分?

Image 如何根据矩阵值检测图像的一部分?,image,algorithm,matlab,image-processing,matlab-figure,Image,Algorithm,Matlab,Image Processing,Matlab Figure,我在Matlab(版本R 2016b)中有一个简单的pcolor绘图,我上传了它,如下图所示。我只需要得到蓝色斜线,它从最左角的中间延伸到最右角,而无需对矩阵值进行硬编码 例如:从pcolor绘图中可以看到所需的坡度线的值大约在20到45之间。(通过查看图表粗略猜测) 我在包含打印值的上应用以下代码 load('Slant.mat'); Slant(Slant<20|Slant>50)=0; pcolor(Slant); colormap(jet); shading interp;

我在Matlab(版本R 2016b)中有一个简单的
pcolor
绘图,我上传了它,如下图所示。我只需要得到蓝色斜线,它从最左角的中间延伸到最右角,而无需对矩阵值进行硬编码

例如:从pcolor绘图中可以看到所需的坡度线的值大约在2045之间。(通过查看图表粗略猜测)

我在包含打印值的上应用以下代码

load('Slant.mat');
Slant(Slant<20|Slant>50)=0;
pcolor(Slant); colormap(jet); shading interp; colorbar;

想法:使用Hough变换:

首先,最好创建一个只包含我们感兴趣的行和列的新矩阵

为了应用matlab内置的
hough
我们必须创建一个二值图像:由于线条的值总是比其他线条的值低,我们可以确定图片中亮度的最低四分位(使用,并将这些设置为白色,其他所有设置为黑色)


然后,为了找到线条,我们可以直接在BW图像上使用。

这里是另一个建议:

  • 删除所有背景
  • 假设这条“线”导致数据的双峰分布(去掉零后),找到下模式
  • 假设线的值始终低于背景值,应用逻辑掩码,将最小+2nd_模式以上的所有值设置为零,如下图所示(红色圆圈):
  • 以下是它的工作原理:

    A = Slant(any(Slant,2),:); % save in A only the nonzero data
    
    现在我们有一个类似这样的
    A

    因此,我们得到一个直线函数
    f_line
    ,如下所示(红色):

    现在我们想让这一行变粗,就像数据中的那一行一样,因此我们采用厚度模式(通过计算每列中的值,您可能希望取
    max
    ),并将这一行向两边“扩展”一半:

    thick = mode(sum(mA)); % mode thickness of the line
    tmp = (1:thick)-ceil(thick/2); % helper vector for expanding
    rows = bsxfun(@plus,tmp.',floor(f_line(1:size(A,2)))); % all the rows for each coloumn
    rows(rows<1) = 1; % make sure to not get out of range
    rows(rows>size(A,1)) = size(A,1); % make sure to not get out of range
    inds = sub2ind(size(A),rows,repmat(1:size(A,2),thick,1)); % convert to linear indecies
    mA(inds) = 1; % add the interpolation to the mask
    result = A.*mA; % apply the mask on A
    
    thick=mode(总和(mA));%线的mode厚度
    tmp=(1:thick)-ceil(thick/2);%用于扩展的辅助向量
    rows=bsxfun(@plus,tmp.),floor(f_行(1:size(A,2)));%每个列的所有行
    行(rowsize(A,1))=大小(A,1);%请确保不要超出范围
    inds=sub2ind(大小(A),行,repmat(1:大小(A,2),粗,1));%转换为线性索引
    mA(inds)=1;%将插值添加到遮罩
    结果=A.*mA;%将遮罩应用于
    
    现在,
    结果
    如下所示:


    矩阵在图像中的任何位置都是非零的吗?矩阵在彩色区域中是非零的。除此之外,它的值为零。为什么要使用这么大的矩阵,它有这么多的条目而不添加任何内容?这条线有什么特性使它与图片的其他部分不同?不知道它的结构和属性您期望的信息似乎无法解决。该矩阵来自某些测量值。由于标准噪波的知识,其他条目为零。(我之前知道这些行和列是噪波值)。线条是移动车辆/物体的测量数据。正如我在问题中提到的,通过目视检查,人们可以很容易地猜测线条的值在20到45之间。但我不想硬编码,我只想删除其他背景值,如黄色值(不一定要精确,但要足够清晰,使线条可见)“通过目视检查”没有任何帮助,因为你想使用计算机。这条线总是一条直线吗?它总是有相同的厚度吗?它总是有相同的角度吗?它比图像的其他部分亮/暗吗?你如何区分它和其他噪音?@EBH和@fairr。这两个答案都非常有用。我用了一个梳子这两个答案的国家都实现了一条曲线的结果。并且很好地解释了这个概念。分位数方法被证明对曲线非常有用。
    [y,x] = findpeaks(histcounts(A)); % find all the mode in the histogram of A
    sorted_x = sortrows([x.' y.'],-2); % sort them by their hight in decendet order
    mA = A<min(A(:))+sorted_x(2,1); % mask all values above the second mode
    result = A.*mA; % apply the mask on A
    
    [y1,x1] = find(mA,1); % find the first nonzero row 
    [y2,x2] = find(mA,1,'last'); % find the last nonzero row
    m = (y1-y2)/(x1-x2); % the line slope
    n = y1-m*x1; % the intercept
    f_line = @(x) m.*x+n; % the line function
    
    thick = mode(sum(mA)); % mode thickness of the line
    tmp = (1:thick)-ceil(thick/2); % helper vector for expanding
    rows = bsxfun(@plus,tmp.',floor(f_line(1:size(A,2)))); % all the rows for each coloumn
    rows(rows<1) = 1; % make sure to not get out of range
    rows(rows>size(A,1)) = size(A,1); % make sure to not get out of range
    inds = sub2ind(size(A),rows,repmat(1:size(A,2),thick,1)); % convert to linear indecies
    mA(inds) = 1; % add the interpolation to the mask
    result = A.*mA; % apply the mask on A