Matlab can't show image after drawing a shape using shapeInserter

2 views (last 30 days)
I'm trying to use shapeInserter as shown here , but when i use imshow i get this error:
Error using imageDisplayValidateParams>validateCData (line 126)
RGB images must be uint8, uint16, single, or double.
Error in imageDisplayValidateParams (line 27)
common_args.CData = validateCData(common_args.CData,image_type);
Error in imageDisplayParseInputs (line 78)
common_args = imageDisplayValidateParams(common_args);
Error in imshow (line 219)
[common_args,specific_args] = ...
Error in myScript (line 35)
imshow(img1);
here is my code:
shapeInserter = vision.ShapeInserter('Shape','Rectangles','BorderColor','Custom', 'CustomBorderColor', uint8([255 0 0]));
rect = int32([x, y, 10, 10]);
rgb = repmat(img, [1, 1, 3]);
img1 = step(shapeInserter, rgb, rect);
imshow(img1);
Where img is a grayscale image i loaded before
  2 Comments
Geoff Hayes
Geoff Hayes on 11 Nov 2014
Gianfranco - in the Command Window, once you have executed the above code which generates the error, type
class(img1)
to determine the class or data type of img1. What does this call return?
Gianfranco
Gianfranco on 11 Nov 2014
Hi, the type of img, rgb and img1 is int16. "imshow()" works on img but not on rgb..

Sign in to comment.

Answers (1)

Geoff Hayes
Geoff Hayes on 12 Nov 2014
Gianfranco - according to imshow, an RGB image can be uint8, uint16, single, or double. A grayscale image can be any numeric data type. Since the data type for both images is int16, then since img is grayscale, you can use imshow. But you cannot do the same for rgb since its data type is not unsigned nor floating point.
I suppose that you could do one of two things - first, check to see what the minimum value of rgb is via
minRgbValue = min(rgb(:));
If this minimum is greater than or equal to zero, you could then just convert your image to a 16-bit unsigned integer as
if minRgbValue>=0
rgb = uint16(rgb);
imshow(rgb);
end
And the above should work fine since the RGB image data type is valid for imshow.
Else, if the minRgbValue is negative, you could convert the image to a double data type as
if minRgbValue>=0
rgb = uint16(rgb); % or rgb = im2uint16(rgb);
else
rgb = im2double(rgb);
end
imshow(rgb);
Try the above and see what happens!

Categories

Find more on Images in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!