Matlab Read Tif File shows Error "Cannot handle different values per sample for "BitsPerSample"." - matlab

I have a tif file that I can view it from Windows Photos App. You can download it from this link.
I tried to load it to Matlab using imread function, however it shows an error below.
TIFF library error - 'TIFFReadDirectory: Cannot handle different values per sample for
"BitsPerSample".'
I then further looking into the file's profile and find that the BitDepth and BitsPerSample value seems not correct. Also, the MaxSample value looks weird.
By checking the Matlab buildin tiff file profile, I learned that for a RGB image, BitDepth should be 24 and BitsPerSample should be [8,8,8]. However, when I tried to explicitly change them, I still get the same error.
fname = 'TifImg.tif';
info = imfinfo(fname);
% Explicitly Assign Correct Value to BitDepth and BitsPerSample (still doesn't work)
for i = 1: length(info)
info(i).BitDepth = 24;
info(i).BitsPerSample = [8 8 8];
end
% Read Tif Image
frame = imread(fname, 1, 'Info', info);
imshow(frame,[])
I hope someone can help me to load this image to Matlab and point me which profile I should change to successfully load the file.

As mentioned in the comments above, the file seemed to be corrupt. I was also unable to open it with gimp or other programs. So here is a hack. You get the size from the header with imfinfo (height by width by 3 colors), then you read the file from the end. A bit of reordering and the image is salvaged. I don't know about the large BitsPerSample you have in the header, if you use [8,8,8] you have the exact length you need. If you use larger color depth etc you don't have enough numbers in the file to fill the 520x696 pix image.
info = imfinfo('TifImg.tif');
fid = fopen('TifImg.tif','r');
s = fread(fid,'uint8=>uint8');
fclose(fid);
len = info.Height*info.Width*3;
data = s(length(s)-len+1:end);
img = reshape(data,3,info.Width,info.Height);
img = permute(img,[3,2,1]);
figure;
imshow(img)

Related

MATLAB - imwrite error in certain folders

this is my first question so I hope I am writing in the right place.
I am currently working with a Grasshopper3 camera, model: GS3-U3-50S5M-C from Point Grey Research connected to MATLAB.
I am recording a lot of frames at a time in grayscale and in order to avoid filling up my memory I would like to move some of the frames to my disk as stacked TIFFs during the recordings. The stacked TIFF format is needed for other programs used.
I am doing this by moving the recorded frames from the camera to the workspace using getdata. This creates a 4-D uint8 object containing height, width, color plane and frame number of the frames. In order to save the frames as a stacked TIFF I use a for loop with imwrite to save one frame at a time as shown below. However when I do this I most often get the error:
Error using imwrite (line 454) Unable to open file "C:\Users\My_User\Desktop\CraneStuff3\Name.tif" for writing. You might not have write permission.
When this happens MATLAB has typically saved some of the frames, e.g. if I have recorded 500 frames it might save 300 before giving me the error. Sometimes it manages to save every frame with no error, sometimes it only saves 30 frames before failing. I do not understand how it can save some frames and then claim that it cannot open the file for writing.
I have noticed, however, that if I instead save the file in "C:\Users\My_User\Documents\MATLAB" it seems to never give any error and save every frame as wanted.
Does anyone know why this is the case or how I can fix my problem?
I have included my code below here.
Note: I tried saving the frames as a .mat-file. To do this I had to use version '-v7.3' in save.
Thank you advance.
% Connect to camera:
vid = videoinput('pointgrey',1,'F7_Mono8_2448x2048_Mode0');
vid.TriggerRepeat = 100;
vid.FrameGrabInterval = 1;
% Make a recording:
start(vid)
pause(50)
stop(vid)
% Get data from camera to workspace:
data = getdata(vid,vid.FramesAvailable);
% Save data to disk one image at a time:
num = size(data);
output_name = fullfile('C:\Users\My_User\Desktop\CraneStuff3', ['Name' '.tif']);
for k = 1:num(4)
imwrite(data(:,:,:,k),output_name,'WriteMode','append');
end
One possible solution is to try saving each of the frames separately into a tiff file.
If you save a .mat file, better use savefast

Matlab thinks an AVI it's written is corrupt

I'm using matlab to interface with a scientific camera using mex, and my matlab program uses VideoWriter() to write the file to disc. The camera is RGB-capable, and if I write the file as such, the video is fine. However, for the current application, I need grayscale images, and so I'm using rgb2gray() to convert it. Unfortunately, when the analysis code tried to read the video file again, I get the error:
Error using VideoReader/init (line 450)
Unable to read the file. The file appears to be corrupt.
and attempting to read the video with VLC confirms it to be corrupt. The only difference in my code between they grayscale and colour versions is the line:
frame = rgb2gray(frame);
My whole writing section of code is:
vid = VideoWriter('testVid.avi');
vid.FrameRate = framerate;
vid.Quality = 100;
open(vid);
for i = 1 : frames;
%read frame data into variable 'frame'
frame = rgb2gray(frame);
writeVideo(vid,frame);
end
I've spent far too long fighting with this, any ideas?
You need to close the video object, using close(vid) after writing the last frame.

insert text into image without computer vision system toolbox - Matlab

In the Matlab central (http://www.mathworks.com/matlabcentral/answers/26033-how-to-insert-text-into-image) I found this code:
Text = sprintf('Create text inserter object \nwith same property values');
H = vision.TextInserter(Text);
H.Color = [1.0 1.0 0];
H.FontSize = 20;
H.Location = [25 25];
I = im2double((imread('football.jpg')));
InsertedImage = step(H, I);
imshow(InsertedImage);
How can I do the same without using the computer vision system toolbox?
Thanks
To follow up on my comment:
A = imread('football.jpg');
YourText = sprintf('Create text inserter object \nwith same property values');
figure;
imshow(A);
hText = text(25,25,YourText,'Color',[1 1 0],'FontSize',20);
Giving the following:
Look at the doc page to see all the options available to modify/customize your text. Notice that you don't have to use sprintf to generate the string, however the newline character (\n) must be used inside sprintf in order to work.
EDIT In response to your comment below, if you need to save the image with text embedded in it you can use getframe to get the content of the figure and then imwrite to save it:
hFrame = getframe(gca) %// Get content of figure
imwrite(hFrame.cdata,'MyImage.tif','tif') %// Save the actual data, contained in the cdata property of hFrame
EDIT #2 As alternatives to using getframe, look here as there are 2 nice ideas proposed, i.e. using saveas or avi files.
The answer above (Benoit_11) requires that you display the image first, write the text, then save the image. This becomes very slow if you are processing the frames of a video (not a single image, or a few of them). To get a much faster processing time, you have to write directly to the image matrix. One alternative I can think of (but that is not very elegant) is to create (or download) small templates for the alphabet characters (e.g. 20x20), then overwrite the image matrix in the required region with those templates. For example, for an RGB "true color" image, we'll have something like this:
im(y:y+19,x:x+19,:)=template;

matlab ind2rgb trobule (showing and saving is different)

I am trying to save the figure as a png file (or any)
But I want to save only the image and the image does not have to have high resolution.
Just screencapture-like quality is fine for me. (image-checking purpose)
Following is my example in matlab:
figure, imagesc(),axis image;
D=get(gca,'Children'); Child=get(D); data=Child.CData;
m8=uint8((data-min(min(data)))./max(max((data-min(min(data))))).*255); % uint 255 normalization
imshow(m8),axis image;
rgbImage = ind2rgb(m8, jet);
imwrite(rgbImage,'test.png');
Here, if I remove "imshow(m8),axis image", the saved image is awkward.
I don't know why the result is different whether there is imshow(m8) or not.
anyone could help me?
I am using Matlab R2014a in windows 7 64bit
+
sometimes, it works properly when imshow(m8) is removed.
But once it does not work improperly,
imshow(m8)
determines the result. I cannot understand this.

Image processing Error in MATLAB

I was given a defined set of images (.png), I am supposed to detect each images Edges, then apply some image processing, but I have a problem.
First I an image array as follows :
imgArray = {'image_1.png','image_2.png','image_3.png'}
Then applied edging (sobel), using the MATLAB built in function edge so :
for i = 1:3
image=imread(imgArray{i});
image = edge(image,'sobel');
imgArray{i} = image;
end
based on that prvious code and my understanding, that the imageArray, now contains all 3 edged images.
Later on, I need to use the Edged images using that command image=imread(imgArray{i}); in a different place in the code, but it gives me an Error, I dont understand why does that happen ??
EDIT:
Here's the error I'm getting:
Error in ==> ImageCompare at 43 image=imread(imgArray{i});
imgArray = {'image_1.png','image_2.png','image_3.png'};
imgArrayEdged = strrep(imgArray, '.png', '_edged.png');
for i = 1 : length(imgArray)
image = imread(imgArray{i});
image = edge(image,'sobel');
imwrite(image, imgArrayEdged{i});
end
% later...
for i = 1 : length(imgArray)
if (your_condition)
image = imread(imgArray{i});
else
image = imread(imgArrayEdged{i});
end
end
Your imgArray contains file names as strings. In your loop, you are reading the image files and replacing each string in the cell array with image data.
If you absolutely require the file name strings later, you must create a second variable to hold the image data itself. If you only require the original images, just don't use imread later in the code!
Having read in an image file with imread once, there is no reason to waste time by reading the files again. It seems like you aren't quite aware of what state your data is in as it moves through your code. I suggest you use MATLAB's excellent debugger to step through and examine the type and content of the variables - you will quickly see where imread, which needs a file name, is inappropriate.