开发者

Color issue in MATLAB

开发者 https://www.devze.com 2023-04-09 22:05 出处:网络
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 appear

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.

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?

The following is my code.

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) || (开发者_如何学Gop2(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


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.


I presume you then do image(img) in order to display the image. In that case, [255 255 255] will correspond to white as a true color value, regardless of the color map. You need to create a monochrome image, which will then be treated as indexed data and displayed using the color map.

See the Tips section of the image help.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号