jpeg_read Can't open file error
Afficher commentaires plus anciens
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
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
Bojan Poletan
le 1 Juin 2022
Jan
le 1 Juin 2022
"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
le 1 Juin 2022
Jan
le 2 Juin 2022
And you are sure that this file is existing?
Bojan Poletan
le 2 Juin 2022
Modifié(e) : Bojan Poletan
le 2 Juin 2022
Image Analyst
le 1 Juin 2022
0 votes
9 commentaires
Bojan Poletan
le 1 Juin 2022
Image Analyst
le 1 Juin 2022
So what did your teacher say about the code throwing an error?
Bojan Poletan
le 1 Juin 2022
Modifié(e) : Bojan Poletan
le 1 Juin 2022
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
le 2 Juin 2022
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
Bojan Poletan
le 2 Juin 2022
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
le 2 Juin 2022
Catégories
En savoir plus sur Image Data dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

