在MATLAB中的颜色问题 [英] Color issue in MATLAB
问题描述
我在写一个函数文件,用于在图像中绘制一条线。现在我面临的颜色问题。如果在命令窗口中设置 color = [255 255 255]
或 color = [128 128 128]
I'm writing a function file which is used to draw a line in an image. Now I'm facing the color issue. If I set color=[255 255 255]
or color=[128 128 128]
in the command window, the lines that appears in the image are both white.
对于 [128 128 128]
,它应该是灰色的, 对?其中不对应的颜色表。我已经测试了一些颜色值,结果是它需要大于零的任何数字为255.我如何解决这个问题?
For [128 128 128]
, it should be gray color, right? Which is not corresponding to the color table. I have tested some values for the color, the conclusion is that it takes any number greater than zero as 255. How do I fix this problem?
以下是我的代码。
function [ret]=drawline(p1,p2,color,img);
xmax=size(img,1);
ymax=size(img,2);
if (p1(1)>xmax) || (p2(1)>xmax) || (p1(2)>ymax) || (p2(2)>ymax)
error('value of point is ouside the image.');
elseif (p1(1)==xmax) || (p2(1)==xmax) || (p1(2)==ymax) || (p2(2)==ymax)
error('warning: value of point reach the max.');
elseif (color(1)>256) ||(color(2)>256)||(color(3)>256)
error('color value is out of range.');
else
m=(p2(2)-p1(2))/(p2(1)-p1(1));
m=round(m);
c=p1(2)-m*p1(1);
for x=linspace(p1(1),p2(1),1000)
y=m*x+c;
if p1(1)==p2(1)
x=p1(1);
y=p1(1):p2(2);
end
img(round(y),round(x),1)=color(1);
img(round(y),round(x),2)=color(2);
img(round(y),round(x),3)=color(3);
end
end
ret=img
推荐答案
您可能使用浮点图像,其中颜色范围是[0,1],而不是[0,255]。也许系统正在截断所有的值超过1,而不是提出一个错误。我不是matlab-savvy到足以纠正你的代码,不幸的是。
You might be using floating point images, where the color range is [0, 1] instead of [0, 255]. Perhaps the system is truncating all values over 1 instead of raising an error. I'm not matlab-savvy enough to correct your code, unfortunately.
这篇关于在MATLAB中的颜色问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!