Demietra95
Demietra95

Reputation: 296

imread function doesn't work in matlab

I am new to Image processing and learning matlab. Actually till now I have used matlab cloud version where uploading an image was directly possible and didn't face any issue. But now I am facing an extreme problem of uploading an image in matlab offline version software.

  1. I imported an image by using "import data"

  2. Then I wrote this command

h = imread( 'digi1.jpg');

  1. But my first line didn't run saying digi1 doesn't exist.

Upvotes: 0

Views: 1821

Answers (1)

rayryeng
rayryeng

Reputation: 104464

The problem you are experiencing comes from saving the data to a MATLAB data file, renaming the file with a .jpg extension and trying to use imread to read in the data.

This unfortunately doesn't work. You can't change the file type of your data from .mat to .jpg. All you're doing is changing what the name of the file is. You're not changing the contents of the file. Changing the file extension and name of the file does not mean that the contents will change. Renaming it to digi1.jpg would still make this file a MATLAB MAT file which you can only read with software that can read these files (Python, R, and yes MATLAB of course).

As such, try using load name.mat (which is the name of your MAT file) in the command prompt. This should give you the image already loaded into the workspace. Whatever that variable is called, use imwrite to save the image to file.

Assuming that the image was stored in a variable called A, do something like this:

>> load name.mat
>> imwrite(A, 'digi1.jpg');

Make sure name.mat is in the current working directory of where you are trying to run the above code. You should now get an image saved on your disk. However, I would recommend you use something lossless instead of JPG if you want to maintain the quality of your image. Try using PNG instead, so save your image as a .png rather than .jpg.

Upvotes: 1

Related Questions