how can to calculate jpeg2000 file header size in matlab? - matlab

I want to know size of header in an image in jpeg2000 format. how can I find out this header?
I want to calculate pure size of image.jp2 in hard disk without the size of header.
I use iminfo to find some information in matlab about image, but I don't know that can I find the header size of the image from this?
Actually I compress an image with jpeg2000 and I want to calculate rate of compression without header size.
please help me.

J2K files have 4 compulsory top-level boxes. They are
JPEG 2000 signature box
File Type box
JP2 Header box
Contiguous Codestream box
Each box is preceded by 4 byte marker and 4 byte size value. So in MATLAB it should be something like this
fname='C:\Users\admin\Documents\MATLAB\SO\Jpeg2k\balloon.jp2';
fid = fopen(fname);
headerMark = uint8('jp2h');
matchCnt = 1;
ch = fread(fid,1,'*uint8');
matchCnt = matchCnt+isequal(headerMark(1),ch);
while matchCnt < 5 && ~feof(fid)
ch = fread(fid,1,'*uint8');
matchCnt = matchCnt+isequal(headerMark(matchCnt),ch);
end
if matchCnt == 5
fseek(fid,ftell(fid)-8,'bof');
sizeBytes = fread(fid,4,'*uint8');
sizeVal = arrayfun(#(x,y) bitshift(x,y,32), uint32(sizeBytes), [3:-1:0]');
sizeVal = bitor(bitor(bitor(sizeVal(1),sizeVal(2)),sizeVal(3)),sizeVal(4));
end
fclose(fid);
I don't know what is you final task (getting header size seems to be halfway) but I recommend to see the quickstart guide for JPEG2000, JPEG2000 validator (written in python) and the validator overview.

Related

read and show raw depth image in matlab

I have a set of .raw depth images. The image format is 500X290 with 32 bytes per pixel. When I open them with IrfanView image viewer I see the depth image correctly like this:
displayed image in IrfanView
Now I want to read and display the same depth image in Matlab. I do like this:
FID=fopen('depthImage.raw','r');
DepthImage = fread(FID,[290,500],'bit32');
fclose(FID);
colormap winter;
imshow(DepthImage);
DepthImage is a 290X500 type double matrix.
what I get from this code is this image:
displayed image in Matlab viewer
when I change fread parameter from 'bit32' to 'bit24' I get this:
displayed image in Matlab with bit24
I guess each element in DepthImage contains 32 bits where each 8 bits corresponds to R,G,B and D values. but how can I read the image correctly and display it like the one in IrfanView?
the raw file: https://drive.google.com/file/d/1aHcRmMKvi5gtodahR5l_Dx8SbK_920c5/view?usp=sharing
There might be an issue with image metadata header, like "date and time of the shot", "camera type". Open your image with notepad++ to check for "date and time". If you upload your original raw image, it will be easier to try things.
Upd: Ok, this is something. Check if it helps
FID=fopen('camera00000000000014167000.raw','r');
DepthImage = fread(FID,290*500*4,'int8');
DepthImageR = DepthImage(1:4:end);
DepthImageG = DepthImage(2:4:end);
DepthImageB = DepthImage(3:4:end);
DepthImageD = DepthImage(4:4:end);
dataR = reshape(DepthImageR, 500,290);
dataG = reshape(DepthImageG, 500,290);
dataB = reshape(DepthImageB, 500,290);
dataD = reshape(DepthImageD, 500,290); % all equal to 64 - useless
figure()
subplot(2,2,1)
image(dataR)
subplot(2,2,2)
image(dataG)
subplot(2,2,3)
image(dataB)
subplot(2,2,4)
data = zeros(500,290,3);
data(:,:,1) = dataR;
data(:,:,2) = dataG;
data(:,:,3) = dataB;
image(data)

After cropping and writing in png file the size of some images has changed,why?

My images are in mat file after I cropped them with a particular size 256x256, the size was correct before writing in png file. I saved them in png format. Now, I wanted to process them and realized that the sizes are not same, what is the reason? And how can I solve it? Thanks
This the code:
[m1,m2] = size(II);
i1_start = floor((m1)/2)-floor(n/2); % or round instead of floor; using neither gives warning
i1_stop = i1_start + n;
i2_start = floor((m2)/2)-floor(n/2);
i2_stop = i2_start + n;
B = II(i1_start+1:i1_stop, i2_start+1:i2_stop);
figure ,imshow(B,[]);
and this is the code for png conversion:
outfile = sprintf(png_filename)
data=uint8(data);
imwrite(data,outfile,'png','BitDepth' ,8);
Use imwrite function
A = rand(50);
imwrite(A,'myGray.png')

Transmission of the image using BPSK

Input image needs to be modulated and transmitted and finally detected using BPSK.
M = 2; %Modulation order 2 for BPSK
randn('state',200); % initializing the randn() function
imdata = imread('lenna.pgm');
bdata = de2bi(imdata);
sizec = size(bdata,1); % height
sizer = size(bdata,2); % width
nbits = sizec*sizer; % number of bits in the image
msg = double(reshape(bdata,nbits,1));
The following code is meant to modulate the input image and display the demodulated output.The resultant output,when i run the code,I get a blank image.Need help in figuring out what I have missed or where I have gone wrong.
%modulate
txpsk = pskmod(msg,M);
%noise
phasenoise = randn(nbits,1)*0.015;
rxpsk = txpsk.*exp(2i*pi*phasenoise);
%demodulate
recovpsk = pskdemod(rxpsk,M);
reshape1rxpsk = reshape(recovpsk,sizec,sizer);
reshape2rxpsk = bi2de(reshape1rxpsk);
finalout= reshape(reshape2rxpsk,size(imdata,1),size(imdata,2));
imshow(finalout)
A PGM file consists of a sequence of one or more PGM images. Each image has a header structure, containing such information, as width, height e.t.c. After modulation you add a noise to your signal. Therefore, the demodulated signal may contain errors. If this errors corrupts header information of image - this may lead to blank image output. Try to comment lines
%phasenoise = randn(nbits,1)*0.015;
%rxpsk = txpsk.*exp(2i*pi*phasenoise);

Read img medical image without header in matlab

I have a radiograph .img file without the header file. However, the researchers who have published the file have given this information about it
High resolution (2048 x 2048 matrix size, 0.175mm pixel size)
Wide density range (12bit, 4096 gray scale)
Universal image format (no header, big-endian raw data)
Using this information, I tried fread command in Matlab to read the image into Matlab.
fid = fopen('image.img','r','B');
oneSlice = fread(fid, [2048 2048], '*uint8','B');
imshow(oneSlice)
However the resulting image is coming up as incorrect. Is there something that I am doing wrong ? Could someone suggest any different method to read this image file ?
The lung x-rays of the JSRT database (www.jsrt.or.jp/jsrt-db/eng.php), have that format. I tested this code with them and it works:
fid = fopen('image.img','r','b');
oneSlice = fread(fid, [2048 2048], '*uint16','b');
img = mat2gray(oneSlice, [0,4096]);
fclose(fid);
%%% Read image
fid = fopen('image.img','r','b');
oneSlice = fread(fid, [2048 2048], '*uint16','b');
img = mat2gray(oneSlice, [0,4096]);
fclose(fid);
%%%rotate image
imgR = imrotate(img,270);
%%%horizontal flip image
imgRF = flipdim(TestImgR ,2);

reading and writing mulltiple images automatically in matlab?

i have 250 images stored at
E:\HandVein_DataSet
these 250 images belong to 50 person , this mean each one has 5 images
these pictures are arranged as following
0001hv1-0001hv2-0001hv3-0001hv4-0001hv5 ....0002hv1-0002hv2-0002hv3-0002hv4-0002hv5 until 0050hv1-0050hv2-0050hv3-0050hv4-0050hv5
I want to read all these images and processing them according my steps and then saved the resultant images after preproceesing in a specific file,for example at E:\final result.
How can i do this in matlab?
I'm assuming that the filenames are, e.g., 0001hv1.jpg (you can change the extension as necessary). You don't say what the output is; I'm assuming it's a modified version of the input image and that you want to use the same naming scheme.
in_dir = 'E:/Hand/Vein_DataSet';
out_dir = 'E:/final\ result';
for px = 1 : 50,
for hx = 1 : 5,
fname = sprintf('%04dhv%d.jpg', px, hx);
current_image = imread(fullfile(in_dir, fname));
% do processing
out_image = some_function(current_image);
% save output
imwrite(out_image, fullfile(out_dir, fname));
end
end