MATLAB - image huffman encoding - image

I have a homework in which i have to convert some images to grayscale and compress them using huffman encoding. I converted them to grayscale and then i tried to compress them but i get an error. I used the code i found here.
Here is the code i'm using:
A=imread('Gray\36.png');
[symbols,p]=hist(A,unique(A))
p=p/sum(p)
[dict,avglen]=huffmandict(symbols,p)
comp=huffmanenco(A,dict)
This is the error i get. It occurs at the second line.
Error using eps
Class must be 'single' or 'double'.
Error in hist (line 90)
bins = xx + eps(xx);
What am i doing wrong?
Thanks.
P.S. how can i find the compression ratio for each image?

The problem is that when you specify the bin locations (the second input argument of 'hist'), they need to be single or double. The vector A itself does not, though. That's nice because sometimes you don't want to convert your whole dataset from an integer type to floating precision. This will fix your code:
[symbols,p]=hist(A,double(unique(A)))
Click here to see this issue is discussed more in detail.

first, try :
whos A
Seems like its type must be single or double. If not, just do A = double(A) after the imread line. Should work that way, however I'm surprised hist is not doing the conversion...
[EDIT] I have just tested it, and I am right, hist won't work in uint8, but it's okay as soon as I convert my image to double.

Related

How to format a number with XPath in Tibco BW 5

I've managed to format the following lines in XPath, from this format:
1000.50
30
to this:
100050
3000
The solution I've adopted is:
concat(substring-before([number], '.'), substring-after([number], '.'))
If the . is not present I directly multiply the number by 100.
I'm wondering if there is any better way to do that. My second thought was using Java.
What goes wrong if you just multiply by 100? So long as the result of multiplying by 100 is an exact integer, it should be formatted without a "." when converted to a string. If there are rounding errors that mean the result is not an exact integer, you might want to use round().
The concat() approach seems fragile to me: what if someone gives you input like 1000.5 or perhaps 1000.500?

MATLAB ConnectedComponentLabeler does not work in for loop

I am trying to get a set of binary images' eccentricity and solidity values using the regionprops function. I obtain the label matrix using the vision.ConnectedComponentLabeler function.
This is the code I have so far:
files = getFiles('images');
ecc = zeros(length(files)); %eccentricity values
sol = zeros(length(files)); %solidity values
ccl = vision.ConnectedComponentLabeler;
for i=1:length(files)
I = imread(files{i});
[L NUM] = step(ccl, I);
for j=1:NUM
L = changem(L==j, 1, j); %*
end
stats = regionprops(L, 'all');
ecc(i) = stats.Eccentricity;
sol(i) = stats.Solidity;
end
However, when I run this, I get an error says indicating the line marked with *:
Error using ConnectedComponentLabeler/step
Variable-size input signals are not supported when the OutputDataType property is set to 'Automatic'.'
I do not understand what MATLAB is talking about and I do not have any idea about how to get rid of it.
Edit
I have returned back to bwlabel function and have no problems now.
The error is a bit hard to understand, but I can explain what exactly it means. When you use the CVST Connected Components Labeller, it assumes that all of your images that you're going to use with the function are all the same size. That error happens because it looks like the images aren't... hence the notion about "Variable-size input signals".
The "Automatic" property means that the output data type of the images are automatic, meaning that you don't have to worry about whether the data type of the output is uint8, uint16, etc. If you want to remove this error, you need to manually set the output data type of the images produced by this labeller, or the OutputDataType property to be static. Hopefully, the images in the directory you're reading are all the same data type, so override this field to be a data type that this function accepts. The available types are uint8, uint16 and uint32. Therefore, assuming your images were uint8 for example, do this before you run your loop:
ccl = vision.ConnectedComponentLabeler;
ccl.OutputDataType = 'uint8';
Now run your code, and it should work. Bear in mind that the input needs to be logical for this to have any meaningful output.
Minor comment
Why are you using the CVST Connected Component Labeller when the Image Processing Toolbox bwlabel function works exactly the same way? As you are using regionprops, you have access to the Image Processing Toolbox, so this should be available to you. It's much simpler to use and requires no setup: http://www.mathworks.com/help/images/ref/bwlabel.html

