jpeg_read Can't open file error

I am trying to read a set of pics. I'm also quite new to matlab and may have made an error when editing the givven code. Here is the source of the main function (which is called by another file but is "self-standing"):
function [Hist, file_path] = dct_coef_hist_from_list(file)
% This function extracts DCT coefficient histograms of images provided in
% a file
% Input:
% file: the file containing all paths
% channel: 1 (luminance), 2 (chrominance)
% Nc: number of analyzed DCT coeff in each 8x8 blocks
% BT: the limit of histogram ==> number of bins = 2*BT + 1
% Output:
% Hist: the nxd histogram matrix; n is the number of images; d is the
% number of histogram bins
% file_path: a list containing file paths (used to split data later)
Nc = 9; % number of AC coefficients (zig zag scan)
BT = 20; % maximum bin value => number of bins = 2*BT+1
fprintf("Filepath: %s\nCurrent directory %s\n", file, pwd);
if exist(file, "file")
fprintf("DCHFL: File exists: %s\n", file);
else
fprintf("DCHFL: File does not exist: %s\n", file);
end
try
fid = fopen(file, 'r');
file_path = textscan(fid, '%s', 'delimiter', '\n', 'whitespace', '');
file_path = file_path{1};
N = length(file_path);
fprintf('\nNumber of images: %d \n', N);
Hist = zeros(N, (2*BT+1)*Nc);
for i = 1:N
[~,file_name,ext] = fileparts(file_path{i});
fprintf('process file: %s\n', [file_name ext]);
fprintf('process file: %s\n', file_path{i});
Hist(i,:) = dct_coef_hist(file_path{i}); % <-- error in this call
end
catch me
fprintf("Error opening file: %s\n", me.message());
end
end
This is the dct_coef_hist.m file:
function histr = dct_coef_hist(imName)
% This function extracts histogram of DCT coefficients
% Input:
% coeff_arrays: 1x3 cell array of coefficients
% Nc: number of analyzed DCT coeff in each 8x8 blocks
% BT: the limit of histogram ==> number of bins = 2*BT + 1
channel = 1; % luminance channel
Nc = 9; % number of AC coefficients (zig zag scan)
BT = 20; % maximum bin value => number of bins = 2*BT+1
% the original code had no try-catch statement. I added it and I'm not sure
% it is syntatically correct
fprintf('imName: %s\n', imName);
jobj = [];
coef_arrays = [];
quant_table = [];
try
jobj=jpeg_read(imName); % <- I set a breakpoint here, img is attached
coef_arrays = jobj.coef_arrays;
quant_table = jobj.quant_tables;
catch me
fprintf("[dct_coef_hist.m]: jpeg_read() error: %s\n", me.message);
end
coef_arrays = jobj.coef_arrays;
quant_table = jobj.quant_tables;
[h,w] = size(coef_arrays{channel});
nblks = (floor(h/8)-1)*(floor(w/8)-1);
coefs = coef_arrays{1};
hist = zeros(Nc, (2*BT+1));
zigzag_inds = zigzag(reshape(1:64, [8 8]));
zigzag_inds = zigzag_inds(2:Nc+1);
for b = 0:nblks-1
i = 8*(floor(b / (floor(w/8)-1))) + 1;
j = 8*mod(b, (floor(w/8)-1)) + 1;
block = coefs(i:i+7, j:j+7);
block = block(zigzag_inds);
quant_values = quant_table{channel}(zigzag_inds);
for n = 1:Nc
v = block(n) * quant_values(n);
if v > BT
hist(n, end) = hist(n, end) + 1;
elseif v < -BT
hist(n, 1) = hist(n, 1) + 1;
else
hist(n, v+BT+1) = hist(n, v+BT+1) + 1;
end
end
end
hist = hist ./ nblks;
histr = reshape(hist, 1, []);
end
The jpeg_read.m is:
function output = jpeg_read(imIn);
% JPEG_READ Read a JPEG file into a JPEG object struct
%
% JPEGOBJ = JPEG_READ(FILENAME) Returns a Matlab struct containing the
% contents of the JPEG file FILENAME, including JPEG header information,
% the quantization tables, Huffman tables, and the DCT coefficients.
%
% This software is based in part on the work of the Independent JPEG Group.
%
% See also JPEG_WRITE.
% Modified by Markos Zampoglou (markzampoglou@iti.gr), ITI-CERTH 2016.
% It now operates as a function
% Phil Sallee 6/2003
error("Mex routine jpeg_read.c not compiled\n");
This is the console window output:
fname_in =
'dat/controlled_config_3_test.dat'
fname_in: dat/controlled_config_3_test.dat
Filepath: dat/controlled_config_3_test.dat
Current directory /home/bojanpoletan/Proj
DCHFL: File exists: dat/controlled_config_3_test.dat
Number of images: 6946
process file: original-041-1012x1800.jpg
process file: dataset/controlled/FB/QF-50/original-041-1012x1800.jpg
imName: dataset/controlled/FB/QF-50/original-041-1012x1800.jpg
[dct_coef_hist.m]: jpeg_read() error: Can't open file
Error opening file: Dot indexing is not supported for variables of this type.
I was able to track the error down in the jpeg_read.c file, here:
/* open file */
if ((infile = fopen(filename, "rb")) == NULL)
mexErrMsgTxt("Can't open file");
Here is an image of the value of imName variable:
Any idea?
What about editing and re-compiling the .C file with mex to get more info?
Thanks a lot.

