How to add noise to rect fuction (rectangle) ?

3 次查看(过去 30 天)
How to add noise to the edges of 2D rect function? While all the rest is 1 and 0. So that the lines are not straight but still have value of 1.
x = linspace(1,10,100);
y = x;
[X Y] = meshgrid(x,y);
T = rect(X/2).*rect(Y/2);
function A = rect(x)
% rectangle function
A = abs(x) <=1/2;
end
how to add noise to line edges (borders) of T only?

回答(2 个)

Image Analyst
Image Analyst 2014-11-21
Try this:
clc;
clearvars;
close all;
workspace;
verticalElements = 100;
horizontalElements = 150;
x1 = 4;
y1 = 2;
x2 = 12;
y2 = 8;
% Make x values.
xTop = linspace(x1,x2,horizontalElements);
xBottom = fliplr(xTop);
xLeft = x1+rand(1,verticalElements) - 0.5;
xRight = x2+rand(1,verticalElements) - 0.5;
% Make y values.
yLeft = linspace(y1, y2,verticalElements);
yRight = linspace(y2, y1,verticalElements);
yTop = 8 + rand(1, horizontalElements) - 0.5;
yBottom = 2 + rand(1, horizontalElements) - 0.5;
% Make a box with jagged edges
xBox = [xTop, xRight, xBottom, xLeft];
yBox = [yTop, yRight, yBottom, yLeft];
% Close it
xBox(end+1) = xBox(1);
yBox(end+1) = yBox(1);
% Plot it.
plot(xBox, yBox, 'LineWidth', 2);
grid on;

Youssef  Khmou
Youssef Khmou 2014-11-20
The solution exists for one dimensional function, when you apply the function, you obtain a logical array, you need to convert it to double and find non zero indexes :
rec=@(x) abs(x)<=1/2;
f=rec(-2:0.01:2);
[a,b]=find(f~=0);
N=length(b);
s=0.15;% noise power
f=double(f);
f(b)=1+s*randn(1,N);
plot(f);
  1 个评论
Dimitar
Dimitar 2014-11-20
Thank you. But I would like it to have exact values of 1 and 0 everywhere on the 2D grid. With borders like polygons (rand noise) instead of straight lines
.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 NaNs 的更多信息

标签

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by