Reading RAW16 images in MATLAB

I am trying to read a RAW16 image in MATLAB. After going through another question here on StackOverflow, I figured I could read it like reading a file and then doing some simple matrix transposes. However, I am running into a weird problem. The image below is what I am getting. I do not understand why this overlap exists and am not entirely sure how to solve the issue. Could someone help?
Code:
fin = fopen('raw13.raw','r');
ima = fread(fin, [col*2 row],'uint8');
temp = zeros(col,row);
j=1;
for i=1:2:col*2-1
temp(j,:) = ima(i,:) + ima(i+1,:)*2^8; %The first element is the lower 8bits and the second element is the higher 8bits
j = j+1;
end
imshow(temp',[0 2^16-1])
In case anyone is having the same problem as me.
It seems the .RAW file that I obtained was somehow corrupted. Using a lower version of the FlyCapture program resulted in a better RAW file and the code that I used worked like a charm
I use col*3 in line 3 and line 5, then it displays the image well.
but i use 8 bit raw image form pointgray camera,and i dont know 'imshow(temp',[0 2^16-1])' would work...

Opencv cvSaveImage

I am trying to save an image using opencv cvSaveImage function. The problem is that I am performing a DCT on the image and then changing the coefficients that are obtained after performing the DCT, after that I am performing an inverse DCT to get back the pixel values. But this time I get the pixel values in Decimals(e.g. 254.34576). So when I save this using cvSaveImage function it discards all the values after decimals(e.g. saving 254.34576 as 254) and saves the image. Due to this my result gets affected. Please Help
"The function cvSaveImage saves the image to the specified file. The image format is chosen depending on the filename extension, see cvLoadImage. Only 8-bit single-channel or 3-channel (with 'BGR' channel order) images can be saved using this function. If the format, depth or channel order is different, use cvCvtScale and cvCvtColor to convert it before saving, or use universal cvSave to save the image to XML or YAML format."
I'd suggest investigating the cvSave function.
HOWEVER, a much easier way is to just write your own save/load functions, this would be very easy:
f = fopen("image.dat","wb");
fprintf(f,"%d%d",width,height);
for (y=0 to height)
for (x=0 to width)
fprintf(f,"%f",pixelAt(x,y));
And a corresponding mirror function for reading.
P.S. Early morning and I can't remember for the life of me if fprintf works with binary files. But you get the idea. You could use fwrite() instead.

Load satellite image in Matlab

I've a problem about how can I load a Landsat image on Matlab. My image is in format .img and have the following information:
Columns and rows= 9487 x 8543
Number of bands= 6
Cellsize= 25 x 25
Source Type= continuous
PĂ­xel type= unsigned integer
Pixel Depth= 16 bit
Scale factor= 0,9996
And this is my code:
IM= multibandread('2000.img',[9487, 8543,
6],'int16',0,'ieee-le',{'Row','Range',[9487 8543]);
But there's the following error:
Error: Unbalanced or unexpected parenthesis or bracket.
I've tried to change but it doesn't work. What can I do?
Thanks in advance,
Emma
Dennis is right, you're missing a closing curly brace. It should be inserted between the bracket and the parenthesis at the end like this:
IM= multibandread('2000.img',[9487, 8543, 6],'int16',0,'ieee-le',{'Row','Range',[9487 8543]});
I think you may want to leave the subset argument out completely, this in addition to using unsigned uint16, try the following:
multibandread('2000.img',[9487, 8543, 6],'uint16',0,'ieee-le')
Note that with your current call it appears that you are trying to extract row 9487 to 8543.

Resources