insertShape function doesn't read color triplet

5 ビュー (過去 30 日間)
gabriele
gabriele 2018 年 12 月 7 日
編集済み: DGM 2023 年 9 月 11 日
The function is able to read colors as strings ('red', 'green'...etc...) but cannot read modified color vectors like [1 0 0] or [0.6 1 0.2]. If I use this as an input the shape just come out black.
My color triplet is type double.
Here is the line:
img = insertShape(img, 'Line', position(i,:), 'Color', ColMap(c,:), 'LineWidth', w);
position and line width are perfect. ColMap ia a Mx3 matrix double.
What I dont understand is that using the function 'plot' to simply plot a line over an image everything works perfectly including the color. Meaning, the following line works:
line = plot([x1 x2], [y1 y2], 'Color', ColMap(c,:), 'LineWidth', w);
Any idea?
thank you

採用された回答

Image Analyst
Image Analyst 2018 年 12 月 7 日
Unlike other functions that require colors in the range 0-1, insertShape expects values in the range 0-255: Try this:
% Try it with a gray scale image.
img = imread('moon.tif');
position = [50, 20, 200, 500];
i = 1;
w = 13;
c = 220;
ColMap = jet(256); % In the range 0-1
customColor = round(255 * ColMap(c, :))
img = insertShape(img, 'Line', position(i,:), 'Color', customColor, 'LineWidth', w);
% img is now converted into an RGB image. No longer a gray scale image.
subplot(2, 1, 1);
imshow(img);
axis('on', 'image');
% Try it with a color image.
img = imread('peppers.png');
img = insertShape(img, 'Line', position(i,:), 'Color', customColor, 'LineWidth', w);
subplot(2, 1, 2);
imshow(img);
axis('on', 'image');
0001 Screenshot.png
  2 件のコメント
gabriele
gabriele 2018 年 12 月 8 日
Correct. wow that was easy, should have thought about that.
Thank you!
DGM
DGM 2023 年 3 月 20 日
編集済み: DGM 2023 年 9 月 11 日
It appears that the documentation is simply wrong -- rather, the webdocs are wrong, but the synopsis is correct.
It will accept unit-scale color specification, but only so long as the image is floating point.
position = [50, 20, 200, 500];
color = lines(1); % a very familiar blue (unit-scale double)
img = im2double(imread('peppers.png')); % double
img = insertShape(img, 'Line', position, 'Color', color, 'LineWidth',15);
imshow(img);
color = double(im2uint16(color)); % uint16-scale float
img = im2uint16(imread('peppers.png')); % uint16
img = insertShape(img, 'Line', position, 'Color',color, 'LineWidth',15);
imshow(img);
color = int16(im2uint8(uint16(color))); % uint8-scale int16
img = im2uint8(imread('peppers.png')); % uint8
img = insertShape(img, 'Line', position, 'Color',color, 'LineWidth',15);
imshow(img);
In other words, the color tuple must be scaled to match the class of the image, regardless of the class of the tuple itself.
See also:

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeComputer Vision with Simulink についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by