how to denormalize the image in matlab - image

I am working on a project where i have to first normalize the image i.e convert the pixel range of my image from 16-235 (as it is only luminance component of the image , pixel values are always defined in the range 16-235) to 0 - 1 then i perform dwt and idwt on the image after the processing i want to again maps these pixels values from 0 - 1 to 16 - 235. ie if the pixel value is 1 value then it should be stored in the image matrix as 235.
for which i tried the following matlab code ,this code works perfectly fine when i denormalize the image immediately after normalization , but output becomes complete white if i use this code after performing dwt and idwt.
for j = 1:1:361
for k=1:1:361 %dimensions of my matrix
if(distorted1(j,k)~=0)
temp=distorted1(j,k);
temp1=temp/0.0045;
distorted1(j,k)=int8(temp1)+1;
end
end
end
imshow(distorted1);

it seems like the result of your idwt is of type double ( or single ) and not int anymore. convert to uint8 and then imshow it.
Trying to imshow an image of type double/single will saturate all pixels with values larger than 1.0 (rather than 255 for images of type int).

Related

How to read image from pixel data?

I am trying to read image of cifar10 dataset in MATLAB. The data is given in 10000x3072 format in which one row contains corresponding RGB value. I used:
img= reshape(data(1, 1:1024), [32,32]);
image(img)
to convert the image into meaningful because it is showing garbage image. How can I read the image from this .mat file? from this dataset https://www.cs.toronto.edu/~kriz/cifar-10-matlab.tar.gz
According to this page, the format of data is:
data -- a 10000x3072 numpy array of uint8s. Each row of the array stores a 32x32 colour image. The first 1024 entries contain the red channel values, the next 1024 the green, and the final 1024 the blue. The image is stored in row-major order, so that the first 32 entries of the array are the red channel values of the first row of the image.
Using your code:
img= reshape(data(1, 1:1024), [32,32]);
you should get the red channel of the first image in column-major order (i.e. transposed). To get a full RGB image with the correct orientation, you'll want to use:
img = reshape(data(1, 1:3072), [32,32,3]); % get 3-channel RGB image
img = permute(img, [2 1 3]); % exchange rows and columns

How to transfer the .mat file to image file without any change?

I have a image.mat of about 4MB.
The size of some image file can also be 4MB.
Can the image.mat be transferred to image file?
I tried this, but that doesn't do the trick:
load image.mat %load Iw
imshow(mat2gray(Iw))
imwrite(Iw,'image.png');
IwNew = imread('image.png');
isequal(Iw,IwNew)
The result is 0; am I misunderstanding something?
The number in Iw are very important, so Iw can not be changed.
Actually my real problem is how to store float numbers into an image?
But MATLAB does not support Tiff 6.0, so I'll have to find some workaround.
I am doing a blind watermarking,and the decimal fraction of a number in Iw is important because it involve the information about another image.So the Iw can not be changed.
Actually,Mathematica can store floating floating-point data:
But my programs are all in MATLAB.
According to Matlab documentation:
"If A is a grayscale or RGB color image of data type double or single, then imwrite assumes that the dynamic range is [0,1] and automatically scales the data by 255 before writing it to the file as 8-bit values."
In other words: imwrite performs automatic conversion from double to uint8.
if you wish to keep the values of Iw unchanged, save it as a mat file and not as an image.
If you do want to save it as an image - there is going to be some loss of information. In this case, there are two things which need to be done:
Change the dynamic range of the matrix to [0,1]. (in your case, the range is between -0.0035 to 255.0035. Also, the matrix contain inf values).
If you want to get an equality, scale IwNew by 255, and convert it to uint8.
Code:
load image.mat %load Iw
%step 1, change the dynamic range of the image to [0,1].
%One way to do it is by using mat2gray on each channel separately.
Iw(:,:,1) = mat2gray(Iw(:,:,1));
Iw(:,:,2) = mat2gray(Iw(:,:,2));
Iw(:,:,3) = mat2gray(Iw(:,:,3));
%write the image to file
imwrite(Iw,'image.png');
%read the image
IwNew=imread('image.png');
%scale it, and convert to uint 8
Iw2 = uint8(Iw*255);
%check equality
isequal(Iw2,IwNew)
Result:
ans =
1
Alternatively, if you want to convert IwNew to double, perform the following:
%conversion to double
Iw2 = double(IwNew)/255;
Notice that in this case, the matrices won't be equal to one another,
Due to the loss of information which happened during the imwrite process (conversion from double to uint8).
Instead, they will be epsilon-close to one another, where epsilon = 0.0001.
In order to test this, write the following:
%equality check
sum(abs(Iw2(:)-Iw(:))>0.0001)
Result:
ans =
0
My MATLAB (R2010a) with the image processing toolbox is perfectly capable of storing double-valued pixel values, and retrieve them without loss of data.
Here's a shameless copy of this answer:
% Some random, data of type double
A = 7.6*rand(10);
% Construct TIFF image...
t = Tiff('test.tif', 'w');
% ...with these custom parameters...
tagstruct = struct(...
'ImageLength' , size(A,1),...
'ImageWidth' , size(A,2),...
'Compression' , Tiff.Compression.None,...
'SampleFormat' , Tiff.SampleFormat.IEEEFP,... % floating point
'Photometric' , Tiff.Photometric.MinIsBlack,...
'BitsPerSample' , 64,... % 8 bytes / double
'SamplesPerPixel' , 1,...
'PlanarConfiguration', Tiff.PlanarConfiguration.Chunky);
t.setTag(tagstruct);
% ...and write it to disk.
t.write(A);
t.close();
% Read the data actually written, and check if all
% information was indeed preserved:
B = imread('test.tif');
isequal(A,B)
Result:
ans =
1
Adjust in obvious ways if you have more than 1 channel (RGB).

