How to add noise to rect fuction (rectangle) ?

2 views (last 30 days)
Dimitar
Dimitar on 20 Nov 2014
Answered: Image Analyst on 21 Nov 2014
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?

Answers (2)

Image Analyst
Image Analyst on 21 Nov 2014
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 on 20 Nov 2014
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 Comment
Dimitar
Dimitar on 20 Nov 2014
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
.

Sign in to comment.

Tags

Community Treasure Hunt

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

Start Hunting!