How to add noise to rect fuction (rectangle) ?

조회 수: 5 (최근 30일)
Dimitar
Dimitar 2014년 11월 20일
답변: Image Analyst 2014년 11월 21일
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에서 Logical에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by