Réponses (2)

Jan
Jan le 1 Juin 2022

0 votes

Work with absolute path names. 'dataset/controlled/FB/QF-50/original-041-1012x1800.jpg' does not include the folder '/home/bojanpoletan/Proj'. Use fullfile() to join the folder and the file name.

5 commentaires

Hi @Jan, thanks for your support.
Are you suggesting to wtite:
imName = fullfile("/home/bojanpoletan/Proj/", "dataset/controlled/FB/QF-50/original-041-1012x1800.jpg")
jobj=jpeg_read(imName);
instead of
jobj=jpeg_read(imName);
but in this case the output is:
process file: original-031-1012x1800.jpg
process file: dataset/controlled/FB/QF-50/original-031-1012x1800.jpg
imName: dataset/controlled/FB/QF-50/original-031-1012x1800.jpg
imName after fullfile: /home/bojanpoletan/Proj/dataset/controlled/FB/QF-50/original-031-1012x1800.jpg
[dct_coef_hist.m]: jpeg_read() error: Filename must be a string
Error opening file: Dot indexing is not supported for variables of this type.
which I guess is caused in jpeg_read.c (lines: 173-176, aprox):
/* check input value */
if (nrhs != 1) mexErrMsgTxt("One input argument required.");
if (mxIsChar(prhs[0]) != 1)
mexErrMsgTxt("Filename must be a string");
"string" is a new type in Matlab. Before R2016b the term"string" was used for CHAR vectors. The MEX function uses the older method to represent strings, so chnage the doublequotes to quotes:
imName = fullfile('/home/bojanpoletan/Proj/', 'dataset/controlled/FB/QF-50/original-041-1012x1800.jpg')
Bojan Poletan
Bojan Poletan le 1 Juin 2022
Still not working, same error.
Jan
Jan le 2 Juin 2022
And you are sure that this file is existing?
Bojan Poletan
Bojan Poletan le 2 Juin 2022
Modifié(e) : Bojan Poletan le 2 Juin 2022
I commented below :)
it looks like the first file that is read is... like key sensitive and others are not...

Connectez-vous pour commenter.

Image Analyst
Image Analyst le 1 Juin 2022

0 votes

I would just use imread instead of your jpeg_read. Any reason not to?

9 commentaires

Bojan Poletan
Bojan Poletan le 1 Juin 2022
Well, I was supposed just to run the code as it is. It is supposed to be fully working (but it isn't on my pc/linux/mac, ecc). So changing the code my teacher did and used at work would be... very weird.
Image Analyst
Image Analyst le 1 Juin 2022
So what did your teacher say about the code throwing an error?
Bojan Poletan
Bojan Poletan le 1 Juin 2022
Modifié(e) : Bojan Poletan le 1 Juin 2022
He sait that it works perfectly in the laboratory and could not really help me with this problem. I guess he has no time to spend on me and I can understand that. Googling helped a lot with other errors i was prompted but not this one.
Image Analyst
Image Analyst le 1 Juin 2022
If the entire jpeg_read() function is comments plus this one line of code:
error("Mex routine jpeg_read.c not compiled\n");
then all it should do it throw an error about the Mex routine not being compiled. But you say it's throwing an error about dot indexing so I think there is more to the jpeg_read() function than that one line you gave.
Bojan Poletan
Bojan Poletan le 2 Juin 2022
As you can see I was given only this
Walter Roberson
Walter Roberson le 2 Juin 2022
After constructing the file name but before calling jpeg_read try verifying that the file is found, using exists(). If it does, try using fopen() and be sure to record the second output of fopen to see why any failure happens
Ok, good news.
With my file manager I renamed the
fb
directory to
FB
and now the path
dataset/controlled/FB/QF-50/original-041-1012x1800.jpg
After that I restarted the code and it worked.
So the problem was the folder name (which was not writte in capital letters).
Now, I have 30 directories and only the one above (FB) have been renamed by me to upper case. All other directories still have the old low case name and despite this the code have no problems reading images inside.
So all these are valid
dataset/controlled/TW/QF-50/original-041-1012x1800.jpg
dataset/controlled/TW/QF-50/original-041-1012x1800.jpg
dataset/controlled/FL/QF-50/original-041-1012x1800.jpg
dataset/controlled/FL-TW/QF-50/original-041-1012x1800.jpg
dataset/controlled/FL-FL/QF-50/original-041-1012x1800.jpg
...
even if the path is:
dataset/controlled/tw/QF-50/original-041-1012x1800.jpg
dataset/controlled/tw/QF-50/original-041-1012x1800.jpg
dataset/controlled/fl/QF-50/original-041-1012x1800.jpg
dataset/controlled/fl-tw/QF-50/original-041-1012x1800.jpg
dataset/controlled/fl-fl/QF-50/original-041-1012x1800.jpg
I don't know what to say Why it complained only for one single file path? More precisely "the first file path", whatever it was!
Walter Roberson
Walter Roberson le 2 Juin 2022
Linux? I know that ext4 filesystem can be configured for case insensitive (normally sensitive)
Would you just happen to be mounting a remote filesystem onto those directories?
Bojan Poletan
Bojan Poletan le 2 Juin 2022
Yes, it is linux: Ubuntu 18 (if I remember well).
I got an access to the university computer running all the stuff related to image data elaboration and matlab so I did not mount anythng, just used scp to copy datas from my local pc to the destination.

Connectez-vous pour commenter.

Produits

Version

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by