imcrop not working for tiff
3 views (last 30 days)
Show older comments
Hi, i want to crop several .tiff pics so i wrote below code but it dosnt work for .tiff files. it works for .png .jepg . i used imshow it shows nothing for .tiff files.
clc;
clear all;
address='D:\all\uni things\1-master\master project\6-Data\1-pics\1-main\2\L\2-E\5';
filenames=dir(fullfile(address,'*.tiff'));
for n=1:numel(filenames)
fullname=fullfile(address,filenames(n).name);
t1=imread(fullname);
info = imfinfo(fullname);
t2=imcrop(t1,[10, 10, 1000, 950]);
t3=size(t2);
imwrite(t2,fullname);
end
3 Comments
Umar
on 24 Jul 2024
Edited: DGM
on 24 Jul 2024
Hi Ali,
I have made modifications to your code. Below is the updated code with added cropping and display functionality:
% Define the directory containing the .tiff images
address = 'D:\all\uni things\1-master\master project\6-Data\1-pics\1-main\2\L\2-E\5';
% Get a list of .tiff files in the directory
filenames = dir(fullfile(address, '*.tiff'));
% Loop through each image file
for i = 1:length(filenames)
% Read the image
t = Tiff(fullfile(address, filenames(i).name), 'r');
imageData = read(t);
% Perform cropping operations on the image
% Add your cropping code here
% For example, crop the image to a specific region
croppedImage = imcrop(imageData, [x y width height]);
% Display the cropped image
figure;
imshow(croppedImage);
title('Cropped Image');
% Save the cropped image (optional)
imwrite(croppedImage, 'cropped_image.tif');
end
For more information on Tiff and read functions, please refer to
I hope this answers your question.
DGM
on 24 Jul 2024
It's hard to be sure what the problem was, but there are a couple other possibilities to consider.
A TIFF might be indexed color. Without reading the color table from the file, the index array may not render in a manner which appears correct or is easily visible.
% unzip the sample tiffs
unzip tiffbucket.zip
% an indexed image with a short colortable (12 colors)
[inpict,CT] = imread('indexedblobs.tiff');
% displaying the index array by itself appears black
% because the dynamic range is only [0 11] versus [0 255]
% as would be expected of a uint8 intensity image
imshow(inpict)
% but displaying the image with its color table fixes that
imshow(inpict,CT)
% similarly, if the goal is to save a copy, we need the color table
imwrite(inpict,CT,'myblobs.tiff')
Similarly, a TIFF might not contain full-scale image data. It's not uncommon for raw images to be represented in wider integer formats than the actual dynamic range of a specific image may require. Avoiding preprocessing is part of the appeal of capturing raw data, after all. If your image is (e.g.) uint16, but the data itself spans a smaller interval (e.g. [1000 2000], then the raw data will be rendered on screen as a nearly-black image.
% arbitrary-scale data stored in uint16
inpict = imread('cman_arbrange_u16.tiff');
% the dynamic range of the data isn't tied to the numeric class
imrange(inpict)
% scale-dependent tools expect black and white to be implied by the class
% in this case, black is at 0, and white is at 65535
% so the image is almost black (hard to see against the white background)
imshow(inpict) % use native-scale (uint16-scale)
% for viewing, you can use the DisplayRange parameter
% (this only works for single-channel images)
imshow(inpict,[]) % scale to data extrema
% or you can normalize the image using mat2gray(), rescale(), etc
outpict = mat2gray(inpict); % normalize to unit-scale float
outpict = im2uint16(outpict); % maybe we want to keep the original class
% at that point, the image could be saved
imwrite(outpict,'cman_rescaled.tiff')
Again, it's hard to know what the problems might have been, but these are fairly common pitfalls. If you run into images which unexpectedly appear black (or white), it's a good start to check that the data scale is appropriate for its class. How that should be resolved depends on the goals and order of operations .
Answers (1)
Karan Singh
on 25 Jul 2024
Hi @Ali yousefi, I think @DGM has solved the query, for the crop part I dont think there is any mistake, here I have tried to keep a check for the same and the images are not empty so you can move forward with your origional code.
clc;
clear all;
unzip tiffbucket.zip
[t1,CT] = imread("indexedblobs.tiff");
if isempty(t1)
disp(['Failed to read image: ', fullname]);
end
figure;
imshow(t1,CT);
title(['Original Image: ', "indexedblobs.tiff"]);
t2 = imcrop(t1, [200, 10, 2000, 950]);
figure;
imshow(t2,CT)
0 Comments
See Also
Categories
Find more on Basic Display in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!