Matlab-how can I use SSIM function using sprintf function - matlab

Now I'm researching on Image processing about interpolation. So I invented my new interpolation algorithms about image.
And I have to check SSIM value about my images that are interpolated by my interpolation algorithms.
Now I'm using ICY tool (On windows, GUI environment) but this tool is too hard to check large amount of images because of GUI interface.
So I want to check image SSIM value using matlab ssim function.
but I'm not a expert in matlab language, so I'm in trouble in dealing with import various name to ssim function.
This is what I want to do:
I want to checks ssim value with a large amount of images.
So I'm going to use sprintf function to make a valuable image name.
The third I want to import this input image name to ssim function.
This is the code I used.
for n= 1 : 10
str = sprintf('./x2/cutted/x2_fn%d_4p_3p_cutted.bmp',n);
str_ori = sprintf('ori_%d.bmp',n);
img_cutted=imread(str);
img_ori=(str_ori);
[ssimval,ssimmap]=ssim(str,str_ori);
end
When I use this code, the error is invoked.
Error: The str must be the types of uint8, uint16, int16, single, double.
but you used char value.
After I got this message, write "uint8 str;" but still the message is invoked.

ssim gets 2 images as input, not 2 strings. replace your code by [ssimval,ssimmap]=ssim(img,img_ori);

Related

Using a variable-sized argument in Matlab coder

I want to generate a c++ code for DCT function using Matlab coder. I wrote this simple function and tried to convert it to c++.
function output_signal = my_dct(input_signal)
output_signal = dct(input_signal);
end
When I use a fixed size type for the input argument (such as double 1x64), there is no problem; however, a variable-sized type (such as double 1x:64) for the input argument results in these errors:
The preceding error is caused by: Non-constant expression..
The input to coder.const cannot be reduced to a constant.
Can anyone please help me?
Thanks in advance.
The documentation is a bit vague for DCT in Coder, but it implies that the input size must be a constant power of 2 along the dimension of the transform. From DCT help:
C/C++ Code Generation
Generate C and C++ code using MATLAB® Coder™.
Usage notes and limitations:
C and C++ code generation for dct requires DSP System Toolbox™ software.
The length of the transform dimension must be a power of two. If specified, the pad or truncation value must be constant. Expressions or variables are allowed if their values do not change.
It doesn't directly say that the length of the variable (at least along the dimension being transformed) going into the dct function must be a constant, but given how coder works, it really probably has to be. Since that's the error it's returning, it appears that's a limitation.
You could always modify your calling function to zero pad to a known maximum length, thus making the length constant.
For instance, something like this might work:
function output_signal = my_dct(input_signal)
maxlength = 64;
tinput = zeros(1,maxlength);
tinput(1:min(end, numel(input_signal))) = input_signal(1:min(end, maxlength));
output_signal = dct(tinput);
end
That code will cause tinput to always have a size of 1 by 64 elements. Of course, the output will also always be 64 elements long also, which means it'll be scaled and may have a difference frequency scale than you're expecting.

Make Matlab issue a warning when converting a double to a uint8 and vice versa?

Typically in Matlab colours are represented by three element vectors of RGB intensity values, with precision uint8 (range 0 - 255) or double (range 0 - 1). Matlabs functions such as imshow work with either representation making both easy to use in a program.
It is equally easy however to introduce a bug when assigning colour values from a matrix of one type, to that of another (because the value is converted silently, but not re-scaled to the new range). Having just spent a number of hours finding such a bug, I would like to make sure it is never introduced again.
How do I make Matlab display a warning when type conversion takes place?
Ideally it would only be when the conversion is between double and uint8. It should also be difficult to deactivate (i.e. the option is not reset when loading a workspace, or when matlab crashes).
A possible solution is to define your own uint8 function that casts to uint8 and issues a warning if some value has been truncated.
You should place this function in a folder where it shadows the builtin uint8 funciton. For example, your user folder is a good choice, as it usually appears the first in path.
Or, as noted by Sam Roberts, if you want this function to be called only when converting from double into uint8 (not when converting from any other type into uint8), put it in a folder named #double within your path.
function y = uint8(x)
y = builtin('uint8', x);
if any(x(:)>255) || any(x(:)<0)
warning('MATLAB:castTruncation', 'Values truncated during conversion to uint8')
end
The warning is on by default. You can switch it on or off with the commands warning('on','MATLAB:castTruncation') and warning('off','MATLAB:castTruncation') (thanks to CitizenInsane for the suggestion).

