imwrite is corrupting a mex file when I run it
4 次查看(过去 30 天)
显示 更早的评论
Hi everyone,
I'm using Matlab 2023a to convert proprietary .nd2 files into 3D tiffs. I'm saving these tiffs by saving single 2D planes at a time and appending new layers as I load them, all by using imwrite. My program is rather long, so I'll outline the part of the pipeline before the chunk of code responsible for my error:
Generate FilePointer, ImagePointer, and ImageReadOut objects to access the .nd2 file. I use the function ND2Open to do so. It should be noted that I don't close these pointers until I'm done and worry this might be part of the reason I'm crashing.
I then select data of interest from my .nd2, then generate 2D matrices and begin saving them. The following is the code I use to save my tiffs:
currSaveDir = fullfile('My Directory','my_file_name.tif');
for indVec = 1:10
baseMat = rand(50,50); % In reality, I have code generating a 2D matrix from my original .nd2 at this point
if indVec == 1
imwrite(baseMat, currSaveDir);
else
imwrite(baseMat, currSaveDir, 'WriteMode', 'append')
end
end
After running this successfully for several data sets, my MATLAB crashed. Now I'm getting the following error:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1474926/image.png)
Invalid MEX-file 'C:\Program Files\MATLAB\R2023a\toolbox\matlab\imagesci\private\wtifc.mexw64': The specified module could not be found.
Error in writetif (line 110)
wtifc(data, map, filename, writemode, colorspace, required_tags);
Error in imwrite (line 566)
feval(fmt_s.write, data, map, filename, paramPairs{:});
Error in Copy_of_nd2Sideview (line 157)
imwrite(baseMat(:,:,currChan), currSaveDir);
I should note that this error persists whenever I try to save any .tif using imwrite. There IS a file called wtifc.mexw64 in the specified directory, but it's compiled so I have no idea what it's supposed to look like. I also had this issue in MATLAB 2022a
Does anybody understand what's going on? Is there an easy way to repair this file? A clean install of MATLAB did the trick before, but I'd like to know why I'm corrupting my MATLAB to begin with!
Thanks!
EDIT: As mentioned in a comment below, I'm accessing files located on an external hard drive connected via USB 3. This hard drive occasionally disconnects. My error could somehow be related to a disconnection event like this. It's still not clear why the above error would be the one that pops up, nor why the error persists when saving any tif using imwrite (regardless of origin or destination).
0 个评论
回答(2 个)
Sachin Lodhi
2023-9-18
Hello Remi,
It seems that you are experiencing an issue with saving large “tiff” files in MATLAB. After conducting some research, I came across a similar question in the MATLAB Answers community. According to the information provided, the “imwrite” function in MATLAB has a default limit of processing input data up to 2 GB. If your input data exceeds this limit, you will need to modify the value of the “RowsPerStrip” parameter.
The “RowsPerStrip” parameter determines the number of rows included in each strip and should be set as a scalar value, which is a multiple of “8”. By explicitly setting this parameter to a smaller value, you should be able to handle larger tiff files without encountering errors.
I recommend referring to the following links for more detailed information:
I hope this information helps you in resolving the error.
2 个评论
DGM
2023-9-18
编辑:DGM
2023-9-18
This doesn't seem to be related to the question. OP is having a problem with a specific precompiled builtin file that's corrupt. OP ran some code that crashed MATLAB, now wtifc.mex64 is corrupt or unreadable. Nobody mentioned file size or anything, and at this point, file format is irrelevant if the necessary support functions are corrupt.
The question is
- how was this even possible? (no clue)
- how to fix it (probably reinstall or use backups)
- how to prevent it (see #1)
See also:
Image Analyst
2023-9-19
I believe imwrite, when saving tiff files expects them to be uint8 in the range of 0-255 or uint16 in the range 0-65535. You're trying to pass it floating point numbers in the range 0-1. This should work
temp = uint8(randi(255, 50, 50)); % A uint8 variable.
imwrite(temp, 'test.tif');
But of course if it's in the middle of saving and the connection to your destination drive is lost, that would be a problem as well.
2 个评论
DGM
2023-9-19
编辑:DGM
2023-9-19
Imwrite will automatically fix a unit-scale float image when writing to image types which don't support float (i.e. all of them). So OP's use of rand() should be fine.
temp = rand(50,50); % unit-scale float
imwrite(temp, 'test.png'); % no need to convert
recovered = imread('test.png');
isequal(im2uint8(temp),recovered) % it's identical
imwrite(temp, 'test.tif'); % no need to convert
recovered = imread('test.tif');
isequal(im2uint8(temp),recovered) % it's identical
That doesn't explain why wtifc.mex64 had become corrupt or inaccessible. AFAIK, those files should be write-protected, and if it's a pathing or library issue, I don't know why that would get changed either, even if some part of the script execution were interrupted. Of course, all that means is that I don't know. .
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Logical 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!