How to convert binary data into an image in matlab - matlab

I'm using the following code to convert an image into binary form.
% lets suppose that the name of image file is caption with an extension .jpg
X=imread('caption.jpg');
g=reshape((dec2bin(typecast(X(:),'uint8'),8)-'0').',1,[])
After running the code, I'm getting 1xn matrix of type double, having a series of 0's and 1's.
Now I want to convert this matrix g back into an image.
The converted image should be similar to caption.jpg.

You will need the number of rows and number of cols in the initial image to get the color image back.
rowsize = size(X,1);
colsize = size(X,2);
final_image = uint8(reshape(bi2de(reshape(g,8,[])',2,'left-msb'),rowsize,colsize,[]));
imwrite(final_image,'caption.jpg');

Related

How to rescale image data before converting data types

This is a follow up to my previous post here
I'm using the following lines of code to convert the data type(from uint16 to uint8) of z-stack images in MATLAB
%Multiple image tiff conversion%
File_Name = "Test_Image.tiff";
Image_Data = imfinfo(File_Name);
Number_Of_Images = length(Image_Data);
Tiff_Structure = struct('Image_File',[]);
for Image_Index = 1: Number_Of_Images
Image = imread(File_Name,Image_Index);
Uint8_Image = im2uint8(Image);
%For more information and plotting individual images%
Tiff_Structure(Image_Index).Image_File = Uint8_Image;
%Saving the converted images to one tiff file%
imwrite(Uint8_Image,'Converted_Image.tiff','WriteMode','append');
end
In the documentation available here it is mentioned that
im2uint8(I) converts the grayscale, RGB, or binary image I to uint8,
rescaling or offsetting the data as necessary
I would like to know if it is possible to rescale the data before converting the datatype to uint8 and how this rescaling can be done.
Test_Image.Tiff
Suggestions will be really helpful.
EDIT:
Plotting the histogram of the image data gives the following
img_data = imfinfo(f);
n_img = length(img_data);
imgs = cell(1, numel(img_data));
for i = 1:numel(img_data)
imgs{i} = imread(f, i);
end
imgs = cat(3, imgs{:});
figure(1)
imhist(imgs(:), 256)
Assuming Image has values in the range 0-65535 (16bit)
to scale the values into an 8bit image:
first divided the image by 65535 (2^16) - this will normalize value range to 0-1
now you need to rescale to 8bit range of values, multiplying the image by 255
so basically:
scaled_image = uint8(double(Image)./65535*255)
Note: To preserve the dynamic range of the image it might be better to choose a different value to normalize with, e.g some max value across all stack

Show image based on [row,column] array of BLOB

I'm trying to do some image analysis in Matlab. What I have is an image which I first convert into an binary image using a threshold and then I use bwlabel to get the BLOBs of the image. I then find the indexes of the first BLOB and tries to show only the RGB values of this BLOB I(row,column,:). However this obviously does not work and I don't know how to index the original image and get the corresponding RGB values based on the row, column data in the binary image. I suppose I could use a for-loop but the image is 4032 x 3024 and I want to do this kind of processing on a lot of images.
I = imread('someimg.jpg');
Ired = I(:,:,1);
Igreen = I(:,:,2);
Iblue = I(:,:,3);
% threshold
IBinary = Ired > 200 & Igreen > 150 & Iblue > 50;
IBinary = imopen(ISigns,strel('square',9));
% get labbelled BLOBs
IBinaryLabelled = bwlabel(IBinary,8);
% find index of the first label image
[row,column] = find(IBinaryLabelled == 1);
% Get RGB of the BLOB with label 1
RGB = I(row,column,:)
My question is therefore: How to get RGB values based on row,column data of binary image in an efficient manner?

How to retrieve the luminance channel from an rgb image in MATLAB

I'm trying to retrieve the luminance component of a set of 'tif' images in matlab.
The code is bellow:
function [defaultImages] = readImgLum(nFiles)
% readImgLum reads a specified number of images in 'tif' format
% and retrieves the luminance component
narginchk(1, 1);
defaultImages = cell(nFiles, 1); % store all images in a vector
for i = 1 : nFiles
imagePreffix = int2str(i);
imageFullName = strcat(imagePreffix, '.tif');
image = imread(imageFullName);
imageYCbCr = rgb2ycbcr(image);
defaultImages{i} = squeeze(imageYCbCr(:,:,1));
end
Am I correctly extracting the luminance component?
As the comments have stated, there is no need for squeeze. This code also looks fine to me. However, if you want to skip computing all components of YCbCr just to extract the luminance, use the SMPTE / PAL standard for calculating luminance instead. This is actually done in rgb2gray in MATLAB if you want to look up the source.
In any case, assuming your images are unsigned 8-bit integer:
image = double(image);
defaultImages{i} = uint8(0.299*image(:,:,1) + 0.587*image(:,:,2) + 0.114*image(:,:,3));
BTW, image is a built-in command in MATLAB. It takes in any matrix, and visualizes it as an image in a new figure. I highly suggest you use another variable to store your temporary image as you may be calling further code later on that requires image as a function.

convert any image type to RGB using MATLAB

I have an image I get it using this code
[path,user_cance]=imgetfile();
im=imread(path);
Now I don't know if this image is RGB or indexed or ...
How to convert im to RGB for example ?
If it is indexed image you can easily use ind2rgb function:
read the image:
[X,map] = imread('imagefile.tif');
Verify that the colormap, map, is not empty, and convert the data in X to RGB.
if ~isempty(map)
Im = ind2rgb(X,map);
end
finally you can View the size and class of X.
whos Im
ind2rgb converts the matrix X and corresponding colormap map to RGB (truecolor) format.
X can be of class uint8, uint16, single, or double. RGB is an m-by-n-by-3 array of class double.
here you can find the image formats that you can read using MATLAB.
According to Mathworks documentation (http://www.mathworks.fr/fr/help/matlab/ref/imread.html), imread can infer the data type from its content.
You could do test if the colormap exists or is empty :
[im, map] = imread(path_to_image);
if(isempty(map)) % image is RGB or grayscale
if(size(im, 3) == 1) % image is grayscale
im = cat(3, img, img, img);
end
else % image is indexed
im = ind2rgb(im, map);
end
% now 'im' is a RGB-image

How can I invert a binary image in MATLAB?

I have a binary image and need to convert all of the black pixels to white pixels and vice versa. Then I need to save the new image to a file. Is there a way to do this without simply looping over every pixel and flipping its value?
If you have a binary image binImage with just zeroes and ones, there are a number of simple ways to invert it:
binImage = ~binImage;
binImage = 1-binImage;
binImage = (binImage == 0);
Then just save the inverted image using the function IMWRITE.
You can use imcomplement matlab function. Say you have a binary image b then,
bc = imcomplement(b); % gives you the inverted version of b
b = imcomplement(bc); % returns it to the original b
imwrite(bc,'c:\...'); % to save the file in disk
In Matlab, by using not we can convert 1's into 0's and 0's into 1's.
inverted_binary_image = not(binary_image)
[filename, pathname] = uigetfile({'*.bmp'},'Text as image');
img=imread(filename);
img=im2double(img);
[r,c,ch]=size(img);
%imshow(img);
invert_img=img;
if(ch==1)
for i=1:r
for j=1:c
if(invert_img(i,j)==0)
invert_img(i,j)=1;
else
invert_img(i,j)=0;
end
end
end
end