Concatenating images from image files

Hello! I have a series of images saved in a folder, and I have written a short program to open two of these image files, concatenate them (preferably vertically, although for now I am trying horizontally), then save this new image to the same folder. This is what I have written so far:
function concatentateImages
%this is the folder where the original images are located path='/home/packremote/SharedDocuments/Amina/zEXAMPLE/';
file1 = strcat(cr45e__ch_21', '.pdf');
[image1,map1] = imread(graph1);
file2 = strcat('cr45f__ch_24', '.jpg');
[image2,map2] = imread(graph2);
image1 = ind2rgb(image1,map1);
image2 = ind2rgb(image2,map2);
image3 = cat(2,image1,image2);
%this is the directory where I want to save the new images
dircase=('/home/packremote/SharedDocuments/Amina/zEXAMPLE/');
nombrejpg=strcat(dircase, 'test', jpgext)
saveas(f, nombrejpg, 'jpg')
fclose('all');
However, I keep getting an error that my files do not exist, though I am certain the names are copied correctly.
I am currently using jpg files, but the format can be easily converted.
Any input on how to fix this error, or a nicer way of preforming this task is greatly appreciated!
Cheers,
Amina

댓글 수: 2

A
A 2011년 5월 27일
The images are 3D images, with dimensions:
Image1 = 901, 1200, 3
Image2 = 1420, 1952, 3
I want Image3 to appear as:
Image1
Image2
A
A 2011년 5월 27일
CAT arguments dimensions are not consistent.

댓글을 달려면 로그인하십시오.

 채택된 답변

Walter Roberson
Walter Roberson 2011년 5월 27일

0 개 추천

iwidth = max(size(image1,2),size(image2,2));
if size(image1,2) < iwidth
image1(1,iwidth,1) = 0;
end
if size(image2,2) < iwidth
image2(1,iwidth,1) = 0;
end
image3 = cat(1,image1,image2);

댓글 수: 6

A
A 2011년 5월 27일
Thank you! However, I'm having trouble with my saveas function, so I'm not able to see if this has concatenated them properly. Is there a way to display my new, concatenated image (image3) as a figure when I run the program so that I can look at it before saving it to a directory?
Walter Roberson
Walter Roberson 2011년 5월 27일
image(image3)
Your code line
nombrejpg=strcat(dircase, 'test', jpgext)
has the problem that you have not defined the variable "jpgext"
A
A 2011년 5월 27일
I used imshow to see the image. It looks great, except that a small part of the second image is being cut off, on the right of the image, and the quality appears to be quite poor.. could this because of the type of file that I am using? Thank you for your input :)
A
A 2011년 5월 27일
I sorted out the jpgext issue, it is because I borrowed this line from another code and forgot to initalize some parameters..
A
A 2011년 5월 27일
I think I will need to resize the original images, as I assume the reduction is quality of the final product (which is perfect, thank you!) is because the dimensions are too large. Is there a way to use NaN in your code?
Walter Roberson
Walter Roberson 2011년 5월 27일
I'm not sure how nan will help, but anyhow:
iwidth = max(size(image1,2),size(image2,2));
if size(image1,2) < iwidth
image1(:,end+1:iwidth,1) = nan;
end
if size(image2,2) < iwidth
image2(:,end+1:iwidth,1) = nan;
end
image3 = cat(1,image1,image2);

댓글을 달려면 로그인하십시오.

추가 답변 (2개)

Harsha Vardhan Rao  Avunoori
Harsha Vardhan Rao Avunoori 2011년 5월 27일

0 개 추천

Are you specifying the proper directory/path when you are executing your code ? Can you please post the exact error so that it would be a little bit easier to understand what's causing you trouble.

댓글 수: 4

A
A 2011년 5월 27일
Thanks for your response! This is the error it returns:
??? Undefined function or variable 'graph1'.
Error in ==> concatentateImages2 at 7
[image1,map1] = imread(graph1);
I am certain that the path is correct, and the file name as well.
Walter Roberson
Walter Roberson 2011년 5월 27일
[image1,map1] = imread(file1); %not graph1 !!
A
A 2011년 5월 27일
Sorry, I had c hanged it to file1 to post the question. Even when I have it written correctly it is returning the same error.
??? Error using ==> imread at 363
File "21.jpg" does not exist.
Error in ==> concatentateImages2 at 7
[image1,map1] = imread(file1);
Walter Roberson
Walter Roberson 2011년 5월 27일
Your line
file1 = strcat(cr45e__ch_21', '.pdf');
should be
file1 = strcat('cr45e__ch_21', '.pdf');
However, without that, the code would not have reached as far as reading the file.
Please post your current code.

댓글을 달려면 로그인하십시오.

Gowtham N
Gowtham N 2016년 3월 10일

0 개 추천

Hello. I want to read many images from a folder and want to concatinate the same in horizontal,how to do this with for loop?

댓글 수: 1

to stitch images together horizontally, use a comma:
wideImage = [image1, image2];
You can find a more general stitch program in the File Exchange: http://www.mathworks.com/matlabcentral/fileexchange/25797-stitch

댓글을 달려면 로그인하십시오.

질문:

A
A
2011년 5월 27일

댓글:

2016년 3월 11일

Community Treasure Hunt

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

Start Hunting!

Translated by