Frame around an image

Hi.
I'm trying to wrap a frame around an image in matlab, but I can't get it working. I get the following error message:
Error using ==> image TrueColor CData contains element out of range 0.0 <= value <= 1.0
But the color array should already be of type uint8, converted by using the cast-command.
Any help will be appreciated! Thank you.
Btw, i'm a Matlab rookie :)
My code:
[X, cmapX] = readimage('images/RGB_Colored/Lena.tif');
dimX = size(X);
if numel(dimX)==2, dimX = [dimX 1]; end;
width = 100; % of the frame
color = [229 67 0];
color = reshape(color,1,1,numel(color));
color = cast(color,class(X)); % same type as X, uint8
h=dimX(1); w=dimX(2); d=dimX(3);
newW = w+2*width;
newH = h+2*width;
img = zeros(newH,newW,d); % zerofilled array
img(1,1,1:end)=color(:,:,1:end);
img(width+1:1:newH-width,width+1:1:newW-width,:)=X;
Y = img;
cmapY = cmapX;
if dimX(3)==1
xwindow = figure('Name','Nuværende billede');
image(ind2rgb(Y,cmapX));
axis image off
elseif dimX(3)==3
xwindow = figure('Name','Nuværende billede');
image(Y);
axis image off
else
disp('error');
end

Answers (3)

This statement;
img = zeros(newH,newW,d);
That creates a double array, not an array of the same data type as "color".
Nicolai
Nicolai on 18 Nov 2011
Thank you for your reply.
I see. What function should I use to make an array containing the frame and the picture then? If I translate zeros with cast to uint8, I get 0.
I tried
img = repmat(color,[newH newW d]);
But I get error with that too. What I am looking for is a array I can fill with a color like this
color = [229 67 0];
And then afterward insert the image. Inserting the image is working fine.

1 Comment

img = zeros(newH,newW,d,'uint8');
If you want to make img repeated copies of color, then
img = repmat(color,newH,newW,1);
Please re-check your image insertion code, and consider the cases:
1) you read in a truecolor (3D) image. The colormap will always be empty for this
2) you read in a grayscale (2D) image. The colormap will be empty for this.
3) you read in a pseudocolor (2D) image. The colormap will not be empty for this.
Consider that you are always creating a 3D matrix, img, but sometimes you are only copying X in to the first pane if it (:,:,1). Where your original was 2D or not, img is 3D. Then Y = img. Then if the original image was 2D, you try to use ind2rgb(Y,cmapX) which is not legal because Y is going to be 3D.

Sign in to comment.

Try this:
padRows = 100;
padCols = 50;
newImageR = padarray(rgbImage(:,:,1), [padRows padCols], 229);
newImageG = padarray(rgbImage(:,:,2), [padRows padCols], 67);
newImageB = padarray(rgbImage(:,:,3), [padRows padCols], 0);
newRGB = cat(3, newImageR, newImageG, newImageB);
padArray works on color images but not if you want different colored padding in each color channel. That's why I did it three times.

Asked:

on 18 Nov 2011

Community Treasure Hunt

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

Start Hunting!