How do I use imhistmatch in Matlab?

I'm trying to implement with the matlab function imhistmatch.
So I use following commends.
A= imread('example1.jpg')
B= imread('example2.jpg')
C= imhistmatch(A,B)
But there is problem like this.
?? Undefined functionor method 'imhistmatch' for input arguments of type 'uint8'
So What am I suppose to do for solve?
I am not sure about your question. Are you trying to implement imhistmatch?
Then probably your function is in the wrong folder or you have a typo in the functions name.
use
addpath('Pathto/imhistmatch')
to ensure that matlab can find your implemented function.
But maybe you want to use the function imhistmatch provided by the image processing toolbox. Then, I guess you don't have the image processing toolbox.
Try
ver
and check the output. You should find these lines:
MATLAB Version X.X (XXXXX)
Image Processing Toolbox Version X.X (XXXXX)
If not, then you may want to buy the image processing toolbox, get a evaluation version or maybe try octave.
On my computer the following works:
imhistmatch(ones(10),ones(10))
imhistmatch(ones(10,'uint8'),ones(10,'uint8'))
but when I type
imhistmatch(ones(10,'int8'),ones(10,'int8'))
I get an error:
Error using imhistmatch
Expected input number 1, A, to be one of these types:
uint8, uint16, double, int16, single
Instead its type was int8.
...
So, if it would be a type problem you would see another error.
Your error is the result of not having the proper function imhistmatch.

'exact' numerical value after numerical optimization MATLAB

I'm having the following issue with my code. I've been trying to use some other posts that I found on line, like this one. But they didn't what I'm looking for.
My code uses a MATLAB Exchange function which optimize a numerical value that is important to be with 32 digits after the dot such as
0.59329669191989231613604260928696
The optimization function can be found here and it is called fminsearchbnd
The optimization function calculate this and store the value in a variable that I use all over my code. In order not to perform the optimization everytime I want to store the variable (I tried either on a *.mat and on a label in the string form.
But when I retrieve it, MATLAB transforms it in a double precision variable 'cutting' all the numbers after the 14th. However I need all of them because they are important!
Is it possible to read a number like that w/o using vpa() because with a symbolic value I can't do anything.
Any help is really appreciated.
Thanks
EDIT:
fminsearchbnd gives me this class(bb) -> double and when I want to see it on the workspace it is 0.586675392365899. But when I set formatSpec = '%.32f\n'; because I want to see all the numbers that the optimization gives me, typing set(editLabel,'String',num2str(bb,formatSpec))
You're trying to store/use a number that cannot be represented exactly in an IEEE754 64-bit double-precision floating point number.
I'm not sure how you got that number without using vpa() in the first place, since 64-bit double is Matlab's maximum of precision...
You could use the multiple precision toolbox by Ben Barrowes, or HPF by John d'Errico, also from the FEX. You'll have to convert/construct to/from string if you want to store/load it to/from file.
But I have to agree with John's comment there:
The fact is, most of the time, if you can't do it with a double, you
are doing something wrong
so...why exactly are those 32-or-more digits important?

Using fwrite in MATLAB to graph constant double values in Simulink Scope blocks?

I am currently trying to use UDP to send constant values from MATLAB to Simulink and then use Simulink to graph these values. Here is an image of the MATLAB code, Simulink block diagram and the graph that I am working with (note that the graph starts at a constant value of 0):
http://i.imgur.com/krkulyQ.png
In the MATLAB code I am using the fwrite function to send data to the Packet Input block which then outputs to the Scope block to graph the data. Specifically I would like to graph fractions and that is why I chose to use type double. When I run the MATLAB code the graph should jump from 0 to 50.5 as it is running in real time. However, it stays at 0.
I think there may be some problems with settings I used for the Packet Input Simulink block. Here are the settings:
http://i.imgur.com/Chc0qk8.png
I have tried this with type int8 and the graph changes according to what I input in the fwrite function. As you can see in the following image I graphed the value 50 using fwrite in int8:
http://i.imgur.com/Xzytfka.png
Here, I made changes to the "Input Packet size" and "Block output data types" since I am using int8 as shown in this image:
http://i.imgur.com/DUJ4eKB.png
If anyone knows what the problem is please let me know, thanks!