Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/hadoop/6.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
在Matlab中随机选择图片中的像素_Matlab - Fatal编程技术网

在Matlab中随机选择图片中的像素

在Matlab中随机选择图片中的像素,matlab,Matlab,我有一个灰度图片,我想手动添加噪音。首先,我想随机选择一个像素,生成一个从0到1的随机值,将该值乘以255,并用新得到的数字替换该像素以前的值,然后重复该过程100次 我相信我已经记下了大部分代码 clc; fid = fopen(str); myimage = fread(fid, [512 683]); fclose(fid); for i = 1:100 A(i) = rand(1) * 255; end 我只是不知道如何从图像中随机选择100个像素,以及如何用我创建的值替换

我有一个灰度图片,我想手动添加噪音。首先,我想随机选择一个像素,生成一个从0到1的随机值,将该值乘以255,并用新得到的数字替换该像素以前的值,然后重复该过程100次

我相信我已经记下了大部分代码

    clc;
fid = fopen(str);
myimage = fread(fid, [512 683]);
fclose(fid);


for i = 1:100
A(i) = rand(1) * 255;
end

我只是不知道如何从图像中随机选择100个像素,以及如何用我创建的值替换它们。非常感谢您的帮助。

您需要找到100个随机像素的索引:

rPix = floor(rand(1,100) * numel(myimage)) + 1;
rVal = rand(1,100);
myimage(rPix) = 255 * rVal;
解释

rand(1,100)  : an array of 1 x 100 random numbers
numel(myimage) : number of pixels
product of the two : a random number between 0 and n
floor() : the next smallest integer. This "almost" points to 100 random pixels; we're off by 1, so
+ 1 : we add one to get a valid index.
我们现在有了一个有效的随机索引。请注意,在Matlab中,在2D数组中使用1D索引是有效的,只要使用的数字不大于数组中的元素数。因此如果

A = rand(3,3);
b = A(5);

b = A(2,2); % because the order is A(1,1), A(2,1), A(3,1), A(1,2), A(2,2), ...
下一行:

rVal = rand(1, 100);
生成100个随机数(介于0和1之间)。最后一行

myimage(rPix) = 255 * rVal;
myimage
中(随机)索引100个元素,并将
rVal
中的值乘以255。这是Matlab非常强大的一部分:矢量化。您可以在一次操作中对多个数字进行Matlab操作(而且,为了提高速度,应该始终尝试使用Matlab)。以上相当于

for ii = 1:100
  myimage(rPix(ii)) = 255 * rVal(ii);
end

只有快得多…

您需要找到100个随机像素的索引:

rPix = floor(rand(1,100) * numel(myimage)) + 1;
rVal = rand(1,100);
myimage(rPix) = 255 * rVal;
解释

rand(1,100)  : an array of 1 x 100 random numbers
numel(myimage) : number of pixels
product of the two : a random number between 0 and n
floor() : the next smallest integer. This "almost" points to 100 random pixels; we're off by 1, so
+ 1 : we add one to get a valid index.
我们现在有了一个有效的随机索引。请注意,在Matlab中,在2D数组中使用1D索引是有效的,只要使用的数字不大于数组中的元素数。因此如果

A = rand(3,3);
b = A(5);

b = A(2,2); % because the order is A(1,1), A(2,1), A(3,1), A(1,2), A(2,2), ...
下一行:

rVal = rand(1, 100);
生成100个随机数(介于0和1之间)。最后一行

myimage(rPix) = 255 * rVal;
myimage
中(随机)索引100个元素,并将
rVal
中的值乘以255。这是Matlab非常强大的一部分:矢量化。您可以在一次操作中对多个数字进行Matlab操作(而且,为了提高速度,应该始终尝试使用Matlab)。以上相当于

for ii = 1:100
  myimage(rPix(ii)) = 255 * rVal(ii);
end

只有快得多…

要获得随机像素,您可以获取两个变量xy,并在限制内为每个变量生成随机值。生成随机像素值,并用得到的随机值替换(x,y)处的值。它看起来像:

for i=1:100
  x = randi([1 512]);
  y = randi([1 683]);
  myimage(x,y) = rand(1)*255;
end;

要获得随机像素,可以获取两个变量xy,并在限制内为每个变量生成随机值。生成随机像素值,并用得到的随机值替换(x,y)处的值。它看起来像:

for i=1:100
  x = randi([1 512]);
  y = randi([1 683]);
  myimage(x,y) = rand(1)*255;
end;

使用函数
randperm

image = imread('image_name.extension');
[row col] = size(image);
indices = randperm(row*col);
loc = randperm(100);

randomly_selected_pixels = image(indices(loc));

% Assign the values that you have to these "randomly_selected_pixels"

使用函数
randperm

image = imread('image_name.extension');
[row col] = size(image);
indices = randperm(row*col);
loc = randperm(100);

randomly_selected_pixels = image(indices(loc));

% Assign the values that you have to these "randomly_selected_pixels"

解释很好,它工作得很好,但我想知道我刚才做了什么。解释很好,它工作得很好,但我想知道我刚才做了什么。嗯,randi是一个matlab函数。您不需要包含任何类型的工具箱。我正在运行Matlab2012A,其中包含randi。您可以在matlab中运行help randi进行检查。好的-我正在运行旧版本。我站在更正-您修改的代码将工作。嗯,兰迪是一个matlab函数。您不需要包含任何类型的工具箱。我正在运行Matlab2012A,其中包含randi。您可以在matlab中运行help randi进行检查。好的-我正在运行旧版本。我站在更正-您修改的代码将工作。