求彩色光栅的MATLAB程序

求彩色光栅的MATLAB程序,第1张

function IMA = grating(m,n,varargin)

% 绘制光栅

% grating(m,n) 绘制光栅图(灰度图),渗毁m为灰度值矩阵的态喊亮行数,n为列数

% grating(m,n,'PARAM1',val1, 'PARAM2',val2, ...) 用成对出现的参数名/参数值

% 指定光栅图的显示属性,可用的参数名/参数值如下:

% 'Direction' -- 设定光栅条纹的方向,其参数值为:

% 'horizontal' -- 水平条纹

% 'Vertical' -- 竖直条纹

% 'circle' -- 同心圆条纹

% 'radiated' -- 辐射状条纹

%

% 'Velocity' -- 设定角速度,其参数值为标量

%

% 'Angle' -- 设定初始相位角,其参数值为标量

%

% 'Color' -- 设定光栅条纹的颜色,其参数值为:

% 'Gray' -- 黑白条纹

% 'Rgb' -- 彩色条纹

%

% IMA = grating( ... ) 返回光栅图对应的灰度值矩阵

%

% CopyRight:xiezhh(谢中华)

% 2011.11.26

% Example:

% grating(512,512,'direction','rad','Velocity',5000,'color','rgb')

% grating(512,512,'Velocity',20,'maxgrayval',200)

% grating(512,512,'direction','ver','Velocity',40)

% grating(512,512,'Velocity',20,'maxgrayval',200,'direction','cir')

if nargin <2

error('输入参数过少')

end

[Direction,Vel,Ang,Col,MaxGrayVal] = parseInputs(varargin{:})

if strncmpi(Direction,'hor',3)

x = (1:m)'

Theta = x*pi/Vel

Theta = repmat(Theta,[1,n])

elseif strncmpi(Direction,'ver',3)

x = 1:n

Theta = x*pi/Vel

Theta = repmat(Theta,[m,1])

elseif strncmpi(Direction,'cir',3)

x = repmat((1:n),[m,1])-n/2

y = repmat((1:m)',[1,n])-m/2

D = sqrt(x.^2 + y.^2)

Theta = D*pi/Vel

elseif strncmpi(Direction,'rad',3)

x = repmat((1:n),[m,1])-n/2

y = repmat((1:m)',[1,n])-m/2

Theta = atan2(y,x)

Theta = Theta*Vel

else

error('方向参数只能为Horizontal,帆宽Vertical,Circle, Radiated 之一')

end

if strncmpi(Col,'gra',3)

I = cos(Theta + Ang)

I = mat2gray(I)

IM = uint8(MaxGrayVal*I)

elseif strncmpi(Col,'rgb',3)

I = zeros(m,n)

IM = uint8(zeros(m,n,3))

for i = 1:3

I = cos(Theta + i*Ang)

IM(:,:,i) = uint8(MaxGrayVal*mat2gray(I))

end

else

error('颜色参数只能为Gray, Rgb 之一')

end

if nargout == 0

figure

imshow(IM)

else

IMA = IM

end

%--------------------------------------------------------------------------

% 解析输入参数

%--------------------------------------------------------------------------

function [Direction,Vel,Ang,Col,MaxGrayVal] = parseInputs(varargin)

if mod(nargin,2)~=0

error('输入参数个数不对,应为成对出现')

end

pnames = {'direction','velocity','angle' 'color','maxgrayvalue'}

dflts = {'horizontal',30,pi/3,'gray',255}

[Direction,Vel,Ang,Col,MaxGrayVal] = ...

internal.stats.parseArgs(pnames, dflts, varargin{:})

validateattributes(Direction,{'char'},{'nonempty'},mfilename,'direction')

validateattributes(Vel,{'numeric'},{'scalar','nonempty'},mfilename,'velocity')

validateattributes(Ang,{'numeric'},{'scalar','nonempty'},mfilename,'angle')

validateattributes(Col,{'char'},{'nonempty'},mfilename,'color')

validateattributes(MaxGrayVal,{'numeric'},{'scalar','nonempty'},mfilename,'maxgrayvalue')

你好吖,假设我们的视场角为键手1°,则每像素对应的空间角度为 1/N °。

计算角频率(以弧银唯度/像素为单位):

angular_frequency = 2 * pi * cycles_per_pixel

生成一个二维网格,对应于每个像素的x坐标:

[x, y] = meshgrid(1:N, 1:N)

根据正弦函数生稿搏嫌成光栅图像:

amplitude = 1% 光栅振幅

grating = amplitude * sin(angular_frequency * x)

显示生成的光栅图像:

figure

imshow(grating, [])

title('5 cpd Sine Grating')

现在就可以生成了一个正弦光栅图像,其空间频率为每度5个周期(5 cpd),具有指定的图像大小和振幅。

如果帮到您了,请采纳点赞哦~~谢谢(●'◡'●)

光栅尺的输出陵前轿信号有很多种,有2路TTL输出(起始就是AB相输出)、有RS422输出(串口通讯)、有4-20mA或者0-20mA输出(模拟量输出)。等等

因此尺肆PLC对于不同的光栅尺输出信号,就需要不同的编程。

2路TTL信号的的光栅尺,PLC程序中就用高速计数器。

RS422输出的光栅尺悔蔽,PLC程序中就用通讯指令。

模拟量输出的光栅尺,PLC程序中就用模拟量输入采集。

望采纳。。。。。。


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/yw/12425659.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-25
下一篇 2023-05-25

发表评论

登录后才能评论

评论列表(0条)

保存