Trying to convert some grayscale images to RGB (1,1,1).. I have a folder of about 1500 images that I need batch converted using the code below (which works well with individual images)
Interestingly enough,
imwrite(repmat(imread(files(1).name), [1 1 3]),files(1).name)
imwrite(repmat(imread(files(2).name), [1 1 3]),files(2).name)
imwrite(repmat(imread(files(3).name), [1 1 3]),files(3).name)
...(and so forth)
works just fine
files = dir('*.jpeg')
for I=1:length(files)
imwrite(repmat(imread(files(i).name), [1 1 3]),files(i).name)
display(i)
end
Error using writejpg (line 46)
Data with 9 components not supported for JPEG files.
Error in imwrite (line 485)
feval(fmt_s.write, data, map, filename, paramPairs{:});
You need to do two things:
Use the correct variable name for looping, i.e. i or I but not a mix! Note that i has a built in definition as the imaginary constant, so you're better of using I, or something different entirely.
You show a warning for JPEGs with 9 elements not being supported when trying to write the file. This suggests you've blindly used repmat to triplicate an image which is already RBG.
We can address both of these like so:
files = dir('*.jpeg')
for k = 1:length(files)
img = imread( files(k).name ); % Load the image first
% Convert greyscale to RBG if not already RGB
% If it's already RBG, we don't even need to overwrite the image
if size(img,3) == 1
imwrite(repmat(img, [1 1 3]), files(k).name);
end
% Display progress
display(k)
end
Related
I'm using the following code to show an image from array which previously converted to array. But the image not show correctly:
I = imread('ut.jpg');
image=mat2gray(I);
imshow(image);
FID = fopen('FileName.txt', 'w');
if FID == -1, error('Cannot create file.'); end
fprintf(FID, '%g %g %g ... %g \n', image);
fclose(FID);
x = 100*rand(512,1500);
fileID = fopen('FileName.txt','w');
fprintf(fileID,'%f',x);
fclose(fileID);
imshow(x);
Bitmaps consist of two things. First: 3 matrices with colour intensities, graduated as uint, numbers from 0 to 255. Second: A header containing information regarding size, colordepth, filelength, etc.
Your program does not create correct images because it misses a header.
Regarding the matlab procedure:
By using imread on a RGB image, you are automatically creating a matrix. If you convert it to grayscale with rgb2gray you will have a single matrix of uint (without any additional layers).
If you want to safe your image after processing it simply use:
I = imread('ut.jpg');
% Convert, do smth e.g. I_new = rgb2gray(I);
filename = 'myNewImage.jpg';
imwrite(I_new,filename);
By using imwrite you automatically create a correct header.
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).
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).
I'm working with some MRI data in Matlab 2014b, but the data is formed of intensity values not RGB. To get around this I use the code below to form a movie out of the MRI frames (I'm working on dynamic data here)
My problem is that the images need to have altered display values for the pixels, as the default only displays between -Inf and Inf, and I need between 0 and 0.25 to get a sensible image out of my data.
Are there any ways to pass that change from the script in to the movie, and then to write to file? I can only seem to do this per image in implay, and I'd like an automated way to edit each image and then store as a frame for a movie..?
%Code for producing movie.
graymap = gray(256);
for i = 1:32
a(:,:,i) = cmunique(Reformed_Data_Colourmap(:,:,i));
end
for i = 1:32
b = im2frame(a(:,:,i),graymap);
a(:,:,1) = ((b.cdata));
image(a(:,:,1))
colormap 'gray'
%The change needs to be here, to display pixel values from 0 to 0.25, to allow for a sensible image from the MR data.
frames(1,i) = getframe;
end
movie(frames)
The solution is provided:
for i = 1:32
b = im2frame(a(:,:,i),graymap);
a(:,:,1) = ((b.cdata));
clims = [0 250];
%image(a(:,:,1),clims)
colormap 'gray'
imagesc(a(:,:,1),clims);
%set('window', [0 400])
frames(1,i) = getframe;
end
clims solves the issue.
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.