MATLAB/Opencv: Converting 24 bit RGB image to 16 bit gray - matlab

Do we have functions in OpenCV or in MATLAB to convert a 24 bit RGB image to 16 bit gray image. I tried CvConvertColor but it is of no use here.
(I can convert to grayscale using cvtColor(src, bwsrc, CV_RGB2GRAY), more help needed, but I want to make it 16 bit from 32 bit.)

I beleive there is the rgb2gray operator in matlab.
Here is a link to where you can read up on it from mathworks site:
http://www.mathworks.com/help/toolbox/images/ref/rgb2gray.html
This even tells you how to read in the image. Most of the code you should need is there!!!
I = imread('board.tif');
J = rgb2gray(I);
figure, imshow(I), figure, imshow(J);

Have you tried OpenCV's cvtcolor? I'm not sure which opencv version you are using but CvConvertColor no longer exists, you should probably set up the latest version.
cvtcolor:
http://docs.opencv.org/modules/imgproc/doc/miscellaneous_transformations.html#cvtcolor
cvtColor itself has no further arguments, it is smart enough to check the bitness of source and destiny matrix. Opencv matrix objects, Mat objects, have a custom opencv type that holds their bitness, number of color channels and primitive type (details: http://docs.opencv.org/modules/core/doc/basic_structures.html#datatype ).
If you want to make sure if the resulting matrix is 16 bit, you can check the Mat type. If the matrixes are not in the bitness you require, opencv has other converter for that: http://docs.opencv.org/modules/core/doc/basic_structures.html?highlight=convert#mat-convertto

Related

Matlab - Read and show the tif image

Please checkout the below link which directs an image (tif). It has 4 channels.
http://amarfree.com/Kannada_1_001.tif
Please help me in reading the image and convert it to b/w image using matlab.
Kindly furnish the image channels information.
The fourth channel of your image is the alpha channel. It contains the transparency value of each pixel. In this example, all the values in the 4th channel are identical to 255:
sum(sum(I(:,:,4)~=255))
ans =
0
The general solution in this specific case is to ignore the last channel(since it doesn't add information at all), and use rgb2gray function:
I = imread('<image path>');
bw = rgb2gray(I(:,:,1:3));
However, in your case you don't have rgb2gray function (probably because you don't have Matlab's Image Processing Toolbox). The general solution for this case is to implement a function which converts rgb images into bw images.
Luckily, in this particular example, the 3 channels are completely identical:
isequal(I(:,:,1),I(:,:,2))
ans =
1
isequal(I(:,:,1),I(:,:,3))
ans =
1
Therefore, you can simply write:
bw = I(:,:,1);
Notice that it will only work for this particular case.

What exactly is returned from PCA in MatLab?

I = double(image1Cropped);
X = reshape(I,size(I,1)*size(I,2),3 );
coeff1 = pca(X);
What exactly is happening in the above 3 lines of code?
Why covert an image into double before passing into reshape?
What is the purpose of reshape?
What is returned from pca(X)?
Could I use coeff1 to compare images (for example, comparing faces)?
From PCA, the principal conponents are returned. Of course.
Check the documentation or any online course to understand what a PCA is.
As PCA is a mathematical tool, it needs floating point data to work, that's why there is a double in the first line, it is converting the data (most likely uint8) into floating point data.
reshape is reshaping your image to a huge matrix of size(I,1)*size(I,2),3, so every X(ii,:) will be 3 of length.
My guess here is that the image is a RGB image, and that this code tries to get the "principal colours" of the image. What the code does is transform you data to points of 3 values, Red, Green and Blue (as opposed to the normal XYZ) and then getting the principal components of the image. The principal components will be "the principal 3 Colors (conbinations of RGB)" that are in the image.
If you search "PCA of an RGB image" on Google you will find lots of information of how/why to do this.

My observations are less than the feature vector of each. Any solution to overcome this?

I'm using GMM to fit my data to 256 Gaussians. I'm using Matlab's fitgmdist to achieve this.
gmm{i} = fitgmdist(model_feats, gaussians, 'Options',statset('MaxIter',1000), ...
'CovType','diagonal', 'SharedCov',false, 'Regularize',0.01, 'Start',cInd);
I am using RootSIFT to extract the features of each image. This produces a vector of 1x128 for each image.
Now I have 45 images maximum for each writer. So after feature extraction and everything the size of my model_feats is 45 x 128.
According to the help file for data arrangement for X is:
The rows of X correspond to observations, and columns correspond to
variables.
The problem I have is when I run the above function I am told that:
"X must have more rows than columns."
I have a total of 45 images for each writer. How will I be able to get this function to run? This is a strange limitation for such a function, I mean even if I am able to get 100 images for each writer it will not work.
I will appreciate any workaround to this.
P.S. I've tried the same with VL_Feat's vl_gmm and it works without any problems but I need this to work in Matlab not VL_FEAT.
With SIFT you usually don't compute the feature for the whole image, but literally hundreds of keypoints for each image. Then you won't have this problem anymore.
Next step then probably is a “bag of visual words” mapping of each image.

face detection for matlab

i searched face detection for matlab to my project.
i found one:
http://people.kyb.tuebingen.mpg.de/kienzle/fdlib/fdlib.htm
i downloaded the source code, but it didn't work, i got that error from matlab:
??? Undefined function or method 'fdmex' for input arguments of type
'uint8'.
Error in ==> tinytest at 10 s = fdmex(x', threshold);
the main script is:
x = imread('geeks.jpg');
% decision threshold.
% change this to a smaller value, if too many false detections occur.
% change it to a larger value, if faces are not recognized.
% a reasonable range is -10 ... 10.
threshold = 0;
imagesc(x); hold on; colormap gray;
s = fdmex(x', threshold);
for i=1:size(s,1)
h = rectangle('Position',[s(i,1)-s(i,3)/2,s(i,2)-s(i,3)/2,s(i,3),s(i,3)], ...
'EdgeColor', [1,0,0], 'linewidth', 2);
end
axis equal;
axis off
can you find the error?
Usually when I see a uint8 error and a grayscale image, its a red flag to me that I need to do
colorImg=imread('imageName.jpg')
% Even if the image is grayscale, if its png or jpg,
% it will load in as a color image almost exclusively
img=rgb2gray(colorImg)
If you look at the img output, you will notice now its of type double instead of uint8 :)
If that doesn't work, hopefully macduffs will, mine just seems easier if that actually does fix it. :)
Depending on your version of matlab, it looks like the fdlib, comes with a .dll, rename it to .mexw32 or whatever your host machine desires. You can determine this by running:
>> mexext
mexw32
on the Matlab command prompt. Use the mex extension and rename the fdmex.dll to fdmex.mexw32, or whatever mexext returns and it should run flawlessly.
If I run in on my Windows XP machine, I get that beautiful picure:
However, if you do not have a 32 bit machine, the author of the software writes on the link in the question:
Please note that all builds were optimized for Intel Pentium CPUs. If
you would like to run it on a different platform, or have any other
questions, please let me know.
He has a link to his profile and email, so I recommend contacting him for a 64 bit version of the executable.
If you have a recent version of Matlab with the Computer Vision System Toolbox installed, you can use vision.CascadeObjectDetector system object to detect faces in images.

steganography in matlab

I'm working on steganography in binary image.but I have a problem in implementation in matlab.
I want to konw that,
How can I hide a message in a binary image?
And,
How can I do it in matlab with m-file or simulation?
Steganography can be done in so many different ways that you will have to define what exactly do you need if you want something specific.
In the mean time here is an example:
Encoding:
take an image and make each pixel uses only 23 out of 24 bits for colors (for example set lowest bit to 0 on the value of red color)
this will give you W x H bits for your message
put your message bit by bit into cleared pixels
Decoding
extract only bits from the lowest bit red component in the order they were put in and... that's it.
There was recently a similar question on SO with great answer from #Jacob with code example.
You can also have a look at LSB Based Steganography article on Advanced Matlab website.
Try to check image Steganography using LSB then you will be knowing how to implement. I have try that for my project.
[fn, pn, FI] = uigetfile('*.jpg*','Select the Cover Image','multiselect','on');
coln=size(fn,2);
numberfile=coln;
for i=1:numberfile
fn(i);
entirefile=fullfile(pn,fn{i});
fid=fopen(entirefile);
fclose(fid);
end
I = imread([pn,fn{i}]);
fprintf('Cover Image Loaded ... \n\n')
[fn, pn] = uigetfile('*.txt','Select Text File')
This code is just for example how to implement, i use to select multiple frames, you use according to your need.