Matlab rgb2hsv dimensions - matlab

I am reading in the matlab documentation that rgb2hsv will return an m-by-n-by-3 image array, yet when I call it, I get a 1-by-3 vector. Am I misunderstanding something?
Here is an sample code:
image_hsv = rgb2hsv('filepath')
and as output
image_hsv =
0.7108 0.3696 92.0000

You cannot call rgb2hsv on a filepath - it must be called on a MATLAB image matrix. Try:
image_rgb = imread('filepath'); % load the image array to MATLAB workspace
image_hsv = rgb2hsv(image_rgb); % convert this array to hsv
You can see these matrices with:
>> whos image* % display all variables whose name begins with 'image'
Name Size Bytes Class Attributes
image_hsv 480x640x3 7372800 double
image_rgb 480x640x3 921600 uint8
What your original code was doing was converting your filepath string to ascii numbers, taking the first three values of this array as RGB values and converting these to HSV.
NOTE: This example highlights dangers with MATLAB's weak typing system, where data types are silently converted from one type to another. Also maybe a lack of correct input checking to the rgb2hsv function.

Related

Making a copy of an image via a Loop not working

Idk why the code won't work. All it does is give me a blank white image. And if you don't declare a matrix before by zeros(x, y) then it works fine. Wth is wrong here?
I tried not declaring the zeros matrix before and only that works. I even tried doing img2(i,j) = img2(i,j)+img1(i,j)
function [imgOut] = scaleLoopBased(img,s)
%UNTITLED4 Summary of this function goes here
% creating a zero matrix of the given scale
[rows,columns]=size(img);
imgTemp=zeros(rows, columns);
for i=1:rows
for j=1:columns
imgTemp(i, j) = img(i, j);
end
end
imshow(imgTemp);
imgOut = imgTemp;
end
Blank white image
This is a result of your new image (of type double, what zeros creates by default) not having the same type as your original image (typically type uint8). You can fix this by initializing your new image to have the same data type as the original using the class function and passing an additional argument to zeros:
imgTemp = zeros(rows, columns, class(img));
The reason it works correctly when you don't initialize imgTemp is because MATLAB initializes the variable for you using the data type of img by default when you perform the first indexed assignment.
The imshow utility expects one of the standard image types in MATLAB. An image of type double is expected to have values spanning the range [0 1], while images of type uint8 will have values in the range [0 255]. In your example you likely have a matrix imgTemp that is of type double but has values spanning [0 255]. Another way to fix your issue is to explicitly tell imshow what value range to use for the display (since the default [0 1] doesn't work):
imshow(imgTemp, [0 255]);
Always be aware of the data type when manipulating or processing images. You may need to scale or convert back and forth, using a double type for calculations (since integers saturate) and a uint8 type for display and reading/writing to files.

collapse cell array to text matlab

it's a basic question I guess (but I'm new to Matlab), but given:
>> class(motifIndexAfterThresholds)
ans =
'double'
with :
16
8037
14340
21091
27903
34082
as the contents of that variable
I hoped to print to the same line on the matlab console the contents of that variable and some other output:
fprintf('With threshold set to %d, %d motifs found at positions %f.\n',threshold,length(motifIndexAfterThresholds), motifIndexAfterThresholds);
When I do this however, I'm getting more than one line of output:
With threshold set to 800, 6 motifs found at positions 16.000000.
With threshold set to 8037, 14340 motifs found at positions 21091.000000.
With threshold set to 27903, 34082 motifs found at positions
Can someone share the method for collapsing this double array to a single line of text that I can display on the Matlab console please?
What you need is the num2str function that is builtin MATLAB. Modify your code as below:
strThresholds = num2str(motifIndexAfterThresholds.', '%f, '); % Transpose used here since you need to make sure that motifIndexAfterThresholds is a row vector
fprintf('With threshold set to %d, %d motifs found at positions %s.\n',threshold,length(motifIndexAfterThresholds), strThresholds);
The num2str function will convert your vector to a string with the specified format. So for your given example,
strThresholds = '16.000000, 8037.000000, 14340.000000, 21091.000000, 27903.000000, 34082.000000,'
You could definitely edit the format string used in the num2str funcion to suit your needs. I would suggest using %d since you have integers in your vector

Convert a Matrix of doubles to a grayscale .tif image

I have an nCol by nRow matrix of doubles that I want to convert into an nCol by nRow grayscale images of pixels. I don't want to cut this down to an image scaled to 256 channels. I'd be happy with using singles instead of doubles. I've looked through the Tiff class documentation from Mathworks but can't find a simple example for this.
I tried to read about Tiff class and yep it is very poor documented. But I found this going through trial and error:
lets say we have matrix
data = rand(100,200);
Lets save it as Tiff:
t = Tiff('new.tif','w') %create object of Tiff class
setTag(t,Tiff.TagID.ImageLength,size(data,1)) %define image dimentions
setTag(t,Tiff.TagID.ImageWidth,size(data,2))
setTag(t,'Photometric', Tiff.Photometric.MinIsBlack) %define the color type of image
%specifies how image data components are stored on disk
setTag(t,'PlanarConfiguration',Tiff.PlanarConfiguration.Chunky);
setTag(t,'BitsPerSample',64); %because 1 double = 8byte = 64bits
%Specify how to interpret each pixel sample (IEEEFP works with input doubles)
setTag(t,'SampleFormat',Tiff.SampleFormat.IEEEFP);
t.write(data)
t.close
Lets test this now:
datac = imread('new.tif')
whos datac
Name Size Bytes Class Attributes
datac 100x200 160000 double
And:
datac(:,1)
ans =
0.4921
0.6908
0.1544
0.4433
...

Checking size of matrix results in error

Error:
Subscript indices must either be real positive integers or logicals.
Hi I am doing image conversion and I get an error when checking the size of the matrix. I am confused as to why I am getting it for this particular instance with the code:
size(maleGrey)
Here is the code I am running:
male = getAllFiles('male');
% Variable Initialization
size = 250*250;
numM = length(male);
maleGrey = zeros(size,numM);
% Convert to gray scale
for i = 1:numM
rgb = imread(char(male(i)));
img = single(rgb2gray(rgb));
vec = img(:); % make it a vector of (62500,1) in size
maleGrey(:,i) = vec;
end
You made the mistake of using size as a variable when calling size=250*250. Once you do that in a workspace, the function is masked (overloaded) by the variable, and Matlab will always treat further calls to size as manipulations of the variable.
Call clear size, and the function will work as intended. Also, do not use size as name for a variable (or other function names like length or double, or zeros etc), but e.g. siz, or numberOfRows (because that's what your variable stands for).
You're overwriting (locally) Matlabs native function reference size by a variable name
% Variable Initialization
size = 250*250; % <--
Hence when you call size(maleGrey) it treats maleGrey as an index in the variable size.

Matlab mat to libsvm format (strange fprint behaviour)

I'm trying to write my own implementation of mat2libsvm format converter(I don't want to use original function because it want double mat for input, but I working with images and have uint8 matrices).
So here is example that I don't understand:
a= zeros(2,256);
a(1,256)=1;
formatSpec = '%i:%d ';
row= a(1,:);id=find(row);fprintf(formatSpec,[id ; row(id)]);
>256:1
row= uint8(a(1,:));id=find(row);fprintf(formatSpec,[id ; row(id)]);
>255:1
why it cuts off to 255? anyway id in 1st and 2nd examples is double.
In the second line you are concatenating an uint8 with an double, which casts both to uint8. Minimal example:
[256;uint8(1)]
To solve this, use fprintf with multiple input arguments:
fprintf(formatSpec,id , row(id));