Frame around an image
Show older comments
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)
Walter Roberson
on 18 Nov 2011
This statement;
img = zeros(newH,newW,d);
That creates a double array, not an array of the same data type as "color".
Nicolai
on 18 Nov 2011
1 Comment
Walter Roberson
on 18 Nov 2011
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.
Image Analyst
on 18 Nov 2011
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.
Categories
Find more on Contrast Adjustment in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!