How can I loop process that converts the images to csv and saves it in another directory. ( have a lot of images)

댓글 수: 3

Stephen23
Stephen23 2018년 8월 6일
" converts the images to csv "
What does this mean exactly? I don't know of any CSV standard that support 3D arrays, which is likely what you have with that image data. Please explain how to convert a 3D image array into a 2D CSV file.
alexey shnyder
alexey shnyder 2018년 8월 6일
this is the example
"this is the example"
So, you show us an image and give us a text file that contains some numbers. And you leave it to us to work out how the two relate to each other? Perhaps, you should explain what's in that text file.
A first glance, it looks like that text file is a partial copy of a matrix output on the command line, as witnessed by the Columns xxx through yyy lines that appear throughout the file similar to:
...
255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255
255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255
Columns 521 through 540
255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255
255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255
...
This is nothing like what is conventionally called a csv file.

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

답변 (2개)

KALYAN ACHARJYA
KALYAN ACHARJYA 2018년 8월 6일
편집: KALYAN ACHARJYA 2018년 8월 6일

1 개 추천

%Save all images name in a sequence manner before doing the operation
% names for example im1,im2,im3..or 1,2,3...so on
%Save the folder of images in the current directory
path_directory='cov_test'; % 'Folder name' having images
original_files=dir([path_directory '/*.jpg']);
for k=1:length(original_files)
filename=[path_directory '/' original_files(k).name];
image1=imread(filename);
image=rgb2gray(image1);
destination='D:\Kalyan Works\Matlab Work\csv_im\im'; %Save Folder path
dlmwrite([destination,num2str(k),'csv'],image);
end

댓글 수: 7

%Save all images name in a sequence manner before doing the operation
% names for example im1,im2,im3..or 1,2,3...so on
%Save the folder of images in the current directory
path_directory='C:\Users\Bankin\Desktop\test\test'; % 'Folder name' having images
original_files=dir([path_directory 'C:\Users\Bankin\Desktop\test*.jpg']);
for k=1:length(original_files)
filename=[path_directory 'C:\Users\Bankin\Desktop\test' original_files(k).name];
image=imread((filename));
destination='C:\Users\Bankin\Desktop\result'; %Save Folder path
csvwrite(image,[destination,num2str(k)]);
end
alexey shnyder
alexey shnyder 2018년 8월 6일
it doesnt work am i doing something wrong?
Jan
Jan 2018년 8월 6일
Please explain "doesn't work" with any details. We cannot guess, what you observe.
alexey shnyder
alexey shnyder 2018년 8월 6일
편집: Stephen23 2018년 8월 6일
I put this code :
%Save all images name in a sequence manner before doing the operation
% names for example im1,im2,im3..or 1,2,3...so on
%Save the folder of images in the current directory
path_directory='C:\Users\Bankin\Desktop\test\test'; % 'Folder name' having images
original_files=dir([path_directory 'C:\Users\Bankin\Desktop\test\test/*.jpg']);
for k=1:length(original_files)
filename=[path_directory 'C:\Users\Bankin\Desktop\test\test' original_files(k).name];
image=imread(rgb2gray(filename));
destination='C:\Users\Bankin\Desktop\result'; %Save Folder path
csvwrite(image,[destination,num2str(k)]);
end
and I get this. I just want to convert images to csv. so I enter folder for saving csv and I expect that code will extract to this folder csv files. Matlab is a totally new field for me so I am sorry for so stupid questions.
Guillaume
Guillaume 2018년 8월 6일
I just want to convert images to csv
csv is a text format for storing numbers usually as a 2D rectangle. You will need to explain what numbers you want to store from your image. If the image is greyscale, it can simply be the pixels intensity. However, if the image is colour, where each pixel is represented by a RGB triplet, you will have to explain how that is converted to a 2D grid of numbers. Either that, or you need to explain how your csv file format work to save 3D data.
Guillaume
Guillaume 2020년 1월 7일
Comment by Teik Jin Lim mistakenly posted as an Answer moved here
Hi,
I do the exactly same code as mentioned. After inputting in the command window, MATLAB does not provide any feedback and respond.
Can anyone help me to solve this ?
Why do you expect any output in the command window? All the code does is create files, admittedly in an unexpected location.
I would expect that you find store1csv and store2csv (no extension) in C:\Users\Jin\Desktop\bioid (i.e. the current directory).
Had the original code used proper path building functions (fullfile) and proper string building functions (sprintf) I suspect that the files would end up where they're intended with the correct name:
source_directory = 'Whatever you want';
destination_directory = 'Wherever';
original_files = dir(fullfile(source_directory, '*.jpg')); %guaranteed to work on any OS. Don't build paths yourself
for fileidx = 1:numel(original_files)
img = imread(fullfile(source_directory, original_files(fileidx).name)); %again use fullfile to build paths
if ndims(img) == 3 %colour image, convert to grey
img = rgb2gray(img);
end
dlmwrite(fullfile(destination_directory, sprintf('%d.csv', fileidx)), img);
end

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

alexey shnyder
alexey shnyder 2018년 8월 6일

0 개 추천

I load variable by this function M = imread('310-4_RH_FO.jpg'); and after convert it to scv by this function dlmwrite('myfile.csv',M). how can I create Loop for this functions ?

댓글 수: 6

KALYAN ACHARJYA
KALYAN ACHARJYA 2018년 8월 6일
편집: KALYAN ACHARJYA 2018년 8월 6일
I have edited the answer, its perfectly work for me, pls check
KALYAN ACHARJYA
KALYAN ACHARJYA 2018년 8월 6일
KALYAN ACHARJYA
KALYAN ACHARJYA 2018년 8월 6일
편집: KALYAN ACHARJYA 2018년 8월 6일
Please note that all images should have proper sequence names, so that call images perfectly works. Let me know Is the problem solved or not?
alexey shnyder
alexey shnyder 2018년 8월 6일
I gave sequence names to images and still have the same problem.
@KALYAN ACHARJYA: the warning that you are getting is very clear, and has nothing to do this code. You have named a function save. Naming functions with the same name as inbuilt MATLAB functions is a bad idea, which is why it is warning you about this conflict.
Solution: rename your function.
Tip: always check if a function name is used bfore using it yourself:
which save -all
alexey shnyder
alexey shnyder 2018년 8월 7일
thank you very much you help me a lot. everything work.

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

카테고리

도움말 센터File Exchange에서 Convert Image Type에 대해 자세히 알아보기

제품

질문:

2018년 8월 6일

댓글:

2020년 1월 7일

Community Treasure Hunt

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

Start Hunting!

Translated by