Image blending with mask

I'm trying to combine the two images based on the information from the mask. I'm using the color information from the background image if the mask is 0 and color information from foreground image if the mask is 1. Because the mask and both
Images are of the same size, I would like to use logical indexing of matrices to achieve this.
My attempt:
mask = imread('mask.png');
foreground = imread('fg.jpg');
background = imread('bg.jpg');
[r,c,~]=size(mask);
A = zeros(size(mask));
for i=1:r
for j=1:c
if mask(i,j) == 0
A(i,j,:) = background(i,j,:);
end
if mask(i,j) > 0
A(i,j,:) = foreground(i,j,:);
end
end
end
imshow(A);
The result looks like a flickering blue image, but I don't want that. Please help.
You can do this a bit more concisely:
f = double(foreground).*double(mask);
b = double(background).*double(~mask);
blend = f+b;
imshow(blend, []);
Using logical indexing you could also do
foreground(logical(mask)) = 0;
background(logical(~mask)) = 0;
blend = foreground+background;
The ISNOT operator '~' inverts your matrix in the second line, so you cut out the area you would like for background.
NOTE: This works for black and white (one channel). For coloured images see rayryeng's solution.
There are two problems with your code. The first problem is that you are trying to assign colour pixels to the output image A, yet this image is only two-dimensional. You want an image with three channels, not two. In addition, the output image type you are specifying is wrong. By default, the output image A is of type double, yet you are copying values into it that aren't double... most likely unsigned 8-bit integer.
As such, cast the image to the same type as the input images. Assuming both input images are the same type, initialize your A so that:
A = zeros(size(foreground), class(foreground));
This correctly makes a colour image with the same type as any of the inputs, assuming that they're both the same type.
Now, your for loop is fine, but it's better if you do this in one shot with logical indexing. If you want to use logical indexing, create a new image that's initially blank like what you've done, but then make sure your mask has three channels to match the number of channels the other images have. After, you simply need to index into each image and set the right locations accordingly:
mask = imread('mask.png');
foreground = imread('fg.jpg');
background = imread('bg.jpg');
[r,c,d]=size(mask); %// Change
%// If your mask isn't three channels, make it so
%// Change
if d ~= 3
mask = cat(3, mask, mask, mask);
end
A = zeros(size(foreground), class(foreground)); %// Change
A(mask) = foreground(mask); %// Assign pixels to foreground
A(~mask) = background(~mask); %// Assign pixels to background
imshow(A);

Converting a grayscale image to black and white

I have a grayscale image that only has the values 60 and 117. How can I convert the image to only black and white without graylevels?
I tried the matlab function gray2ind, but didn't get the expected output.
Thanks.
Try im2bw(img, level) with level = 0.5.
This is a matlab function that takes a grayscale image img, applies a threshold of level (a value between [0,1]) and returns a black and white image.
This function is part of the Image Processing Toolbox. Your case is simple enough that you could also try something like:
bwImg = false(size(img));
bwImg(img == 117) = true;
I edited the above to set values equal to false/true to more closely mimic Matlab's im2bw() which returns a matrix of logical values rather than ints.
2nd Edit: Modified the code block to reflect improvements suggested by #Amro

OpenCV: create image from floating point data

I have a floating point data [size: 4000 X 140 ]. I want to convert it to an IplImage in OpenCV. To have an idea about data, I am giving first 8 X 8 entries of that data.These data are very close to zero. So, I am getting a dark image.
-1.14E-04 -4.71E-04 -1.27E-04 2.43E-04 4.58E-04 1.63E-04 2.56E-04 2.86E-04
1.12E-04 -2.80E-04 2.89E-05 -2.18E-04 4.08E-05 -2.23E-04 -7.96E-05 -3.97E-05
-3.98E-04 -2.35E-04 6.11E-04 4.53E-05 4.74E-05 8.02E-05 2.10E-04 1.10E-04
2.08E-04 3.09E-04 -1.34E-04 -2.58E-04 -2.25E-04 -1.74E-04 2.28E-04 2.65E-04
-6.65E-04 -2.94E-04 6.37E-04 -5.16E-05 9.90E-05 1.05E-04 -2.20E-04 -5.49E-05
1.85E-04 5.69E-04 -5.19E-04 -4.98E-05 2.07E-04 -2.00E-05 1.24E-04 1.49E-04
1.54E-04 -4.09E-04 4.29E-04 -7.67E-04 5.19E-04 3.56E-04 -4.82E-04 3.66E-04
-1.71E-04 -5.15E-04 5.71E-04 -5.68E-04 -2.75E-04 -6.17E-05 1.40E-04 2.19E-04
1) when I am multiplying these entries with a factor like 10E4 or 10E5, I can see an image. But image quality is very poor unlike matlab-generated image.
[
MATLAB code corresponding image:
[path,file] = uigetfile;
data = load(strcat(file,path));
figure;
imagesc(data);
colormap(gray);
]
[
OpenCV code sequence:
I created CvMat and filled it with those data.
I prepared IplImage from that CvMat.
I resized the image( 560 X 420 )
]
2) There are many negative data. Should those be consider zero? or Should all the data be added with a number(like 10E-4) to make all entries positive? or should I proceed through someother ways?
3) I changed contrast , brightness. But those seem to be useless.
Try mapping the minimum value to 0.0 and the maximum to 1.0.

Resources