将像素设置为特定值 [英] Setting pixels to a specific value

查看:171
本文介绍了将像素设置为特定值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 matlab 中编写了这个函数,该函数设置了像素 x code> y = 1 1 如下:

 函数c = core(x,y)
tolerance = 0.01;
pixels = [];
index = 1;
for i = 1:length(y)
for j = 1:length(y)
如果abs(y(i,j)-1)<=公差
x (i,j)= 1;
像素(索引)= x(i,j);
结束
结束
结束
c =像素;
end

由于我从脚本调用这个函数,我该如何返回那些设置为 1 的像素?或者,在这里以正确的方式返回原始图像,其像素符合设置为 1 的标准。在继续之前,我没有看到图像中符合标准的像素被设置为 1 $ C>。是不是我的像素设置为 1 正确?



底线是我假设核心代表具有等于 1 的隶属度的像素。而且,在我试图实现的算法中,我有以下行:

  C1 =核心(F)

其中 F 代表图片。



基于此,在 matlab 中编写这个函数的正确方法是什么。那么,是的,在 matlab 这一行可以简单地写成:

  C。('C1')= core(x,y); 

但是,根据上述信息,问题将返回给我的调用脚本,如何?



而且,作为输出,我总是在 1 > ANS 。为什么会这样?



谢谢。

解决方案

您在函数右侧传递的参数将被视为函数的本地参数,并且不会在外部进行更新。所以要获得更新的图像,请将其返回到左侧。



其次,算法中存在错误:

<



<2-> 循环不会扫描所有图像。 > index 变量永远不会被更新。



下面的这个函数应该可以达到你想要的:

 函数[x,像素] =核心(y)
容差= 0.01;
pixels = [];
index = 1;
for i = 1:size(y,1)
for j = 1:size(y,2)
index = j + i * size(y,2);如果abs(y(i,j)-1)<=容差
x(i,j)= 1,则为
;
像素= [像素索引];
end
end
end
end

编辑:



一个简单的方法可以在没有循环的情况下执行:

 容差= 0.01; 
x = zeros(size(y)); $(abs(y)-1)≤=容差)= 1;
pixels = find(x == 1);


I wrote this function in matlab that sets the value of the pixels x that have a degree of membership y = 1 to 1 as follows:

function c = core(x, y)
        tolerance = 0.01;
        pixels = [];
        index = 1;
        for i=1:length(y)
            for j=1:length(y)
                if abs(y(i,j)-1) <= tolerance
                x(i,j) = 1;
                pixels(index) = x(i,j);
                end
            end
            end
            c = pixels;
       end

Since I'm calling this function from a script, how can I return back those pixels that were set to 1? Or, will the correct way here to return the original image with the pixels that met the criterion set to 1.

Bur, before I continue, I didn't see that the pixels in the image that met the criterion were set to 1. Isn't my of setting the pixel to 1 correct?

The bottom line is that I'm assuming that core represents those pixels that had the degree of membership equal to 1. And, in the algorithm I'm trying to implement, I have the following line:

C1 = core(F)

Where F represents the image.

Based on that, what is the correct way to write this in matlab. Well, yes, in matlab this line can simply be written as:

C.('C1') = core(x,y);

But, the question is, based on the information above, what will be returned to my calling script and how?

And, yes, as an output, I'm always getting 1 in ans. Why is that?

Thanks.

解决方案

First off, all the parameters you pass on the right hand side of the function are treated as local parameters to the function and do not get updated outside. So to get the updated image, return it on the left side.

Second there are errors in your algorithm:

1- the for loops does not scan all the image.

2- the index variable never gets updated.

This function below should achieve what you want:

function [x,pixels] = core(y)
    tolerance = 0.01;
    pixels = [];
    index = 1;
    for i=1:size(y,1)
        for j=1:size(y,2)
            index = j+i*size(y,2);
            if abs(y(i,j)-1) <= tolerance
            x(i,j) = 1;
            pixels = [pixels index];
            end
        end
    end
end

EDIT:

A much easier way to do this without looping:

tolerance = 0.01;
x = zeros(size(y));
x((abs(y)-1) <= tolerance) = 1;
pixels = find(x==1);

这篇关于将像素设置为特定值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